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.views.decorators.cache import never_cache
6 from django.conf import settings
7 from django.core.urlresolvers import reverse
8 from django.http import Http404
9 from django.shortcuts import redirect, get_object_or_404
10 from django.utils.decorators import method_decorator
11 from django.views.generic import TemplateView, FormView, DetailView, ListView
12 from getpaid.forms import PaymentMethodForm
13 from .forms import DummyForm
14 from .models import Offer, Spent, Funding
19 for stream, read_date, tag in streams:
20 iterstream = iter(stream)
22 item = next(iterstream)
26 substreams.append([read_date(item), item, iterstream, read_date, tag])
28 i, substream = max(enumerate(substreams), key=lambda x: x[1][0])
29 yield substream[4], substream[1]
31 item = next(substream[2])
35 substream[0:2] = [substream[3](item), item]
38 class WLFundView(TemplateView):
39 template_name = "funding/wlfund.html"
41 def get_context_data(self):
42 def add_total(total, it):
51 ctx = super(WLFundView, self).get_context_data()
53 for o in Offer.objects.all():
54 if o.state() == 'lose':
58 elif o.state() == 'win':
59 o.wlfund = o.sum() - o.target
62 amount = sum(o.wlfund for o in offers) - sum(o.amount for o in Spent.objects.all())
65 ctx['amount'] = amount
66 ctx['log'] = add_total(amount, mix(
67 (offers, lambda x: x.end, 'offer'),
68 (Spent.objects.all(), lambda x: x.timestamp, 'spent'),
73 class OfferDetailView(FormView):
74 form_class = DummyForm
75 template_name = "funding/offer_detail.html"
77 def dispatch(self, request, slug=None):
79 self.object = get_object_or_404(Offer.public(), slug=slug)
81 self.object = Offer.current()
82 if self.object is None:
84 return super(OfferDetailView, self).dispatch(request, slug)
86 def get_form(self, form_class):
87 if self.request.method == 'POST':
88 return form_class(self.object, self.request.POST)
90 return form_class(self.object, initial={'amount': settings.FUNDING_DEFAULT})
92 def get_context_data(self, *args, **kwargs):
93 ctx = super(OfferDetailView, self).get_context_data(*args, **kwargs)
94 ctx['object'] = self.object
95 if self.object.is_current():
96 ctx['funding_no_show_current'] = True
97 ctx['payment_form'] = PaymentMethodForm('PLN', initial={'order': self.object})
100 def form_valid(self, form):
101 funding = form.save()
102 return redirect(funding.get_absolute_url())
105 class OfferListView(ListView):
106 queryset = Offer.public()
108 def get_context_data(self, *args, **kwargs):
109 ctx = super(OfferListView, self).get_context_data(*args, **kwargs)
110 ctx['funding_no_show_current'] = True
114 class FundingView(DetailView):
117 @method_decorator(never_cache)
118 def dispatch(self, *args, **kwargs):
119 return super(FundingView, self).dispatch(*args, **kwargs)
121 def get_context_data(self, *args, **kwargs):
122 ctx = super(FundingView, self).get_context_data(*args, **kwargs)
123 if self.object.offer.is_current():
124 ctx['funding_no_show_current'] = True
125 ctx['payment_form'] = PaymentMethodForm('PLN', initial={'order': self.object})