1 from django.conf import settings
2 from django.contrib.auth.decorators import login_required
3 from django.http import HttpResponseRedirect
4 from django.shortcuts import get_object_or_404, render
5 from django.utils.decorators import method_decorator
6 from django.views.decorators.cache import never_cache
7 from django.views.generic import FormView, CreateView, TemplateView, DetailView, UpdateView
8 from django.views import View
10 from .payu import views as payu_views
11 from .forms import ScheduleForm, PayUCardTokenForm
13 from .helpers import get_active_schedule
14 from .payment_methods import payure_method
17 class ClubView(TemplateView):
18 template_name = 'club/index.html'
20 def get_context_data(self, *args, **kwargs):
21 ctx = super().get_context_data(*args, **kwargs)
22 ctx['active_menu_item'] = 'club'
26 class JoinView(CreateView):
27 form_class = ScheduleForm
28 template_name = 'club/membership_form.html'
31 return self.request.GET.get('app')
33 def get(self, request):
34 # TODO: configure as app-allowed hosts.
35 if settings.CLUB_APP_HOST and self.is_app() and request.META['HTTP_HOST'] != settings.CLUB_APP_HOST:
36 return HttpResponseRedirect("https://" + settings.CLUB_APP_HOST + request.get_full_path())
39 request.session['from_app'] = True
40 elif request.session and 'from_app' in request.session:
41 del request.session['from_app']
42 schedule = get_active_schedule(request.user)
43 if schedule is not None:
44 return HttpResponseRedirect(schedule.get_absolute_url())
46 return super(JoinView, self).get(request)
48 def get_form_kwargs(self):
49 kwargs = super().get_form_kwargs()
50 kwargs['request'] = self.request
53 def get_context_data(self, **kwargs):
54 c = super(JoinView, self).get_context_data(**kwargs)
55 c['membership'] = getattr(self.request.user, 'membership', None)
56 c['active_menu_item'] = 'club'
59 def get_initial(self):
60 if self.request.user.is_authenticated and self.request.user.email:
62 'email': self.request.user.email,
65 def form_valid(self, form):
66 retval = super(JoinView, self).form_valid(form)
67 if self.request.user.is_authenticated:
68 form.instance.membership, created = models.Membership.objects.get_or_create(user=self.request.user)
72 def get_success_url(self):
73 return self.object.initiate_payment(self.request)
76 @method_decorator(never_cache, name='dispatch')
77 class ScheduleView(DetailView):
78 model = models.Schedule
79 slug_field = slug_url_kwarg = 'key'
80 template_name = 'club/schedule.html'
82 def get_context_data(self, *args, **kwargs):
83 ctx = super().get_context_data(*args, **kwargs)
84 ctx['active_menu_item'] = 'club'
87 def post(self, request, key):
88 schedule = self.get_object()
89 return HttpResponseRedirect(schedule.initiate_payment(request))
93 def claim(request, key):
94 schedule = models.Schedule.objects.get(key=key, membership=None)
95 schedule.membership, created = models.Membership.objects.get_or_create(user=request.user)
97 return HttpResponseRedirect(schedule.get_absolute_url())
100 def cancel(request, key):
101 schedule = models.Schedule.objects.get(key=key)
102 schedule.is_cancelled = True
104 return HttpResponseRedirect(schedule.get_absolute_url())
107 class DummyPaymentView(TemplateView):
108 template_name = 'club/dummy_payment.html'
110 def get_context_data(self, key):
112 'schedule': models.Schedule.objects.get(key=key),
115 def post(self, request, key):
116 schedule = models.Schedule.objects.get(key=key)
117 schedule.create_payment()
118 return HttpResponseRedirect(schedule.get_absolute_url())
121 class PayUPayment(payu_views.Payment):
125 class PayURecPayment(payu_views.RecPayment):
126 form_class = PayUCardTokenForm
128 def get_schedule(self):
129 return get_object_or_404(models.Schedule, key=self.kwargs['key'])
132 pos_id = payure_method.pos_id
135 def get_success_url(self):
136 return self.get_schedule().pay(self.request)
139 class PayUNotifyView(payu_views.NotifyView):
140 order_model = models.PayUOrder
143 class MembershipView(UpdateView):
146 def get_success_url(self):
147 # TODO: get only current schedule if multiple.
148 return self.object.schedule_set.first().get_absolute_url()
150 def get_object(self):
151 return self.request.user.membership
154 class ScheduleThanksView(DetailView):
155 model = models.Schedule
156 slug_field = slug_url_kwarg = 'key'
157 template_name = 'club/thanks.html'
159 def get_context_data(self, *args, **kwargs):
160 ctx = super().get_context_data(*args, **kwargs)
161 ctx['active_menu_item'] = 'club'