Base payment scheme.
[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 render
4 from django.views.generic import FormView, CreateView, TemplateView
5 from django.views import View
6 from .forms import ScheduleForm
7 from . import models
8 from .helpers import get_active_schedule
9
10
11 class ClubView(TemplateView):
12     template_name = 'club/index.html'
13
14
15 class JoinView(CreateView):
16     template_name = 'club/membership_form.html'
17     form_class = ScheduleForm
18
19     def get(self, request):
20         schedule = get_active_schedule(request.user)
21         if schedule is not None:
22             return HttpResponseRedirect(schedule.get_absolute_url())
23         else:
24             return super(JoinView, self).get(request)
25
26     def get_context_data(self):
27         c = super(JoinView, self).get_context_data()
28         c['membership'] = getattr(self.request.user, 'membership', None)
29         return c
30
31     def get_initial(self):
32         if self.request.user.is_authenticated and self.request.user.email:
33             return {
34                 'email': self.request.user.email,
35             }
36
37     def form_valid(self, form):
38         retval = super(JoinView, self).form_valid(form)
39         if self.request.user.is_authenticated:
40             form.instance.membership, created = models.Membership.objects.get_or_create(user=self.request.user)
41             form.instance.save()
42         return retval
43
44
45 class ScheduleView(View):
46     def get(self, request, key):
47         schedule = models.Schedule.objects.get(key=key)
48         if not schedule.is_active:
49             return HttpResponseRedirect(schedule.get_payment_method().get_payment_url(schedule))
50         else:
51             return render(
52                 request,
53                 'club/schedule.html',
54                 {
55                     'schedule': schedule,
56                 }
57             )
58
59
60 @login_required
61 def claim(request, key):
62     schedule = models.Schedule.objects.get(key=key, membership=None)
63     schedule.membership, created = models.Membership.objects.get_or_create(user=request.user)
64     schedule.save()
65     return HttpResponseRedirect(schedule.get_absolute_url())
66
67
68 def cancel(request, key):
69     schedule = models.Schedule.objects.get(key=key)
70     schedule.is_cancelled = True
71     schedule.save()
72     return HttpResponseRedirect(schedule.get_absolute_url())
73
74
75 class DummyPaymentView(TemplateView):
76     template_name = 'club/dummy_payment.html'
77
78     def get_context_data(self, key):
79         return {
80             'schedule': models.Schedule.objects.get(key=key),
81         }
82
83     def post(self, request, key):
84         schedule = models.Schedule.objects.get(key=key)
85         schedule.create_payment()
86         return HttpResponseRedirect(schedule.get_absolute_url())