e80fcf977620c0eb8c1acae85008b6894acdb186
[wolnelektury.git] / src / club / views.py
1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
4 from django.conf import settings
5 from django.contrib.auth.decorators import login_required, permission_required
6 from django.db.models import Sum
7 from django.http import HttpResponseRedirect
8 from django.shortcuts import get_object_or_404, render
9 from django.urls import reverse
10 from django.utils.decorators import method_decorator
11 from django.views.decorators.cache import never_cache
12 from django.views.generic import FormView, CreateView, TemplateView, DetailView, UpdateView
13 from django.views import View
14 from .payu import POSS
15 from .payu import views as payu_views
16 from .forms import ScheduleForm, PayUCardTokenForm
17 from . import forms
18 from . import models
19 from .helpers import get_active_schedule
20 from .payment_methods import recurring_payment_method
21
22
23 class ClubView(TemplateView):
24     template_name = 'club/index.html'
25
26     def get_context_data(self, *args, **kwargs):
27         ctx = super().get_context_data(*args, **kwargs)
28         ctx['active_menu_item'] = 'club'
29         return ctx
30
31
32
33 class DonationStep1(UpdateView):
34     queryset = models.Schedule.objects.filter(payed_at=None)
35     form_class = forms.DonationStep1Form
36     slug_field = slug_url_kwarg = 'key'
37     template_name = 'club/2022/donation_step1.html'
38     step = 1
39
40     def get_context_data(self, **kwargs):
41         c = super().get_context_data(**kwargs)
42         c['club'] = models.Club.objects.first()
43         return c
44
45     def get_success_url(self):
46         return reverse('donation_step2', args=[self.object.key])
47
48
49 class DonationStep2(UpdateView):
50     queryset = models.Schedule.objects.filter(payed_at=None)
51     form_class = forms.DonationStep2Form
52     slug_field = slug_url_kwarg = 'key'
53     template_name = 'club/2022/donation_step2.html'
54     step = 2
55
56     def get_context_data(self, **kwargs):
57         c = super().get_context_data(**kwargs)
58         c['club'] = models.Club.objects.first()
59         return c
60
61
62 class JoinView(CreateView):
63     @property
64     def club(self):
65         return models.Club.objects.first()
66
67     @property
68     def new_layout(self):
69         return self.request.EXPERIMENTS['layout'].value
70     
71     def get_template_names(self):
72         if self.new_layout:
73             return 'club/2022/donation_step1.html'
74         return 'club/membership_form.html'
75
76     def get_form_class(self):
77         if self.new_layout:
78             return forms.DonationStep1Form
79         return ScheduleForm
80     
81     def is_app(self):
82         return self.request.GET.get('app')
83
84     def get(self, request):
85         if settings.CLUB_APP_HOST and self.is_app() and request.META['HTTP_HOST'] != settings.CLUB_APP_HOST:
86             return HttpResponseRedirect("https://" + settings.CLUB_APP_HOST + request.get_full_path())
87
88         if self.is_app():
89             request.session['from_app'] = True
90         elif request.session and 'from_app' in request.session:
91             del request.session['from_app']
92
93         return super().get(request)
94
95     def get_context_data(self, **kwargs):
96         c = super(JoinView, self).get_context_data(**kwargs)
97         c['membership'] = getattr(self.request.user, 'membership', None)
98         c['active_menu_item'] = 'club'
99         c['club'] = models.Club.objects.first()
100
101         c['ambassador'] = models.Ambassador.objects.all().order_by('?').first()
102         return c
103
104     def get_initial(self):
105         if self.request.user.is_authenticated and self.request.user.email:
106             return {
107                 'email': self.request.user.email
108             }
109
110     def get_form_kwargs(self):
111         kwargs = super().get_form_kwargs()
112         if not self.new_layout:
113             kwargs['referer'] = self.request.META.get('HTTP_REFERER', '')
114         return kwargs
115
116     def form_valid(self, form):
117         retval = super(JoinView, self).form_valid(form)
118         if self.request.user.is_authenticated:
119             form.instance.membership, created = models.Membership.objects.get_or_create(user=self.request.user)
120             form.instance.save()
121         return retval
122
123     def get_success_url(self):
124         if self.new_layout:
125             return reverse('donation_step2', args=[self.object.key])
126         return self.object.initiate_payment(self.request)
127
128
129 @method_decorator(never_cache, name='dispatch')
130 class ScheduleView(DetailView):
131     queryset = models.Schedule.objects.exclude(email='')
132     slug_field = slug_url_kwarg = 'key'
133     template_name = 'club/schedule.html'
134     step = 3
135     
136     def get_template_names(self):
137         if self.request.EXPERIMENTS['layout'].value:
138             if not self.object.payed_at:
139                 return 'club/2022/donation_step3.html'
140         return 'club/schedule.html'
141         
142     def get_context_data(self, *args, **kwargs):
143         ctx = super().get_context_data(*args, **kwargs)
144         ctx['active_menu_item'] = 'club'
145         return ctx
146
147     def post(self, request, key):
148         schedule = self.get_object()
149         return HttpResponseRedirect(schedule.initiate_payment(request))
150
151
152 @login_required
153 def claim(request, key):
154     schedule = models.Schedule.objects.get(key=key, membership=None)
155     schedule.membership, created = models.Membership.objects.get_or_create(user=request.user)
156     schedule.save()
157     return HttpResponseRedirect(schedule.get_absolute_url())
158
159
160 def cancel(request, key):
161     schedule = models.Schedule.objects.get(key=key)
162     schedule.is_cancelled = True
163     schedule.save()
164     return HttpResponseRedirect(schedule.get_absolute_url())
165
166
167 class DummyPaymentView(TemplateView):
168     template_name = 'club/dummy_payment.html'
169
170     def get_context_data(self, key):
171         return {
172             'schedule': models.Schedule.objects.get(key=key),
173         }
174
175     def post(self, request, key):
176         schedule = models.Schedule.objects.get(key=key)
177         schedule.create_payment()
178
179         return HttpResponseRedirect(schedule.get_absolute_url())
180
181
182 class PayUPayment(DetailView):
183     model = models.Schedule
184     slug_field = slug_url_kwarg = 'key'
185
186     def get(self, request, key):
187         schedule = self.get_object()
188         schedule.method = 'payu'
189         schedule.save(update_fields=['method'])
190         return HttpResponseRedirect(schedule.initiate_payment(request))
191
192
193
194 class PayURecPayment(payu_views.RecPayment):
195     form_class = PayUCardTokenForm
196
197     def get_schedule(self):
198         return get_object_or_404(models.Schedule, key=self.kwargs['key'])
199
200     def get_pos(self):
201         pos_id = recurring_payment_method.pos_id
202         return POSS[pos_id]
203
204     def get_success_url(self):
205         schedule = self.get_schedule()
206         schedule.method = 'payu-re'
207         schedule.save(update_fields=['method'])
208         return schedule.pay(self.request)
209
210
211 class PayUNotifyView(payu_views.NotifyView):
212     order_model = models.PayUOrder
213
214
215 class ScheduleThanksView(DetailView):
216     model = models.Schedule
217     slug_field = slug_url_kwarg = 'key'
218     step = 4
219
220     def get_template_names(self):
221         if self.request.EXPERIMENTS['layout'].value:
222             return 'club/2022/donation_step4.html'
223         return 'club/thanks.html'
224     
225     def get_context_data(self, *args, **kwargs):
226         ctx = super().get_context_data(*args, **kwargs)
227         ctx['active_menu_item'] = 'club'
228         return ctx
229
230
231 class YearSummaryView(DetailView):
232     model = models.Schedule
233     slug_field = slug_url_kwarg = 'key'
234     template_name = 'club/year_summary.html'
235
236     def get_context_data(self, *args, **kwargs):
237         ctx = super().get_context_data(*args, **kwargs)
238         ctx['payments'] = models.PayUOrder.objects.filter(
239             status='COMPLETED',
240             completed_at__year=self.kwargs['year'],
241             schedule__email=self.object.email,
242         ).order_by('completed_at')
243         ctx['total_amount'] = ctx['payments'].aggregate(s=Sum('schedule__amount'))['s']
244         return ctx
245
246
247 @permission_required('club.schedule_view')
248 def member_verify(request):
249     if request.method == 'POST':
250         emails = request.POST.get('emails').strip().split('\n')
251         rows = ['email;członek;nazwa użytkownika;aktywny;co najmniej do']
252         for email in emails:
253             email = email.strip()
254             row = [email]
255             schedules = models.Schedule.objects.filter(email=email).exclude(payed_at=None)
256             if schedules.exists():
257                 row.append('tak')
258                 akt = False
259                 unames = set()
260                 exp = None
261                 for s in schedules:
262                     if s.is_active():
263                         akt = True
264                     if s.membership:
265                         unames.add(s.membership.user.username) 
266                     if exp is None or s.expires_at > exp:
267                         exp = s.expires_at
268                 row.append(','.join(sorted(unames)))
269                 row.append('tak' if akt else 'nie')
270                 row.append(exp.date().isoformat())
271             else:
272                 row.append('nie')
273             rows.append(';'.join(row))
274         rows = '\n'.join(rows)
275     else:
276         rows = ''
277
278     return render(
279         request,
280         'club/member_verify.html',
281         {
282             'result': rows
283         }
284     )