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