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