af59b1e9a6307367e479d8853c003d1640a35afe
[wolnelektury.git] / src / club / views.py
1 from django.contrib.auth.decorators import login_required
2 from django.http import HttpResponseRedirect
3 from django.shortcuts import get_object_or_404, render
4 from django.views.generic import FormView, CreateView, TemplateView, DetailView
5 from django.views import View
6 from .payu import POSS
7 from .payu import views as payu_views
8 from .forms import ScheduleForm, PayUCardTokenForm
9 from . import models
10 from .helpers import get_active_schedule
11 from .payment_methods import payure_method
12
13
14 class ClubView(TemplateView):
15     template_name = 'club/index.html'
16
17
18 class JoinView(CreateView):
19     form_class = ScheduleForm
20     template_name = 'club/membership_form.html'
21
22     def is_app(self):
23         return self.request.GET.get('app')
24
25     def get(self, request):
26         if self.is_app():
27             request.session['from_app'] = True
28         elif request.session and 'from_app' in request.session:
29             del request.session['from_app']
30         schedule = get_active_schedule(request.user)
31         if schedule is not None:
32             return HttpResponseRedirect(schedule.get_absolute_url())
33         else:
34             return super(JoinView, self).get(request)
35
36     def get_form_kwargs(self):
37         kwargs = super().get_form_kwargs()
38         kwargs['request'] = self.request
39         return kwargs
40
41     def get_context_data(self, form=None):
42         c = super(JoinView, self).get_context_data()
43         c['membership'] = getattr(self.request.user, 'membership', None)
44         return c
45
46     def get_initial(self):
47         if self.request.user.is_authenticated and self.request.user.email:
48             return {
49                 'email': self.request.user.email,
50             }
51
52     def form_valid(self, form):
53         retval = super(JoinView, self).form_valid(form)
54         if self.request.user.is_authenticated:
55             form.instance.membership, created = models.Membership.objects.get_or_create(user=self.request.user)
56             form.instance.save()
57         return retval
58
59     def get_success_url(self):
60         return self.object.initiate_payment(self.request)
61
62
63 class ScheduleView(DetailView):
64     model = models.Schedule
65     slug_field = slug_url_kwarg = 'key'
66     template_name = 'club/schedule.html'
67
68     def post(self, request, key):
69         schedule = self.get_object()
70         if not schedule.is_active():
71             return HttpResponseRedirect(schedule.initiate_payment(request))
72         else:
73             return HttpResponseRedirect(schedule.get_absolute_url())
74
75
76 @login_required
77 def claim(request, key):
78     schedule = models.Schedule.objects.get(key=key, membership=None)
79     schedule.membership, created = models.Membership.objects.get_or_create(user=request.user)
80     schedule.save()
81     return HttpResponseRedirect(schedule.get_absolute_url())
82
83
84 def cancel(request, key):
85     schedule = models.Schedule.objects.get(key=key)
86     schedule.is_cancelled = True
87     schedule.save()
88     return HttpResponseRedirect(schedule.get_absolute_url())
89
90
91 class DummyPaymentView(TemplateView):
92     template_name = 'club/dummy_payment.html'
93
94     def get_context_data(self, key):
95         return {
96             'schedule': models.Schedule.objects.get(key=key),
97         }
98
99     def post(self, request, key):
100         schedule = models.Schedule.objects.get(key=key)
101         schedule.create_payment()
102         return HttpResponseRedirect(schedule.get_absolute_url())
103
104
105 class PayUPayment(payu_views.Payment):
106     pass
107
108
109 class PayURecPayment(payu_views.RecPayment):
110     form_class = PayUCardTokenForm
111
112     def get_schedule(self):
113         return get_object_or_404(models.Schedule, key=self.kwargs['key'])
114
115     def get_pos(self):
116         pos_id = payure_method.pos_id
117         return POSS[pos_id]
118
119     def get_success_url(self):
120         return self.get_schedule().pay(self.request)
121
122
123 class PayUNotifyView(payu_views.NotifyView):
124     order_model = models.PayUOrder
125