Skip getpaid's PaymentMethodForm,
[wolnelektury.git] / apps / funding / views.py
1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 #
5 from django.views.decorators.cache import never_cache
6 from django.conf import settings
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.utils.decorators import method_decorator
11 from django.views.generic import TemplateView, FormView, DetailView, ListView
12 import getpaid.backends.payu
13 from getpaid.models import Payment
14 from .forms import FundingForm
15 from .models import Offer, Spent, Funding
16
17
18 def mix(*streams):
19     substreams = []
20     for stream, read_date, tag in streams:
21         iterstream = iter(stream)
22         try:
23             item = next(iterstream)
24         except StopIteration:
25             pass
26         else:
27             substreams.append([read_date(item), item, iterstream, read_date, tag])
28     while substreams:
29         i, substream = max(enumerate(substreams), key=lambda x: x[1][0])
30         yield substream[4], substream[1]
31         try:
32             item = next(substream[2])
33         except StopIteration:
34             del substreams[i]
35         else:
36             substream[0:2] = [substream[3](item), item]
37
38
39 class WLFundView(TemplateView):
40     template_name = "funding/wlfund.html"
41
42     def get_context_data(self):
43         def add_total(total, it):
44             for tag, e in it:
45                 e.total = total
46                 if tag == 'spent':
47                     total += e.amount
48                 else:
49                     total -= e.wlfund
50                 yield tag, e
51
52         ctx = super(WLFundView, self).get_context_data()
53         offers = []
54         for o in Offer.objects.all():
55             if o.state() == 'lose':
56                 o.wlfund = o.sum()
57                 if o.wlfund > 0:
58                     offers.append(o)
59             elif o.state() == 'win':
60                 o.wlfund = o.sum() - o.target
61                 if o.wlfund > 0:
62                     offers.append(o)
63         amount = sum(o.wlfund for o in offers) - sum(o.amount for o in Spent.objects.all())
64         print offers
65
66         ctx['amount'] = amount
67         ctx['log'] = add_total(amount, mix(
68             (offers, lambda x: x.end, 'offer'),
69             (Spent.objects.all(), lambda x: x.timestamp, 'spent'),
70         ))
71         return ctx
72
73
74 class OfferDetailView(FormView):
75     form_class = FundingForm
76     template_name = "funding/offer_detail.html"
77     backend = 'getpaid.backends.payu'
78
79     def dispatch(self, request, slug=None):
80         if slug:
81             self.object = get_object_or_404(Offer.public(), slug=slug)
82         else:
83             self.object = Offer.current()
84             if self.object is None:
85                 raise Http404
86         return super(OfferDetailView, self).dispatch(request, slug)
87
88     def get_form(self, form_class):
89         if self.request.method == 'POST':
90             return form_class(self.object, self.request.POST)
91         else:
92             return form_class(self.object, initial={'amount': settings.FUNDING_DEFAULT})
93
94     def get_context_data(self, *args, **kwargs):
95         ctx = super(OfferDetailView, self).get_context_data(*args, **kwargs)
96         ctx['object'] = self.object
97         if self.object.is_current():
98             ctx['funding_no_show_current'] = True
99         return ctx
100
101     def form_valid(self, form):
102         funding = form.save()
103         # Skip getpaid.forms.PaymentMethodForm, go directly to the broker.
104         payment = Payment.create(funding, self.backend)
105         gateway_url = payment.get_processor()(payment).get_gateway_url(self.request)
106         payment.change_status('in_progress')
107         return redirect(gateway_url)
108
109
110 class OfferListView(ListView):
111     queryset = Offer.public()
112
113     def get_context_data(self, *args, **kwargs):
114         ctx = super(OfferListView, self).get_context_data(*args, **kwargs)
115         ctx['funding_no_show_current'] = True
116         return ctx
117
118
119 class ThanksView(TemplateView):
120     template_name = "funding/thanks.html"
121
122     def get_context_data(self, *args, **kwargs):
123         ctx = super(ThanksView, self).get_context_data(*args, **kwargs)
124         ctx['funding_no_show_current'] = True
125         return ctx
126
127 class NoThanksView(TemplateView):
128     template_name = "funding/no_thanks.html"