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, HttpResponseRedirect
5 from django.shortcuts import get_object_or_404, redirect, render
6 from django.urls import reverse
7 from django.contrib.auth.decorators import login_required
8 from django.views.decorators.csrf import csrf_exempt
9 from django.views.generic import TemplateView, FormView, ListView
10 import club.payu.views
11 from . import app_settings
12 from .forms import FundingForm
13 from .models import Offer, Spent, Funding
18 for stream, read_date, tag in streams:
19 iterstream = iter(stream)
21 item = next(iterstream)
25 substreams.append([read_date(item), item, iterstream, read_date, tag])
27 i, substream = max(enumerate(substreams), key=lambda x: x[1][0])
28 yield substream[4], substream[1]
30 item = next(substream[2])
34 substream[0:2] = [substream[3](item), item]
37 class WLFundView(TemplateView):
38 template_name = "funding/wlfund.html"
40 def get_context_data(self):
41 def add_total(total, it):
50 ctx = super(WLFundView, self).get_context_data()
53 for o in Offer.past():
55 o.wlfund = o.sum() - o.target
62 amount = sum(o.wlfund for o in offers) - sum(o.amount for o in Spent.objects.all())
64 ctx['amount'] = amount
65 ctx['log'] = add_total(amount, mix(
66 (offers, lambda x: x.end, 'offer'),
67 (Spent.objects.all().select_related(), lambda x: x.timestamp, 'spent'),
72 class OfferDetailView(FormView):
73 form_class = FundingForm
75 def get_template_names(self):
76 if self.request.EXPERIMENTS['layout'].value:
77 return 'funding/2022/offer_detail.html'
79 return "funding/offer_detail.html"
82 def dispatch(self, request, slug=None):
83 if getattr(self, 'object', None) is None:
85 if request.user.is_staff:
86 offers = Offer.objects.all()
88 offers = Offer.public()
89 self.object = get_object_or_404(offers, slug=slug)
91 self.object = Offer.current()
92 if self.object is None:
94 return super(OfferDetailView, self).dispatch(request, slug)
96 def get_form(self, form_class=None):
97 if form_class is None:
98 form_class = self.get_form_class()
99 if self.request.method == 'POST':
100 return form_class(self.request, self.object, self.request.POST)
102 return form_class(self.request, self.object, initial={'amount': app_settings.DEFAULT_AMOUNT})
104 def get_context_data(self, **kwargs):
105 ctx = super(OfferDetailView, self).get_context_data(**kwargs)
106 ctx['object'] = self.object
107 if self.object.is_current():
108 ctx['funding_no_show_current'] = True
111 def form_valid(self, form):
112 funding = form.save()
113 return redirect(funding.put())
116 class CurrentView(OfferDetailView):
118 def dispatch(self, request, slug=None):
119 self.object = Offer.current()
120 if self.object is None:
121 return redirect(reverse('funding'))
122 elif slug != self.object.slug:
123 return redirect(reverse('funding_current', args=[self.object.slug]))
124 return super(CurrentView, self).dispatch(request, slug)
127 class OfferListView(ListView):
128 queryset = Offer.public()
130 def get_context_data(self, **kwargs):
131 ctx = super(OfferListView, self).get_context_data(**kwargs)
132 ctx['funding_no_show_current'] = True
136 class ThanksView(TemplateView):
137 template_name = "funding/thanks.html"
139 def get_context_data(self, **kwargs):
140 ctx = super(ThanksView, self).get_context_data(**kwargs)
141 ctx['offer'] = Offer.current()
142 ctx['funding_no_show_current'] = True
146 class NoThanksView(TemplateView):
147 template_name = "funding/no_thanks.html"
150 class DisableNotifications(TemplateView):
151 template_name = "funding/disable_notifications.html"
154 def dispatch(self, request):
155 self.object = get_object_or_404(Funding, email=request.GET.get('email'), notify_key=request.GET.get('key'))
156 return super(DisableNotifications, self).dispatch(request)
158 def post(self, *args, **kwargs):
159 self.object.disable_notifications()
160 return redirect(self.request.get_full_path())
164 def claim(request, key):
165 funding = get_object_or_404(Funding, notify_key=key)
166 if funding.user is None:
167 funding.user = request.user
169 return HttpResponseRedirect(
170 funding.offer.book.get_absolute_url() if funding.offer.book is not None
171 else funding.offer.get_absolute_url()
175 class PayUNotifyView(club.payu.views.NotifyView):
176 order_model = Funding