1 from django.contrib.auth.decorators import login_required
2 from django.http import HttpResponseRedirect
3 from django.shortcuts import get_object_or_404, render
4 from django.views.generic import FormView, CreateView, TemplateView, DetailView, UpdateView
5 from django.views import View
7 from .payu import views as payu_views
8 from .forms import ScheduleForm, PayUCardTokenForm
10 from .helpers import get_active_schedule
11 from .payment_methods import payure_method
14 class ClubView(TemplateView):
15 template_name = 'club/index.html'
18 class JoinView(CreateView):
19 form_class = ScheduleForm
20 template_name = 'club/membership_form.html'
23 return self.request.GET.get('app')
25 def get(self, request):
27 request.session['from_app'] = True
28 elif request.session and 'from_app' in request.session:
29 del request.session['from_app']
30 schedule = get_active_schedule(request.user)
31 if schedule is not None:
32 return HttpResponseRedirect(schedule.get_absolute_url())
34 return super(JoinView, self).get(request)
36 def get_form_kwargs(self):
37 kwargs = super().get_form_kwargs()
38 kwargs['request'] = self.request
41 def get_context_data(self, form=None):
42 c = super(JoinView, self).get_context_data()
43 c['membership'] = getattr(self.request.user, 'membership', None)
46 def get_initial(self):
47 if self.request.user.is_authenticated and self.request.user.email:
49 'email': self.request.user.email,
52 def form_valid(self, form):
53 retval = super(JoinView, self).form_valid(form)
54 if self.request.user.is_authenticated:
55 form.instance.membership, created = models.Membership.objects.get_or_create(user=self.request.user)
59 def get_success_url(self):
60 return self.object.initiate_payment(self.request)
63 class ScheduleView(DetailView):
64 model = models.Schedule
65 slug_field = slug_url_kwarg = 'key'
66 template_name = 'club/schedule.html'
68 def post(self, request, key):
69 schedule = self.get_object()
70 return HttpResponseRedirect(schedule.initiate_payment(request))
74 def claim(request, key):
75 schedule = models.Schedule.objects.get(key=key, membership=None)
76 schedule.membership, created = models.Membership.objects.get_or_create(user=request.user)
78 return HttpResponseRedirect(schedule.get_absolute_url())
81 def cancel(request, key):
82 schedule = models.Schedule.objects.get(key=key)
83 schedule.is_cancelled = True
85 return HttpResponseRedirect(schedule.get_absolute_url())
88 class DummyPaymentView(TemplateView):
89 template_name = 'club/dummy_payment.html'
91 def get_context_data(self, key):
93 'schedule': models.Schedule.objects.get(key=key),
96 def post(self, request, key):
97 schedule = models.Schedule.objects.get(key=key)
98 schedule.create_payment()
99 return HttpResponseRedirect(schedule.get_absolute_url())
102 class PayUPayment(payu_views.Payment):
106 class PayURecPayment(payu_views.RecPayment):
107 form_class = PayUCardTokenForm
109 def get_schedule(self):
110 return get_object_or_404(models.Schedule, key=self.kwargs['key'])
113 pos_id = payure_method.pos_id
116 def get_success_url(self):
117 return self.get_schedule().pay(self.request)
120 class PayUNotifyView(payu_views.NotifyView):
121 order_model = models.PayUOrder
124 class MembershipView(UpdateView):
127 def get_success_url(self):
128 # TODO: get only current schedule if multiple.
129 return self.object.schedule_set.first().get_absolute_url()
131 def get_object(self):
132 return self.request.user.membership