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.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 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/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/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):
63 form_class = forms.DonationStep1Form
64 template_name = 'club/donation_step1.html'
68 return models.Club.objects.first()
71 return self.request.GET.get('app')
73 def get(self, request):
74 if settings.CLUB_APP_HOST and self.is_app() and request.META['HTTP_HOST'] != settings.CLUB_APP_HOST:
75 return HttpResponseRedirect("https://" + settings.CLUB_APP_HOST + request.get_full_path())
78 request.session['from_app'] = True
79 elif request.session and 'from_app' in request.session:
80 del request.session['from_app']
82 return super().get(request)
84 def get_context_data(self, **kwargs):
85 c = super(JoinView, self).get_context_data(**kwargs)
86 c['membership'] = getattr(self.request.user, 'membership', None)
87 c['active_menu_item'] = 'club'
88 c['club'] = models.Club.objects.first()
90 c['ambassador'] = models.Ambassador.objects.all().order_by('?').first()
93 def get_initial(self):
95 if self.request.user.is_authenticated and self.request.user.email:
97 'email': self.request.user.email
100 def get_form_kwargs(self):
101 kwargs = super().get_form_kwargs()
102 #kwargs['referer'] = self.request.META.get('HTTP_REFERER', '')
105 def form_valid(self, form):
106 retval = super(JoinView, self).form_valid(form)
107 if self.request.user.is_authenticated:
108 form.instance.membership, created = models.Membership.objects.get_or_create(user=self.request.user)
112 def get_success_url(self):
113 return reverse('donation_step2', args=[self.object.key])
116 @method_decorator(never_cache, name='dispatch')
117 class ScheduleView(DetailView):
118 queryset = models.Schedule.objects.exclude(email='')
119 slug_field = slug_url_kwarg = 'key'
120 template_name = 'club/schedule.html'
123 def get_template_names(self):
124 if not self.object.payed_at:
125 return 'club/donation_step3.html'
126 return 'club/schedule.html'
128 def get_context_data(self, *args, **kwargs):
129 ctx = super().get_context_data(*args, **kwargs)
130 ctx['active_menu_item'] = 'club'
133 def post(self, request, key):
134 schedule = self.get_object()
135 return HttpResponseRedirect(schedule.initiate_payment(request))
139 def claim(request, key):
140 schedule = models.Schedule.objects.get(key=key, membership=None)
141 schedule.membership, created = models.Membership.objects.get_or_create(user=request.user)
143 return HttpResponseRedirect(schedule.get_absolute_url())
146 def cancel(request, key):
147 schedule = models.Schedule.objects.get(key=key)
148 schedule.is_cancelled = True
150 return HttpResponseRedirect(schedule.get_absolute_url())
153 class PayUPayment(DetailView):
154 model = models.Schedule
155 slug_field = slug_url_kwarg = 'key'
157 def get(self, request, key):
158 schedule = self.get_object()
159 schedule.method = 'payu'
160 schedule.save(update_fields=['method'])
161 return HttpResponseRedirect(schedule.initiate_payment(request))
165 class PayURecPayment(payu_views.RecPayment):
166 form_class = PayUCardTokenForm
168 def get_schedule(self):
169 return get_object_or_404(models.Schedule, key=self.kwargs['key'])
172 pos_id = recurring_payment_method.pos_id
175 def get_success_url(self):
176 schedule = self.get_schedule()
177 schedule.method = 'payu-re'
178 schedule.save(update_fields=['method'])
179 return schedule.pay(self.request)
182 class PayUNotifyView(payu_views.NotifyView):
183 order_model = models.PayUOrder
186 class ScheduleThanksView(DetailView):
187 model = models.Schedule
188 template_name = 'club/donation_step4.html'
189 slug_field = slug_url_kwarg = 'key'
192 def get_context_data(self, *args, **kwargs):
193 ctx = super().get_context_data(*args, **kwargs)
194 ctx['active_menu_item'] = 'club'
198 class YearSummaryView(DetailView):
199 model = models.Schedule
200 slug_field = slug_url_kwarg = 'key'
201 template_name = 'club/year_summary.html'
203 def get_context_data(self, *args, **kwargs):
204 ctx = super().get_context_data(*args, **kwargs)
205 ctx['payments'] = models.PayUOrder.objects.filter(
207 completed_at__year=self.kwargs['year'],
208 schedule__email=self.object.email,
209 ).order_by('completed_at')
210 ctx['total_amount'] = ctx['payments'].aggregate(s=Sum('schedule__amount'))['s']
214 @permission_required('club.schedule_view')
215 def member_verify(request):
216 if request.method == 'POST':
217 emails = request.POST.get('emails').strip().split('\n')
218 rows = ['email;członek;nazwa użytkownika;aktywny;co najmniej do']
220 email = email.strip()
222 schedules = models.Schedule.objects.filter(email=email).exclude(payed_at=None)
223 if schedules.exists():
232 unames.add(s.membership.user.username)
233 if exp is None or s.expires_at > exp:
235 row.append(','.join(sorted(unames)))
236 row.append('tak' if akt else 'nie')
237 row.append(exp.date().isoformat())
240 rows.append(';'.join(row))
241 rows = '\n'.join(rows)
247 'club/member_verify.html',