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
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 if not schedule.is_active():
71 return HttpResponseRedirect(schedule.initiate_payment(request))
73 return HttpResponseRedirect(schedule.get_absolute_url())
77 def claim(request, key):
78 schedule = models.Schedule.objects.get(key=key, membership=None)
79 schedule.membership, created = models.Membership.objects.get_or_create(user=request.user)
81 return HttpResponseRedirect(schedule.get_absolute_url())
84 def cancel(request, key):
85 schedule = models.Schedule.objects.get(key=key)
86 schedule.is_cancelled = True
88 return HttpResponseRedirect(schedule.get_absolute_url())
91 class DummyPaymentView(TemplateView):
92 template_name = 'club/dummy_payment.html'
94 def get_context_data(self, key):
96 'schedule': models.Schedule.objects.get(key=key),
99 def post(self, request, key):
100 schedule = models.Schedule.objects.get(key=key)
101 schedule.create_payment()
102 return HttpResponseRedirect(schedule.get_absolute_url())
105 class PayUPayment(payu_views.Payment):
109 class PayURecPayment(payu_views.RecPayment):
110 form_class = PayUCardTokenForm
112 def get_schedule(self):
113 return get_object_or_404(models.Schedule, key=self.kwargs['key'])
116 pos_id = payure_method.pos_id
119 def get_success_url(self):
120 return self.get_schedule().pay(self.request)
123 class PayUNotifyView(payu_views.NotifyView):
124 order_model = models.PayUOrder