Basic crowdfunding.
[wolnelektury.git] / apps / funding / views.py
1 # Create your views here.
2 from django.views.generic import TemplateView
3 from .models import Offer
4
5
6 def mix(*streams):
7     substreams = []
8     for stream, read_date in streams:
9         iterstream = iter(stream)
10         try:
11             item = next(iterstream)
12         except StopIteration:
13             pass
14         else:
15             substreams.append([read_date(item), item, iterstream, read_date])
16     while substreams:
17         i, substream = max(enumerate(substreams), key=lambda x: x[0])
18         yield substream[1]
19         try:
20             item = next(substream[2])
21         except StopIteration:
22             del substreams[i]
23         else:
24             substream[0:2] = [substream[3](item), item]
25
26
27 class WLFundView(TemplateView):
28     template_name = "funding/wlfund.html"
29
30     def get_context_data(self):
31         ctx = super(WLFundView, self).get_context_data()
32         offers = [o for o in Offer.objects.all() if o.state() == 'lose' and o.sum()]
33         amount = sum(o.sum() for o in offers)
34         print offers
35
36         #offers = (o for o in Offer.objects.all() if o.state() == 'lose' and o.sum())
37         ctx['amount'] = amount
38         ctx['log'] = mix((offers, lambda x: x.end))
39         return ctx