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