Move excessive funds to WLF
[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.conf import settings
6 from django.core.urlresolvers import reverse
7 from django.shortcuts import redirect, get_object_or_404
8 from django.views.generic import TemplateView, FormView, DetailView
9 from .forms import DummyForm
10 from .models import Offer, Spent
11
12
13 def mix(*streams):
14     substreams = []
15     for stream, read_date, tag in streams:
16         iterstream = iter(stream)
17         try:
18             item = next(iterstream)
19         except StopIteration:
20             pass
21         else:
22             substreams.append([read_date(item), item, iterstream, read_date, tag])
23     while substreams:
24         i, substream = max(enumerate(substreams), key=lambda x: x[1][0])
25         yield substream[4], substream[1]
26         try:
27             item = next(substream[2])
28         except StopIteration:
29             del substreams[i]
30         else:
31             substream[0:2] = [substream[3](item), item]
32
33
34 class WLFundView(TemplateView):
35     template_name = "funding/wlfund.html"
36
37     def get_context_data(self):
38         def add_total(total, it):
39             for tag, e in it:
40                 e.total = total
41                 if tag == 'spent':
42                     total += e.amount
43                 else:
44                     total -= e.wlfund
45                 yield tag, e
46
47         ctx = super(WLFundView, self).get_context_data()
48         offers = []
49         for o in Offer.objects.all():
50             if o.state() == 'lose':
51                 o.wlfund = o.sum()
52                 if o.wlfund > 0:
53                     offers.append(o)
54             elif o.state() == 'win':
55                 o.wlfund = o.sum() - o.target
56                 if o.wlfund > 0:
57                     offers.append(o)
58         amount = sum(o.wlfund for o in offers) - sum(o.amount for o in Spent.objects.all())
59         print offers
60
61         ctx['amount'] = amount
62         ctx['log'] = add_total(amount, mix(
63             (offers, lambda x: x.end, 'offer'),
64             (Spent.objects.all(), lambda x: x.timestamp, 'spent'),
65         ))
66         return ctx
67
68
69 class OfferDetailView(FormView):
70     form_class = DummyForm
71     template_name = "funding/offer_detail.html"
72
73     def dispatch(self, request, slug):
74         self.object = get_object_or_404(Offer.public(), slug=slug)
75         return super(OfferDetailView, self).dispatch(request, slug)
76
77     def get_form(self, form_class):
78         if self.request.method == 'POST':
79             return form_class(self.object, self.request.POST)
80         else:
81             return form_class(self.object, initial={'amount': settings.FUNDING_DEFAULT})
82
83     def get_context_data(self, *args, **kwargs):
84         ctx = super(OfferDetailView, self).get_context_data(*args, **kwargs)
85         ctx['object'] = self.object
86         return ctx
87
88     def form_valid(self, form):
89         form.save()
90         return redirect(reverse("funding_thanks"))
91
92
93 class ThanksView(TemplateView):
94     template_name = "funding/thanks.html"
95
96     def get_context_data(self, *args, **kwargs):
97         ctx = super(ThanksView, self).get_context_data(*args, **kwargs)
98         ctx['object'] = Offer.current()
99         return ctx