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
11 for stream, read_date, tag in streams:
12 iterstream = iter(stream)
14 item = next(iterstream)
18 substreams.append([read_date(item), item, iterstream, read_date, tag])
20 i, substream = max(enumerate(substreams), key=lambda x: x[0])
21 yield substream[4], substream[1]
23 item = next(substream[2])
27 substream[0:2] = [substream[3](item), item]
30 class WLFundView(TemplateView):
31 template_name = "funding/wlfund.html"
33 def get_context_data(self):
34 def add_total(total, it):
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())
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'),
56 class OfferDetailView(FormView):
57 form_class = DummyForm
58 template_name = "funding/offer_detail.html"
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)
64 def get_form(self, form_class):
65 if self.request.method == 'POST':
66 return form_class(self.object, self.request.POST)
68 return form_class(self.object)
70 def get_context_data(self, *args, **kwargs):
71 ctx = super(OfferDetailView, self).get_context_data(*args, **kwargs)
72 ctx['object'] = self.object
75 def form_valid(self, form):
77 return redirect(reverse("funding_thanks"))
80 class ThanksView(TemplateView):
81 template_name = "funding/thanks.html"
83 def get_context_data(self, *args, **kwargs):
84 ctx = super(ThanksView, self).get_context_data(*args, **kwargs)
85 ctx['object'] = Offer.current()