32daffde1920a78d84e44c8e18a0150c13db6b16
[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.http import HttpResponseRedirect
7 from django.shortcuts import get_object_or_404, render
8 from django.utils.decorators import method_decorator
9 from django.views.decorators.cache import never_cache
10 from django.views.generic import FormView, CreateView, TemplateView, DetailView, UpdateView
11 from django.views import View
12 from .payu import POSS
13 from .payu import views as payu_views
14 from .forms import ScheduleForm, PayUCardTokenForm
15 from . import models
16 from .helpers import get_active_schedule
17 from .payment_methods import recurring_payment_method
18
19
20 class ClubView(TemplateView):
21     template_name = 'club/index.html'
22
23     def get_context_data(self, *args, **kwargs):
24         ctx = super().get_context_data(*args, **kwargs)
25         ctx['active_menu_item'] = 'club'
26         return ctx
27
28
29 class JoinView(CreateView):
30     form_class = ScheduleForm
31     template_name = 'club/membership_form.html'
32
33     def is_app(self):
34         return self.request.GET.get('app')
35
36     def get(self, request):
37         # TODO: configure as app-allowed hosts.
38         if settings.CLUB_APP_HOST and self.is_app() and request.META['HTTP_HOST'] != settings.CLUB_APP_HOST:
39             return HttpResponseRedirect("https://" + settings.CLUB_APP_HOST + request.get_full_path())
40
41         if self.is_app():
42             request.session['from_app'] = True
43         elif request.session and 'from_app' in request.session:
44             del request.session['from_app']
45         schedule = get_active_schedule(request.user)
46         if schedule is not None:
47             return HttpResponseRedirect(schedule.get_absolute_url())
48         else:
49             return super(JoinView, self).get(request)
50
51     def get_context_data(self, **kwargs):
52         c = super(JoinView, self).get_context_data(**kwargs)
53         c['membership'] = getattr(self.request.user, 'membership', None)
54         c['active_menu_item'] = 'club'
55         c['club'] = models.Club.objects.first()
56
57         c['ambassador'] = models.Ambassador.objects.all().order_by('?').first()
58         return c
59
60     def get_initial(self):
61         if self.request.user.is_authenticated and self.request.user.email:
62             return {
63                 'email': self.request.user.email,
64             }
65
66     def form_valid(self, form):
67         retval = super(JoinView, self).form_valid(form)
68         if self.request.user.is_authenticated:
69             form.instance.membership, created = models.Membership.objects.get_or_create(user=self.request.user)
70             form.instance.save()
71         return retval
72
73     def get_success_url(self):
74         return self.object.initiate_payment(self.request)
75
76
77 @method_decorator(never_cache, name='dispatch')
78 class ScheduleView(DetailView):
79     model = models.Schedule
80     slug_field = slug_url_kwarg = 'key'
81     template_name = 'club/schedule.html'
82
83     def get_context_data(self, *args, **kwargs):
84         ctx = super().get_context_data(*args, **kwargs)
85         ctx['active_menu_item'] = 'club'
86         return ctx
87
88     def post(self, request, key):
89         schedule = self.get_object()
90         return HttpResponseRedirect(schedule.initiate_payment(request))
91
92
93 @login_required
94 def claim(request, key):
95     schedule = models.Schedule.objects.get(key=key, membership=None)
96     schedule.membership, created = models.Membership.objects.get_or_create(user=request.user)
97     schedule.save()
98     return HttpResponseRedirect(schedule.get_absolute_url())
99
100
101 def cancel(request, key):
102     schedule = models.Schedule.objects.get(key=key)
103     schedule.is_cancelled = True
104     schedule.save()
105     return HttpResponseRedirect(schedule.get_absolute_url())
106
107
108 class DummyPaymentView(TemplateView):
109     template_name = 'club/dummy_payment.html'
110
111     def get_context_data(self, key):
112         return {
113             'schedule': models.Schedule.objects.get(key=key),
114         }
115
116     def post(self, request, key):
117         schedule = models.Schedule.objects.get(key=key)
118         schedule.create_payment()
119         return HttpResponseRedirect(schedule.get_absolute_url())
120
121
122 class PayUPayment(payu_views.Payment):
123     pass
124
125
126 class PayURecPayment(payu_views.RecPayment):
127     form_class = PayUCardTokenForm
128
129     def get_schedule(self):
130         return get_object_or_404(models.Schedule, key=self.kwargs['key'])
131
132     def get_pos(self):
133         pos_id = recurring_payment_method.pos_id
134         return POSS[pos_id]
135
136     def get_success_url(self):
137         return self.get_schedule().pay(self.request)
138
139
140 class PayUNotifyView(payu_views.NotifyView):
141     order_model = models.PayUOrder
142
143
144 class MembershipView(UpdateView):
145     fields = ['name']
146
147     def get_success_url(self):
148         # TODO: get only current schedule if multiple.
149         return self.object.schedule_set.first().get_absolute_url()
150
151     def get_object(self):
152         return self.request.user.membership
153
154
155 class ScheduleThanksView(DetailView):
156     model = models.Schedule
157     slug_field = slug_url_kwarg = 'key'
158     template_name = 'club/thanks.html'
159
160     def get_context_data(self, *args, **kwargs):
161         ctx = super().get_context_data(*args, **kwargs)
162         ctx['active_menu_item'] = 'club'
163         return ctx
164