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'
56 def get_context_data(self, **kwargs):
57 c = super().get_context_data(**kwargs)
58 c['club'] = models.Club.objects.first()
62 class JoinView(CreateView):
65 return models.Club.objects.first()
69 return self.request.EXPERIMENTS['layout'].value
71 def get_template_names(self):
73 return 'club/2022/donation_step1.html'
74 return 'club/membership_form.html'
76 def get_form_class(self):
78 return forms.DonationStep1Form
82 return self.request.GET.get('app')
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())
89 request.session['from_app'] = True
90 elif request.session and 'from_app' in request.session:
91 del request.session['from_app']
93 return super().get(request)
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()
101 c['ambassador'] = models.Ambassador.objects.all().order_by('?').first()
104 def get_initial(self):
105 if self.request.user.is_authenticated and self.request.user.email:
107 'email': self.request.user.email
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', '')
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)
123 def get_success_url(self):
125 return reverse('donation_step2', args=[self.object.key])
126 return self.object.initiate_payment(self.request)
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'
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'
142 def get_context_data(self, *args, **kwargs):
143 ctx = super().get_context_data(*args, **kwargs)
144 ctx['active_menu_item'] = 'club'
147 def post(self, request, key):
148 schedule = self.get_object()
149 return HttpResponseRedirect(schedule.initiate_payment(request))
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)
157 return HttpResponseRedirect(schedule.get_absolute_url())
160 def cancel(request, key):
161 schedule = models.Schedule.objects.get(key=key)
162 schedule.is_cancelled = True
164 return HttpResponseRedirect(schedule.get_absolute_url())
167 class DummyPaymentView(TemplateView):
168 template_name = 'club/dummy_payment.html'
170 def get_context_data(self, key):
172 'schedule': models.Schedule.objects.get(key=key),
175 def post(self, request, key):
176 schedule = models.Schedule.objects.get(key=key)
177 schedule.create_payment()
179 return HttpResponseRedirect(schedule.get_absolute_url())
182 class PayUPayment(DetailView):
183 model = models.Schedule
184 slug_field = slug_url_kwarg = 'key'
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))
194 class PayURecPayment(payu_views.RecPayment):
195 form_class = PayUCardTokenForm
197 def get_schedule(self):
198 return get_object_or_404(models.Schedule, key=self.kwargs['key'])
201 pos_id = recurring_payment_method.pos_id
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)
211 class PayUNotifyView(payu_views.NotifyView):
212 order_model = models.PayUOrder
215 class ScheduleThanksView(DetailView):
216 model = models.Schedule
217 slug_field = slug_url_kwarg = 'key'
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'
225 def get_context_data(self, *args, **kwargs):
226 ctx = super().get_context_data(*args, **kwargs)
227 ctx['active_menu_item'] = 'club'
231 class YearSummaryView(DetailView):
232 model = models.Schedule
233 slug_field = slug_url_kwarg = 'key'
234 template_name = 'club/year_summary.html'
236 def get_context_data(self, *args, **kwargs):
237 ctx = super().get_context_data(*args, **kwargs)
238 ctx['payments'] = models.PayUOrder.objects.filter(
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']
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']
253 email = email.strip()
255 schedules = models.Schedule.objects.filter(email=email).exclude(payed_at=None)
256 if schedules.exists():
265 unames.add(s.membership.user.username)
266 if exp is None or s.expires_at > exp:
268 row.append(','.join(sorted(unames)))
269 row.append('tak' if akt else 'nie')
270 row.append(exp.date().isoformat())
273 rows.append(';'.join(row))
274 rows = '\n'.join(rows)
280 'club/member_verify.html',