1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 from django.http import Http404
5 from django.shortcuts import get_object_or_404, redirect, render
6 from django.urls import reverse
7 from django.views.decorators.csrf import csrf_exempt
8 from django.views.generic import TemplateView, FormView, ListView
9 from getpaid.models import Payment
10 from . import app_settings
11 from .forms import FundingForm
12 from .models import Offer, Spent, Funding
17 for stream, read_date, tag in streams:
18 iterstream = iter(stream)
20 item = next(iterstream)
24 substreams.append([read_date(item), item, iterstream, read_date, tag])
26 i, substream = max(enumerate(substreams), key=lambda x: x[1][0])
27 yield substream[4], substream[1]
29 item = next(substream[2])
33 substream[0:2] = [substream[3](item), item]
36 class WLFundView(TemplateView):
37 template_name = "funding/wlfund.html"
39 def get_context_data(self):
40 def add_total(total, it):
49 ctx = super(WLFundView, self).get_context_data()
52 for o in Offer.past():
54 o.wlfund = o.sum() - o.target
61 amount = sum(o.wlfund for o in offers) - sum(o.amount for o in Spent.objects.all())
63 ctx['amount'] = amount
64 ctx['log'] = add_total(amount, mix(
65 (offers, lambda x: x.end, 'offer'),
66 (Spent.objects.all().select_related(), lambda x: x.timestamp, 'spent'),
71 class OfferDetailView(FormView):
72 form_class = FundingForm
73 template_name = "funding/offer_detail.html"
74 backend = 'getpaid.backends.payu'
77 def dispatch(self, request, slug=None):
78 if getattr(self, 'object', None) is None:
80 self.object = get_object_or_404(Offer.public(), slug=slug)
82 self.object = Offer.current()
83 if self.object is None:
85 return super(OfferDetailView, self).dispatch(request, slug)
87 def get_form(self, form_class=None):
88 if form_class is None:
89 form_class = self.get_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': app_settings.DEFAULT_AMOUNT})
95 def get_context_data(self, **kwargs):
96 ctx = super(OfferDetailView, self).get_context_data(**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):
113 def dispatch(self, request, slug=None):
114 self.object = Offer.current()
115 if self.object is None:
116 return redirect(reverse('funding'))
117 elif slug != self.object.slug:
118 return redirect(reverse('funding_current', args=[self.object.slug]))
119 return super(CurrentView, self).dispatch(request, slug)
122 class OfferListView(ListView):
123 queryset = Offer.public()
125 def get_context_data(self, **kwargs):
126 ctx = super(OfferListView, self).get_context_data(**kwargs)
127 ctx['funding_no_show_current'] = True
131 class ThanksView(TemplateView):
132 template_name = "funding/thanks.html"
134 def get_context_data(self, **kwargs):
135 ctx = super(ThanksView, self).get_context_data(**kwargs)
136 ctx['offer'] = Offer.current()
137 ctx['funding_no_show_current'] = True
141 class NoThanksView(TemplateView):
142 template_name = "funding/no_thanks.html"
145 class DisableNotifications(TemplateView):
146 template_name = "funding/disable_notifications.html"
149 def dispatch(self, request):
150 self.object = get_object_or_404(Funding, email=request.GET.get('email'), notify_key=request.GET.get('key'))
151 return super(DisableNotifications, self).dispatch(request)
153 def post(self, *args, **kwargs):
154 self.object.disable_notifications()
155 return redirect(self.request.get_full_path())