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.
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
15 for stream, read_date, tag in streams:
16 iterstream = iter(stream)
18 item = next(iterstream)
22 substreams.append([read_date(item), item, iterstream, read_date, tag])
24 i, substream = max(enumerate(substreams), key=lambda x: x[0])
25 yield substream[4], substream[1]
27 item = next(substream[2])
31 substream[0:2] = [substream[3](item), item]
34 class WLFundView(TemplateView):
35 template_name = "funding/wlfund.html"
37 def get_context_data(self):
38 def add_total(total, it):
47 ctx = super(WLFundView, self).get_context_data()
48 offers = [o for o in Offer.objects.all() if o.state() == 'lose' and o.sum()]
49 amount = sum(o.sum() for o in offers) - sum(o.amount for o in Spent.objects.all())
52 ctx['amount'] = amount
53 ctx['log'] = add_total(amount, mix(
54 (offers, lambda x: x.end, 'offer'),
55 (Spent.objects.all(), lambda x: x.timestamp, 'spent'),
60 class OfferDetailView(FormView):
61 form_class = DummyForm
62 template_name = "funding/offer_detail.html"
64 def dispatch(self, request, slug):
65 self.object = get_object_or_404(Offer.public(), slug=slug)
66 return super(OfferDetailView, self).dispatch(request, slug)
68 def get_form(self, form_class):
69 if self.request.method == 'POST':
70 return form_class(self.object, self.request.POST)
72 return form_class(self.object, initial={'amount': settings.FUNDING_DEFAULT})
74 def get_context_data(self, *args, **kwargs):
75 ctx = super(OfferDetailView, self).get_context_data(*args, **kwargs)
76 ctx['object'] = self.object
79 def form_valid(self, form):
81 return redirect(reverse("funding_thanks"))
84 class ThanksView(TemplateView):
85 template_name = "funding/thanks.html"
87 def get_context_data(self, *args, **kwargs):
88 ctx = super(ThanksView, self).get_context_data(*args, **kwargs)
89 ctx['object'] = Offer.current()