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 datetime import date
6 from django.views.decorators.cache import never_cache
7 from django.conf import settings
8 from django.core.urlresolvers import reverse
9 from django.http import Http404
10 from django.shortcuts import redirect, get_object_or_404
11 from django.views.generic import TemplateView, FormView, DetailView, ListView
12 import getpaid.backends.payu
13 from getpaid.models import Payment
14 from .forms import FundingForm
15 from .models import Offer, Spent, Funding
20 for stream, read_date, tag in streams:
21 iterstream = iter(stream)
23 item = next(iterstream)
27 substreams.append([read_date(item), item, iterstream, read_date, tag])
29 i, substream = max(enumerate(substreams), key=lambda x: x[1][0])
30 yield substream[4], substream[1]
32 item = next(substream[2])
36 substream[0:2] = [substream[3](item), item]
39 class WLFundView(TemplateView):
40 template_name = "funding/wlfund.html"
42 def get_context_data(self):
43 def add_total(total, it):
52 ctx = super(WLFundView, self).get_context_data()
55 for o in Offer.past():
57 o.wlfund = o.sum() - o.target
64 amount = sum(o.wlfund for o in offers) - sum(o.amount for o in Spent.objects.all())
66 ctx['amount'] = amount
67 ctx['log'] = add_total(amount, mix(
68 (offers, lambda x: x.end, 'offer'),
69 (Spent.objects.all().select_related(), lambda x: x.timestamp, 'spent'),
74 class OfferDetailView(FormView):
75 form_class = FundingForm
76 template_name = "funding/offer_detail.html"
77 backend = 'getpaid.backends.payu'
79 def dispatch(self, request, slug=None):
80 if getattr(self, 'object', None) is None:
82 self.object = get_object_or_404(Offer.public(), slug=slug)
84 self.object = Offer.current()
85 if self.object is None:
87 return super(OfferDetailView, self).dispatch(request, slug)
89 def get_form(self, form_class):
90 if self.request.method == 'POST':
91 return form_class(self.object, self.request.POST)
93 return form_class(self.object, initial={'amount': settings.FUNDING_DEFAULT})
95 def get_context_data(self, *args, **kwargs):
96 ctx = super(OfferDetailView, self).get_context_data(*args, **kwargs)
97 ctx['object'] = self.object
98 if self.object.is_current():
99 ctx['funding_no_show_current'] = True
102 def form_valid(self, form):
103 funding = form.save()
104 # Skip getpaid.forms.PaymentMethodForm, go directly to the broker.
105 payment = Payment.create(funding, self.backend)
106 gateway_url_tuple = payment.get_processor()(payment).get_gateway_url(self.request)
107 payment.change_status('in_progress')
108 return redirect(gateway_url_tuple[0])
111 class CurrentView(OfferDetailView):
112 def dispatch(self, request, slug=None):
113 self.object = Offer.current()
114 if self.object is None:
116 elif slug != self.object.slug:
117 return redirect(reverse('funding_current', args=[self.object.slug]))
118 return super(CurrentView, self).dispatch(request, slug)
121 class OfferListView(ListView):
122 queryset = Offer.public()
124 def get_context_data(self, *args, **kwargs):
125 ctx = super(OfferListView, self).get_context_data(*args, **kwargs)
126 ctx['funding_no_show_current'] = True
130 class ThanksView(TemplateView):
131 template_name = "funding/thanks.html"
133 def get_context_data(self, *args, **kwargs):
134 ctx = super(ThanksView, self).get_context_data(*args, **kwargs)
135 ctx['offer'] = Offer.current()
136 ctx['funding_no_show_current'] = True
139 class NoThanksView(TemplateView):
140 template_name = "funding/no_thanks.html"