16634e730db476beff7d6d69350f651cc10487b9
[prawokultury.git] / shop / views.py
1 # -*- coding: utf-8 -*-
2 # This file is part of PrawoKultury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 #
5 from datetime import date
6 from django.views.decorators.cache import never_cache
7 from django.core.urlresolvers import reverse
8 from django.http import Http404
9 from django.shortcuts import redirect, get_object_or_404
10 from django.views.decorators.csrf import csrf_exempt
11 from django.views.generic import TemplateView, FormView, DetailView, ListView
12 import getpaid.backends.payu
13 from getpaid.models import Payment
14 from . import app_settings
15 from .forms import OrderForm
16 from .models import Offer, Order
17
18
19
20 class OfferDetailView(FormView):
21     form_class = OrderForm
22     template_name = "shop/offer_detail.html"
23     backend = 'getpaid.backends.payu'
24
25     @csrf_exempt
26     def dispatch(self, request, slug):
27         if getattr(self, 'object', None) is None:
28             lang = request.LANGUAGE_CODE
29             args = {'entry__slug_%s' % lang: slug}
30             self.object = get_object_or_404(Offer, **args)
31         return super(OfferDetailView, self).dispatch(request, slug)
32
33     def get_context_data(self, *args, **kwargs):
34         ctx = super(OfferDetailView, self).get_context_data(*args, **kwargs)
35         ctx['entry'] = self.object.entry
36         return ctx
37
38     def get_form(self, form_class):
39         if self.request.method == 'POST':
40             return form_class(self.object, self.request.POST)
41         else:
42             return form_class(self.object)
43
44     def form_valid(self, form):
45         order = form.save()
46         # Skip getpaid.forms.PaymentMethodForm, go directly to the broker.
47         payment = Payment.create(order, self.backend)
48         gateway_url_tuple = payment.get_processor()(payment).get_gateway_url(self.request)
49         payment.change_status('in_progress')
50         return redirect(gateway_url_tuple[0])
51
52
53 class ThanksView(TemplateView):
54     template_name = "shop/thanks.html"
55
56
57 class NoThanksView(TemplateView):
58     template_name = "shop/no_thanks.html"