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 form_class = ScheduleForm
17 template_name = 'club/membership_form.html'
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, form=None):
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 AppJoinView(JoinView):
46 template_name = 'club/membership_form_app.html'
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))
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)
69 return HttpResponseRedirect(schedule.get_absolute_url())
72 def cancel(request, key):
73 schedule = models.Schedule.objects.get(key=key)
74 schedule.is_cancelled = True
76 return HttpResponseRedirect(schedule.get_absolute_url())
79 class DummyPaymentView(TemplateView):
80 template_name = 'club/dummy_payment.html'
82 def get_context_data(self, key):
84 'schedule': models.Schedule.objects.get(key=key),
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())