1 # This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. 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():
57 amount = sum(o.wlfund for o in offers) - sum(o.amount for o in Spent.objects.all())
59 ctx['amount'] = amount
60 ctx['log'] = add_total(amount, mix(
61 (Spent.objects.all().select_related(), lambda x: x.timestamp, 'spent'),
62 (offers, lambda x: x.end, 'offer'),
67 class OfferDetailView(FormView):
68 form_class = FundingForm
69 template_name = 'funding/offer_detail.html'
72 def dispatch(self, request, slug=None):
73 if getattr(self, 'object', None) is None:
75 if request.user.is_staff:
76 offers = Offer.objects.all()
78 offers = Offer.public()
79 self.object = get_object_or_404(offers, 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=None):
87 if form_class is None:
88 form_class = self.get_form_class()
89 if self.request.method == 'POST':
90 return form_class(self.request, self.object, self.request.POST)
92 return form_class(self.request, self.object, initial={'amount': app_settings.DEFAULT_AMOUNT})
94 def get_context_data(self, **kwargs):
95 ctx = super(OfferDetailView, self).get_context_data(**kwargs)
96 ctx['object'] = self.object
97 if self.object.is_current():
98 ctx['funding_no_show_current'] = True
101 def form_valid(self, form):
102 funding = form.save()
103 return redirect(funding.put())
106 class CurrentView(OfferDetailView):
108 def dispatch(self, request, slug=None):
109 self.object = Offer.current()
110 if self.object is None:
111 return redirect(reverse('funding'))
112 elif slug != self.object.slug:
113 return redirect(reverse('funding_current', args=[self.object.slug]))
114 return super(CurrentView, self).dispatch(request, slug)
117 class OfferListView(ListView):
118 queryset = Offer.public()
119 template_name = 'funding/offer_list.html'
121 def get_context_data(self, **kwargs):
122 ctx = super(OfferListView, self).get_context_data(**kwargs)
123 ctx['funding_no_show_current'] = True
127 class ThanksView(TemplateView):
128 template_name = "funding/thanks.html"
130 def get_context_data(self, **kwargs):
131 ctx = super(ThanksView, self).get_context_data(**kwargs)
132 ctx['offer'] = Offer.current()
133 ctx['funding_no_show_current'] = True
137 class NoThanksView(TemplateView):
138 template_name = "funding/no_thanks.html"
141 class DisableNotifications(TemplateView):
142 template_name = "funding/disable_notifications.html"
145 def dispatch(self, request):
146 self.object = get_object_or_404(Funding, email=request.GET.get('email'), notify_key=request.GET.get('key'))
147 return super(DisableNotifications, self).dispatch(request)
149 def post(self, *args, **kwargs):
150 self.object.disable_notifications()
151 return redirect(self.request.get_full_path())
155 def claim(request, key):
156 funding = get_object_or_404(Funding, notify_key=key)
157 if funding.user is None:
158 funding.user = request.user
160 return HttpResponseRedirect(
161 funding.offer.book.get_absolute_url() if funding.offer.book is not None
162 else funding.offer.get_absolute_url()
166 class PayUNotifyView(club.payu.views.NotifyView):
167 order_model = Funding