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
8 from .helpers import get_active_schedule
11 class ClubView(TemplateView):
12 template_name = 'club/index.html'
15 class JoinView(CreateView):
16 template_name = 'club/membership_form.html'
17 form_class = ScheduleForm
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())
24 return super(JoinView, self).get(request)
26 def get_context_data(self):
27 c = super(JoinView, self).get_context_data()
28 c['membership'] = getattr(self.request.user, 'membership', None)
31 def get_initial(self):
32 if self.request.user.is_authenticated and self.request.user.email:
34 'email': self.request.user.email,
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)
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))
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)
65 return HttpResponseRedirect(schedule.get_absolute_url())
68 def cancel(request, key):
69 schedule = models.Schedule.objects.get(key=key)
70 schedule.is_cancelled = True
72 return HttpResponseRedirect(schedule.get_absolute_url())
75 class DummyPaymentView(TemplateView):
76 template_name = 'club/dummy_payment.html'
78 def get_context_data(self, key):
80 'schedule': models.Schedule.objects.get(key=key),
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())