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.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
19 from .helpers import get_active_schedule
20 from .payment_methods import recurring_payment_method
23 class ClubView(TemplateView):
24 template_name = 'club/index.html'
26 def get_context_data(self, *args, **kwargs):
27 ctx = super().get_context_data(*args, **kwargs)
28 ctx['active_menu_item'] = 'club'
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'
40 def get_context_data(self, **kwargs):
41 c = super().get_context_data(**kwargs)
42 c['club'] = models.Club.objects.first()
45 def get_success_url(self):
46 return reverse('donation_step2', args=[self.object.key])
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'
57 class JoinView(CreateView):
60 return models.Club.objects.first()
64 return self.request.EXPERIMENTS['layout'].value
66 def get_template_names(self):
68 return 'club/2022/donation_step1.html'
69 return 'club/membership_form.html'
71 def get_form_class(self):
73 return forms.DonationStep1Form
77 return self.request.GET.get('app')
79 def get(self, request):
80 if settings.CLUB_APP_HOST and self.is_app() and request.META['HTTP_HOST'] != settings.CLUB_APP_HOST:
81 return HttpResponseRedirect("https://" + settings.CLUB_APP_HOST + request.get_full_path())
84 request.session['from_app'] = True
85 elif request.session and 'from_app' in request.session:
86 del request.session['from_app']
88 return super().get(request)
90 def get_context_data(self, **kwargs):
91 c = super(JoinView, self).get_context_data(**kwargs)
92 c['membership'] = getattr(self.request.user, 'membership', None)
93 c['active_menu_item'] = 'club'
94 c['club'] = models.Club.objects.first()
96 c['ambassador'] = models.Ambassador.objects.all().order_by('?').first()
99 def get_initial(self):
100 if self.request.user.is_authenticated and self.request.user.email:
102 'email': self.request.user.email
105 def get_form_kwargs(self):
106 kwargs = super().get_form_kwargs()
107 if not self.new_layout:
108 kwargs['referer'] = self.request.META.get('HTTP_REFERER', '')
111 def form_valid(self, form):
112 retval = super(JoinView, self).form_valid(form)
113 if self.request.user.is_authenticated:
114 form.instance.membership, created = models.Membership.objects.get_or_create(user=self.request.user)
118 def get_success_url(self):
120 return reverse('donation_step2', args=[self.object.key])
121 return self.object.initiate_payment(self.request)
124 @method_decorator(never_cache, name='dispatch')
125 class ScheduleView(DetailView):
126 queryset = models.Schedule.objects.exclude(email='')
127 slug_field = slug_url_kwarg = 'key'
128 template_name = 'club/schedule.html'
131 def get_template_names(self):
132 if self.request.EXPERIMENTS['layout'].value:
133 if not self.object.payed_at:
134 return 'club/2022/donation_step3.html'
135 return 'club/schedule.html'
137 def get_context_data(self, *args, **kwargs):
138 ctx = super().get_context_data(*args, **kwargs)
139 ctx['active_menu_item'] = 'club'
142 def post(self, request, key):
143 schedule = self.get_object()
144 return HttpResponseRedirect(schedule.initiate_payment(request))
148 def claim(request, key):
149 schedule = models.Schedule.objects.get(key=key, membership=None)
150 schedule.membership, created = models.Membership.objects.get_or_create(user=request.user)
152 return HttpResponseRedirect(schedule.get_absolute_url())
155 def cancel(request, key):
156 schedule = models.Schedule.objects.get(key=key)
157 schedule.is_cancelled = True
159 return HttpResponseRedirect(schedule.get_absolute_url())
162 class DummyPaymentView(TemplateView):
163 template_name = 'club/dummy_payment.html'
165 def get_context_data(self, key):
167 'schedule': models.Schedule.objects.get(key=key),
170 def post(self, request, key):
171 schedule = models.Schedule.objects.get(key=key)
172 schedule.create_payment()
174 return HttpResponseRedirect(schedule.get_absolute_url())
177 class PayUPayment(DetailView):
178 model = models.Schedule
179 slug_field = slug_url_kwarg = 'key'
181 def get(self, request, key):
182 schedule = self.get_object()
183 schedule.method = 'payu'
184 schedule.save(update_fields=['method'])
185 return HttpResponseRedirect(schedule.initiate_payment(request))
189 class PayURecPayment(payu_views.RecPayment):
190 form_class = PayUCardTokenForm
192 def get_schedule(self):
193 return get_object_or_404(models.Schedule, key=self.kwargs['key'])
196 pos_id = recurring_payment_method.pos_id
199 def get_success_url(self):
200 schedule = self.get_schedule()
201 schedule.method = 'payu-re'
202 schedule.save(update_fields=['method'])
203 return schedule.pay(self.request)
206 class PayUNotifyView(payu_views.NotifyView):
207 order_model = models.PayUOrder
210 class ScheduleThanksView(DetailView):
211 model = models.Schedule
212 slug_field = slug_url_kwarg = 'key'
215 def get_template_names(self):
216 if self.request.EXPERIMENTS['layout'].value:
217 return 'club/2022/donation_step4.html'
218 return 'club/thanks.html'
220 def get_context_data(self, *args, **kwargs):
221 ctx = super().get_context_data(*args, **kwargs)
222 ctx['active_menu_item'] = 'club'
226 class YearSummaryView(DetailView):
227 model = models.Schedule
228 slug_field = slug_url_kwarg = 'key'
229 template_name = 'club/year_summary.html'
231 def get_context_data(self, *args, **kwargs):
232 ctx = super().get_context_data(*args, **kwargs)
233 ctx['payments'] = models.PayUOrder.objects.filter(
235 completed_at__year=self.kwargs['year'],
236 schedule__email=self.object.email,
237 ).order_by('completed_at')
238 ctx['total_amount'] = ctx['payments'].aggregate(s=Sum('schedule__amount'))['s']
242 @permission_required('club.schedule_view')
243 def member_verify(request):
244 if request.method == 'POST':
245 emails = request.POST.get('emails').strip().split('\n')
246 rows = ['email;członek;nazwa użytkownika;aktywny;co najmniej do']
248 email = email.strip()
250 schedules = models.Schedule.objects.filter(email=email).exclude(payed_at=None)
251 if schedules.exists():
260 unames.add(s.membership.user.username)
261 if exp is None or s.expires_at > exp:
263 row.append(','.join(sorted(unames)))
264 row.append('tak' if akt else 'nie')
265 row.append(exp.date().isoformat())
268 rows.append(';'.join(row))
269 rows = '\n'.join(rows)
275 'club/member_verify.html',