Remove legacy contact app.
[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 payure_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_form_kwargs(self):
52         kwargs = super().get_form_kwargs()
53         kwargs['request'] = self.request
54         return kwargs
55
56     def get_context_data(self, **kwargs):
57         c = super(JoinView, self).get_context_data(**kwargs)
58         c['membership'] = getattr(self.request.user, 'membership', None)
59         c['active_menu_item'] = 'club'
60         return c
61
62     def get_initial(self):
63         if self.request.user.is_authenticated and self.request.user.email:
64             return {
65                 'email': self.request.user.email,
66             }
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(payu_views.Payment):
125     pass
126
127
128 class PayURecPayment(payu_views.RecPayment):
129     form_class = PayUCardTokenForm
130
131     def get_schedule(self):
132         return get_object_or_404(models.Schedule, key=self.kwargs['key'])
133
134     def get_pos(self):
135         pos_id = payure_method.pos_id
136         return POSS[pos_id]
137
138     def get_success_url(self):
139         return self.get_schedule().pay(self.request)
140
141
142 class PayUNotifyView(payu_views.NotifyView):
143     order_model = models.PayUOrder
144
145
146 class MembershipView(UpdateView):
147     fields = ['name']
148
149     def get_success_url(self):
150         # TODO: get only current schedule if multiple.
151         return self.object.schedule_set.first().get_absolute_url()
152
153     def get_object(self):
154         return self.request.user.membership
155
156
157 class ScheduleThanksView(DetailView):
158     model = models.Schedule
159     slug_field = slug_url_kwarg = 'key'
160     template_name = 'club/thanks.html'
161
162     def get_context_data(self, *args, **kwargs):
163         ctx = super().get_context_data(*args, **kwargs)
164         ctx['active_menu_item'] = 'club'
165         return ctx
166