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