X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/94a4fcc7ab9e1713dc2defc8b35c875fc90f6fa5..95db9a686ba5be5d07936dfc1178ccd7da3d2818:/src/club/views.py diff --git a/src/club/views.py b/src/club/views.py index 85faa85ad..3f283af15 100644 --- a/src/club/views.py +++ b/src/club/views.py @@ -1,10 +1,11 @@ -# This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later. -# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. +# This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later. +# Copyright © Fundacja Wolne Lektury. See NOTICE for more information. # +from datetime import date, timedelta from django.conf import settings from django.contrib.auth.decorators import login_required, permission_required from django.db.models import Sum -from django.http import HttpResponseRedirect +from django.http import HttpResponse, HttpResponseRedirect, JsonResponse from django.shortcuts import get_object_or_404, render from django.urls import reverse from django.utils.decorators import method_decorator @@ -30,11 +31,12 @@ class ClubView(TemplateView): +@method_decorator(never_cache, name='dispatch') class DonationStep1(UpdateView): queryset = models.Schedule.objects.filter(payed_at=None) form_class = forms.DonationStep1Form slug_field = slug_url_kwarg = 'key' - template_name = 'club/2022/donation_step1.html' + template_name = 'club/donation_step1.html' step = 1 def get_context_data(self, **kwargs): @@ -46,11 +48,12 @@ class DonationStep1(UpdateView): return reverse('donation_step2', args=[self.object.key]) +@method_decorator(never_cache, name='dispatch') class DonationStep2(UpdateView): queryset = models.Schedule.objects.filter(payed_at=None) form_class = forms.DonationStep2Form slug_field = slug_url_kwarg = 'key' - template_name = 'club/2022/donation_step2.html' + template_name = 'club/donation_step2.html' step = 2 def get_context_data(self, **kwargs): @@ -59,9 +62,20 @@ class DonationStep2(UpdateView): return c +def set_monthly(request, key): + schedule = get_object_or_404(models.Schedule, payed_at=None, key=key) + if request.POST: + schedule.monthly = request.POST.get('monthly') == 'true' + schedule.save(update_fields=['monthly']) + return JsonResponse({ + "amount": schedule.amount, + "monthly": schedule.monthly, + }) + + class JoinView(CreateView): form_class = forms.DonationStep1Form - template_name = 'club/2022/donation_step1.html' + template_name = 'club/donation_step1.html' @property def club(self): @@ -99,7 +113,7 @@ class JoinView(CreateView): def get_form_kwargs(self): kwargs = super().get_form_kwargs() - #kwargs['referer'] = self.request.META.get('HTTP_REFERER', '') + kwargs['referer'] = self.request.META.get('HTTP_REFERER', '') return kwargs def form_valid(self, form): @@ -122,7 +136,7 @@ class ScheduleView(DetailView): def get_template_names(self): if not self.object.payed_at: - return 'club/2022/donation_step3.html' + return 'club/donation_step3.html' return 'club/schedule.html' def get_context_data(self, *args, **kwargs): @@ -185,7 +199,7 @@ class PayUNotifyView(payu_views.NotifyView): class ScheduleThanksView(DetailView): model = models.Schedule - template_name = 'club/2022/donation_step4.html' + template_name = 'club/donation_step4.html' slug_field = slug_url_kwarg = 'key' step = 4 @@ -249,3 +263,60 @@ def member_verify(request): 'result': rows } ) + + +@permission_required('club.schedule_view') +def receipt(request): + email = request.POST.get('email') + try: + year = int(request.POST.get('year')) + except: + return HttpResponse('no content') + + receipt = models.PayUOrder.generate_receipt(email, year) + if receipt: + content, optout, payments = receipt + else: + return HttpResponse('no content') + return HttpResponse( + content, + headers={ + "Content-Type": "application/pdf", + "Content-Disposition": f'attachment; filename="wolnelektury-{year}-{email}.pdf"', + } + ) + + +@permission_required('club.schedule_view') +def stats(request): + maxes = {} + acq = {} + today = date.today() + start = today - timedelta(365) + for schedule in models.Schedule.objects.filter( + payed_at__gte=start, + ): + d = schedule.payed_at.date() + m = schedule.method.replace('-', '_') + acq.setdefault(d, {}) + acq[d].setdefault(m, 0) + acq[d][m] += schedule.amount + + for a in acq.values(): + for m, v in a.items(): + maxes.setdefault(m, 0) + if v > maxes[m]: + maxes[m] = v + + days = [] + d = today + while d >= start: + a = acq.get(d, {}) + for k, v in a.items(): + a[k] = (v, 100 * v/(maxes[k] or 1)) + days.append((d.isoformat(), a)) + d -= timedelta(1) + + return render(request, 'club/stats.html', + {'days': days}) +