e93864cab9a0d7b35953889d4796926cf4da5e96
[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
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 form_valid(self, form):
64         retval = super(JoinView, self).form_valid(form)
65         if self.request.user.is_authenticated:
66             form.instance.membership, created = models.Membership.objects.get_or_create(user=self.request.user)
67             form.instance.save()
68         return retval
69
70     def get_success_url(self):
71         return self.object.initiate_payment(self.request)
72
73
74 @method_decorator(never_cache, name='dispatch')
75 class ScheduleView(DetailView):
76     model = models.Schedule
77     slug_field = slug_url_kwarg = 'key'
78     template_name = 'club/schedule.html'
79
80     def get_context_data(self, *args, **kwargs):
81         ctx = super().get_context_data(*args, **kwargs)
82         ctx['active_menu_item'] = 'club'
83         return ctx
84
85     def post(self, request, key):
86         schedule = self.get_object()
87         return HttpResponseRedirect(schedule.initiate_payment(request))
88
89
90 @login_required
91 def claim(request, key):
92     schedule = models.Schedule.objects.get(key=key, membership=None)
93     schedule.membership, created = models.Membership.objects.get_or_create(user=request.user)
94     schedule.save()
95     return HttpResponseRedirect(schedule.get_absolute_url())
96
97
98 def cancel(request, key):
99     schedule = models.Schedule.objects.get(key=key)
100     schedule.is_cancelled = True
101     schedule.save()
102     return HttpResponseRedirect(schedule.get_absolute_url())
103
104
105 class DummyPaymentView(TemplateView):
106     template_name = 'club/dummy_payment.html'
107
108     def get_context_data(self, key):
109         return {
110             'schedule': models.Schedule.objects.get(key=key),
111         }
112
113     def post(self, request, key):
114         schedule = models.Schedule.objects.get(key=key)
115         schedule.create_payment()
116         return HttpResponseRedirect(schedule.get_absolute_url())
117
118
119 class PayUPayment(DetailView):
120     model = models.Schedule
121     slug_field = slug_url_kwarg = 'key'
122
123     def get(self, request, key):
124         schedule = self.get_object()
125         return HttpResponseRedirect(schedule.initiate_payment(request))
126
127
128
129 class PayURecPayment(payu_views.RecPayment):
130     form_class = PayUCardTokenForm
131
132     def get_schedule(self):
133         return get_object_or_404(models.Schedule, key=self.kwargs['key'])
134
135     def get_pos(self):
136         pos_id = recurring_payment_method.pos_id
137         return POSS[pos_id]
138
139     def get_success_url(self):
140         return self.get_schedule().pay(self.request)
141
142
143 class PayUNotifyView(payu_views.NotifyView):
144     order_model = models.PayUOrder
145
146
147 class MembershipView(UpdateView):
148     fields = ['name']
149
150     def get_success_url(self):
151         # TODO: get only current schedule if multiple.
152         return self.object.schedule_set.first().get_absolute_url()
153
154     def get_object(self):
155         return self.request.user.membership
156
157
158 class ScheduleThanksView(DetailView):
159     model = models.Schedule
160     slug_field = slug_url_kwarg = 'key'
161     template_name = 'club/thanks.html'
162
163     def get_context_data(self, *args, **kwargs):
164         ctx = super().get_context_data(*args, **kwargs)
165         ctx['active_menu_item'] = 'club'
166         return ctx
167
168
169 class YearSummaryView(DetailView):
170     model = models.Schedule
171     slug_field = slug_url_kwarg = 'key'
172     template_name = 'club/year_summary.html'
173
174     def get_context_data(self, *args, **kwargs):
175         ctx = super().get_context_data(*args, **kwargs)
176         ctx['payments'] = models.PayUOrder.objects.filter(
177             status='COMPLETED',
178             completed_at__year=self.kwargs['year'],
179             schedule__email=self.object.email,
180         ).order_by('completed_at')
181         ctx['total_amount'] = ctx['payments'].aggregate(s=Sum('schedule__amount'))['s']
182         return ctx