047be49aba5e4c050688fb25521bfca9c22e512e
[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 from getpaid.forms import PaymentMethodForm
13 from .forms import DummyForm
14 from .models import Offer, Spent, Funding
15
16
17 def mix(*streams):
18     substreams = []
19     for stream, read_date, tag in streams:
20         iterstream = iter(stream)
21         try:
22             item = next(iterstream)
23         except StopIteration:
24             pass
25         else:
26             substreams.append([read_date(item), item, iterstream, read_date, tag])
27     while substreams:
28         i, substream = max(enumerate(substreams), key=lambda x: x[1][0])
29         yield substream[4], substream[1]
30         try:
31             item = next(substream[2])
32         except StopIteration:
33             del substreams[i]
34         else:
35             substream[0:2] = [substream[3](item), item]
36
37
38 class WLFundView(TemplateView):
39     template_name = "funding/wlfund.html"
40
41     def get_context_data(self):
42         def add_total(total, it):
43             for tag, e in it:
44                 e.total = total
45                 if tag == 'spent':
46                     total += e.amount
47                 else:
48                     total -= e.wlfund
49                 yield tag, e
50
51         ctx = super(WLFundView, self).get_context_data()
52         offers = []
53         for o in Offer.objects.all():
54             if o.state() == 'lose':
55                 o.wlfund = o.sum()
56                 if o.wlfund > 0:
57                     offers.append(o)
58             elif o.state() == 'win':
59                 o.wlfund = o.sum() - o.target
60                 if o.wlfund > 0:
61                     offers.append(o)
62         amount = sum(o.wlfund for o in offers) - sum(o.amount for o in Spent.objects.all())
63         print offers
64
65         ctx['amount'] = amount
66         ctx['log'] = add_total(amount, mix(
67             (offers, lambda x: x.end, 'offer'),
68             (Spent.objects.all(), lambda x: x.timestamp, 'spent'),
69         ))
70         return ctx
71
72
73 class OfferDetailView(FormView):
74     form_class = DummyForm
75     template_name = "funding/offer_detail.html"
76
77     def dispatch(self, request, slug=None):
78         if slug:
79             self.object = get_object_or_404(Offer.public(), slug=slug)
80         else:
81             self.object = Offer.current()
82             if self.object is None:
83                 raise Http404
84         return super(OfferDetailView, self).dispatch(request, slug)
85
86     def get_form(self, form_class):
87         if self.request.method == 'POST':
88             return form_class(self.object, self.request.POST)
89         else:
90             return form_class(self.object, initial={'amount': settings.FUNDING_DEFAULT})
91
92     def get_context_data(self, *args, **kwargs):
93         ctx = super(OfferDetailView, self).get_context_data(*args, **kwargs)
94         ctx['object'] = self.object
95         if self.object.is_current():
96             ctx['funding_no_show_current'] = True
97             ctx['payment_form'] = PaymentMethodForm('PLN', initial={'order': self.object})
98         return ctx
99
100     def form_valid(self, form):
101         funding = form.save()
102         return redirect(funding.get_absolute_url())
103
104
105 class OfferListView(ListView):
106     queryset = Offer.public()
107
108     def get_context_data(self, *args, **kwargs):
109         ctx = super(OfferListView, self).get_context_data(*args, **kwargs)
110         ctx['funding_no_show_current'] = True
111         return ctx
112
113
114 class FundingView(DetailView):
115     model = Funding
116
117     @method_decorator(never_cache)
118     def dispatch(self, *args, **kwargs):
119         return super(FundingView, self).dispatch(*args, **kwargs)
120
121     def get_context_data(self, *args, **kwargs):
122         ctx = super(FundingView, self).get_context_data(*args, **kwargs)
123         if self.object.offer.is_current():
124             ctx['funding_no_show_current'] = True
125             ctx['payment_form'] = PaymentMethodForm('PLN', initial={'order': self.object})
126         return ctx
127