librarian upgrade
[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.utils.decorators import method_decorator
10 from django.views.decorators.cache import never_cache
11 from django.views.generic import FormView, CreateView, TemplateView, DetailView, UpdateView
12 from django.views import View
13 from .payu import POSS
14 from .payu import views as payu_views
15 from .forms import ScheduleForm, PayUCardTokenForm
16 from . import models
17 from .helpers import get_active_schedule
18 from .payment_methods import recurring_payment_method
19
20
21 class ClubView(TemplateView):
22     template_name = 'club/index.html'
23
24     def get_context_data(self, *args, **kwargs):
25         ctx = super().get_context_data(*args, **kwargs)
26         ctx['active_menu_item'] = 'club'
27         return ctx
28
29
30 class JoinView(CreateView):
31     form_class = ScheduleForm
32     template_name = 'club/membership_form.html'
33
34     def is_app(self):
35         return self.request.GET.get('app')
36
37     def get(self, request):
38         # TODO: configure as app-allowed hosts.
39         if settings.CLUB_APP_HOST and self.is_app() and request.META['HTTP_HOST'] != settings.CLUB_APP_HOST:
40             return HttpResponseRedirect("https://" + settings.CLUB_APP_HOST + request.get_full_path())
41
42         if self.is_app():
43             request.session['from_app'] = True
44         elif request.session and 'from_app' in request.session:
45             del request.session['from_app']
46         return super(JoinView, self).get(request)
47
48     def get_context_data(self, **kwargs):
49         c = super(JoinView, self).get_context_data(**kwargs)
50         c['membership'] = getattr(self.request.user, 'membership', None)
51         c['active_menu_item'] = 'club'
52         c['club'] = models.Club.objects.first()
53
54         c['ambassador'] = models.Ambassador.objects.all().order_by('?').first()
55         return c
56
57     def get_initial(self):
58         if self.request.user.is_authenticated and self.request.user.email:
59             return {
60                 'email': self.request.user.email,
61             }
62
63     def get_form_kwargs(self):
64         kwargs = super().get_form_kwargs()
65         kwargs['referer'] = self.request.META.get('HTTP_REFERER', '')
66         return kwargs
67
68     def form_valid(self, form):
69         retval = super(JoinView, self).form_valid(form)
70         if self.request.user.is_authenticated:
71             form.instance.membership, created = models.Membership.objects.get_or_create(user=self.request.user)
72             form.instance.save()
73         return retval
74
75     def get_success_url(self):
76         return self.object.initiate_payment(self.request)
77
78
79 @method_decorator(never_cache, name='dispatch')
80 class ScheduleView(DetailView):
81     model = models.Schedule
82     slug_field = slug_url_kwarg = 'key'
83     template_name = 'club/schedule.html'
84
85     def get_context_data(self, *args, **kwargs):
86         ctx = super().get_context_data(*args, **kwargs)
87         ctx['active_menu_item'] = 'club'
88         return ctx
89
90     def post(self, request, key):
91         schedule = self.get_object()
92         return HttpResponseRedirect(schedule.initiate_payment(request))
93
94
95 @login_required
96 def claim(request, key):
97     schedule = models.Schedule.objects.get(key=key, membership=None)
98     schedule.membership, created = models.Membership.objects.get_or_create(user=request.user)
99     schedule.save()
100     return HttpResponseRedirect(schedule.get_absolute_url())
101
102
103 def cancel(request, key):
104     schedule = models.Schedule.objects.get(key=key)
105     schedule.is_cancelled = True
106     schedule.save()
107     return HttpResponseRedirect(schedule.get_absolute_url())
108
109
110 class DummyPaymentView(TemplateView):
111     template_name = 'club/dummy_payment.html'
112
113     def get_context_data(self, key):
114         return {
115             'schedule': models.Schedule.objects.get(key=key),
116         }
117
118     def post(self, request, key):
119         schedule = models.Schedule.objects.get(key=key)
120         schedule.create_payment()
121         return HttpResponseRedirect(schedule.get_absolute_url())
122
123
124 class PayUPayment(DetailView):
125     model = models.Schedule
126     slug_field = slug_url_kwarg = 'key'
127
128     def get(self, request, key):
129         schedule = self.get_object()
130         return HttpResponseRedirect(schedule.initiate_payment(request))
131
132
133
134 class PayURecPayment(payu_views.RecPayment):
135     form_class = PayUCardTokenForm
136
137     def get_schedule(self):
138         return get_object_or_404(models.Schedule, key=self.kwargs['key'])
139
140     def get_pos(self):
141         pos_id = recurring_payment_method.pos_id
142         return POSS[pos_id]
143
144     def get_success_url(self):
145         return self.get_schedule().pay(self.request)
146
147
148 class PayUNotifyView(payu_views.NotifyView):
149     order_model = models.PayUOrder
150
151
152 class MembershipView(UpdateView):
153     fields = ['name']
154
155     def get_success_url(self):
156         # TODO: get only current schedule if multiple.
157         return self.object.schedule_set.first().get_absolute_url()
158
159     def get_object(self):
160         return self.request.user.membership
161
162
163 class ScheduleThanksView(DetailView):
164     model = models.Schedule
165     slug_field = slug_url_kwarg = 'key'
166     template_name = 'club/thanks.html'
167
168     def get_context_data(self, *args, **kwargs):
169         ctx = super().get_context_data(*args, **kwargs)
170         ctx['active_menu_item'] = 'club'
171         return ctx
172
173
174 class YearSummaryView(DetailView):
175     model = models.Schedule
176     slug_field = slug_url_kwarg = 'key'
177     template_name = 'club/year_summary.html'
178
179     def get_context_data(self, *args, **kwargs):
180         ctx = super().get_context_data(*args, **kwargs)
181         ctx['payments'] = models.PayUOrder.objects.filter(
182             status='COMPLETED',
183             completed_at__year=self.kwargs['year'],
184             schedule__email=self.object.email,
185         ).order_by('completed_at')
186         ctx['total_amount'] = ctx['payments'].aggregate(s=Sum('schedule__amount'))['s']
187         return ctx
188
189
190 @permission_required('club.schedule_view')
191 def member_verify(request):
192     if request.method == 'POST':
193         emails = request.POST.get('emails').strip().split('\n')
194         rows = ['email;członek;nazwa użytkownika;aktywny;co najmniej do']
195         for email in emails:
196             email = email.strip()
197             row = [email]
198             schedules = models.Schedule.objects.filter(email=email).exclude(payed_at=None)
199             if schedules.exists():
200                 row.append('tak')
201                 akt = False
202                 unames = set()
203                 exp = None
204                 for s in schedules:
205                     if s.is_active():
206                         akt = True
207                     if s.membership:
208                         unames.add(s.membership.user.username) 
209                     if exp is None or s.expires_at > exp:
210                         exp = s.expires_at
211                 row.append(','.join(sorted(unames)))
212                 row.append('tak' if akt else 'nie')
213                 row.append(exp.date().isoformat())
214             else:
215                 row.append('nie')
216             rows.append(';'.join(row))
217         rows = '\n'.join(rows)
218     else:
219         rows = ''
220
221     return render(
222         request,
223         'club/member_verify.html',
224         {
225             'result': rows
226         }
227     )