1393b9693adaaa49a82d395263a332fb0d5dfb0f
[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     form_class = ScheduleForm
17     template_name = 'club/membership_form.html'
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, form=None):
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 AppJoinView(JoinView):
46     template_name = 'club/membership_form_app.html'
47
48
49 class ScheduleView(View):
50     def get(self, request, key):
51         schedule = models.Schedule.objects.get(key=key)
52         if not schedule.is_active:
53             return HttpResponseRedirect(schedule.get_payment_method().get_payment_url(schedule))
54         else:
55             return render(
56                 request,
57                 'club/schedule.html',
58                 {
59                     'schedule': schedule,
60                 }
61             )
62
63
64 @login_required
65 def claim(request, key):
66     schedule = models.Schedule.objects.get(key=key, membership=None)
67     schedule.membership, created = models.Membership.objects.get_or_create(user=request.user)
68     schedule.save()
69     return HttpResponseRedirect(schedule.get_absolute_url())
70
71
72 def cancel(request, key):
73     schedule = models.Schedule.objects.get(key=key)
74     schedule.is_cancelled = True
75     schedule.save()
76     return HttpResponseRedirect(schedule.get_absolute_url())
77
78
79 class DummyPaymentView(TemplateView):
80     template_name = 'club/dummy_payment.html'
81
82     def get_context_data(self, key):
83         return {
84             'schedule': models.Schedule.objects.get(key=key),
85         }
86
87     def post(self, request, key):
88         schedule = models.Schedule.objects.get(key=key)
89         schedule.create_payment()
90         return HttpResponseRedirect(schedule.get_absolute_url())