fix
[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(self, *args, **kwargs):
34         return redirect(self.object.get_absolute_url())
35
36     def get_context_data(self, *args, **kwargs):
37         ctx = super(OfferDetailView, self).get_context_data(*args, **kwargs)
38         ctx['entry'] = self.object.entry
39         return ctx
40
41     def get_form(self, form_class):
42         return form_class(self.object, self.request.POST)
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(DetailView):
54     model = Payment
55     template_name = "shop/thanks.html"
56
57
58 class NoThanksView(DetailView):
59     model = Payment
60     template_name = "shop/no_thanks.html"