9816b4b9927f05e51b0d1831ac92b486a5a16e79
[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, UpdateView
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         return HttpResponseRedirect(schedule.initiate_payment(request))
71
72
73 @login_required
74 def claim(request, key):
75     schedule = models.Schedule.objects.get(key=key, membership=None)
76     schedule.membership, created = models.Membership.objects.get_or_create(user=request.user)
77     schedule.save()
78     return HttpResponseRedirect(schedule.get_absolute_url())
79
80
81 def cancel(request, key):
82     schedule = models.Schedule.objects.get(key=key)
83     schedule.is_cancelled = True
84     schedule.save()
85     return HttpResponseRedirect(schedule.get_absolute_url())
86
87
88 class DummyPaymentView(TemplateView):
89     template_name = 'club/dummy_payment.html'
90
91     def get_context_data(self, key):
92         return {
93             'schedule': models.Schedule.objects.get(key=key),
94         }
95
96     def post(self, request, key):
97         schedule = models.Schedule.objects.get(key=key)
98         schedule.create_payment()
99         return HttpResponseRedirect(schedule.get_absolute_url())
100
101
102 class PayUPayment(payu_views.Payment):
103     pass
104
105
106 class PayURecPayment(payu_views.RecPayment):
107     form_class = PayUCardTokenForm
108
109     def get_schedule(self):
110         return get_object_or_404(models.Schedule, key=self.kwargs['key'])
111
112     def get_pos(self):
113         pos_id = payure_method.pos_id
114         return POSS[pos_id]
115
116     def get_success_url(self):
117         return self.get_schedule().pay(self.request)
118
119
120 class PayUNotifyView(payu_views.NotifyView):
121     order_model = models.PayUOrder
122
123
124 class MembershipView(UpdateView):
125     fields = ['name']
126
127     def get_success_url(self):
128         # TODO: get only current schedule if multiple.
129         return self.object.schedule_set.first().get_absolute_url()
130
131     def get_object(self):
132         return self.request.user.membership