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 if request.user.is_staff:
81 offers = Offer.objects.all()
83 offers = Offer.public()
84 self.object = get_object_or_404(offers, slug=slug)
86 self.object = Offer.current()
87 if self.object is None:
89 return super(OfferDetailView, self).dispatch(request, slug)
91 def get_form(self, form_class=None):
92 if form_class is None:
93 form_class = self.get_form_class()
94 if self.request.method == 'POST':
95 return form_class(self.object, self.request.POST)
97 return form_class(self.object, initial={'amount': app_settings.DEFAULT_AMOUNT})
99 def get_context_data(self, **kwargs):
100 ctx = super(OfferDetailView, self).get_context_data(**kwargs)
101 ctx['object'] = self.object
102 if self.object.is_current():
103 ctx['funding_no_show_current'] = True
106 def form_valid(self, form):
107 funding = form.save()
108 # Skip getpaid.forms.PaymentMethodForm, go directly to the broker.
109 payment = Payment.create(funding, self.backend)
110 gateway_url_tuple = payment.get_processor()(payment).get_gateway_url(self.request)
111 payment.change_status('in_progress')
112 return redirect(gateway_url_tuple[0])
115 class CurrentView(OfferDetailView):
117 def dispatch(self, request, slug=None):
118 self.object = Offer.current()
119 if self.object is None:
120 return redirect(reverse('funding'))
121 elif slug != self.object.slug:
122 return redirect(reverse('funding_current', args=[self.object.slug]))
123 return super(CurrentView, self).dispatch(request, slug)
126 class OfferListView(ListView):
127 queryset = Offer.public()
129 def get_context_data(self, **kwargs):
130 ctx = super(OfferListView, self).get_context_data(**kwargs)
131 ctx['funding_no_show_current'] = True
135 class ThanksView(TemplateView):
136 template_name = "funding/thanks.html"
138 def get_context_data(self, **kwargs):
139 ctx = super(ThanksView, self).get_context_data(**kwargs)
140 ctx['offer'] = Offer.current()
141 ctx['funding_no_show_current'] = True
145 class NoThanksView(TemplateView):
146 template_name = "funding/no_thanks.html"
149 class DisableNotifications(TemplateView):
150 template_name = "funding/disable_notifications.html"
153 def dispatch(self, request):
154 self.object = get_object_or_404(Funding, email=request.GET.get('email'), notify_key=request.GET.get('key'))
155 return super(DisableNotifications, self).dispatch(request)
157 def post(self, *args, **kwargs):
158 self.object.disable_notifications()
159 return redirect(self.request.get_full_path())