1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 from django.conf import settings
5 from django.contrib.auth.decorators import login_required
6 from django.http import HttpResponseRedirect
7 from django.shortcuts import get_object_or_404, render
8 from django.utils.decorators import method_decorator
9 from django.views.decorators.cache import never_cache
10 from django.views.generic import FormView, CreateView, TemplateView, DetailView, UpdateView
11 from django.views import View
12 from .payu import POSS
13 from .payu import views as payu_views
14 from .forms import ScheduleForm, PayUCardTokenForm
16 from .helpers import get_active_schedule
17 from .payment_methods import payure_method
20 class ClubView(TemplateView):
21 template_name = 'club/index.html'
23 def get_context_data(self, *args, **kwargs):
24 ctx = super().get_context_data(*args, **kwargs)
25 ctx['active_menu_item'] = 'club'
29 class JoinView(CreateView):
30 form_class = ScheduleForm
31 template_name = 'club/membership_form.html'
34 return self.request.GET.get('app')
36 def get(self, request):
37 # TODO: configure as app-allowed hosts.
38 if settings.CLUB_APP_HOST and self.is_app() and request.META['HTTP_HOST'] != settings.CLUB_APP_HOST:
39 return HttpResponseRedirect("https://" + settings.CLUB_APP_HOST + request.get_full_path())
42 request.session['from_app'] = True
43 elif request.session and 'from_app' in request.session:
44 del request.session['from_app']
45 schedule = get_active_schedule(request.user)
46 if schedule is not None:
47 return HttpResponseRedirect(schedule.get_absolute_url())
49 return super(JoinView, self).get(request)
51 def get_form_kwargs(self):
52 kwargs = super().get_form_kwargs()
53 kwargs['request'] = self.request
56 def get_context_data(self, **kwargs):
57 c = super(JoinView, self).get_context_data(**kwargs)
58 c['membership'] = getattr(self.request.user, 'membership', None)
59 c['active_menu_item'] = 'club'
62 def get_initial(self):
63 if self.request.user.is_authenticated and self.request.user.email:
65 'email': self.request.user.email,
68 def form_valid(self, form):
69 retval = super(JoinView, self).form_valid(form)
70 if self.request.user.is_authenticated:
71 form.instance.membership, created = models.Membership.objects.get_or_create(user=self.request.user)
75 def get_success_url(self):
76 return self.object.initiate_payment(self.request)
79 @method_decorator(never_cache, name='dispatch')
80 class ScheduleView(DetailView):
81 model = models.Schedule
82 slug_field = slug_url_kwarg = 'key'
83 template_name = 'club/schedule.html'
85 def get_context_data(self, *args, **kwargs):
86 ctx = super().get_context_data(*args, **kwargs)
87 ctx['active_menu_item'] = 'club'
90 def post(self, request, key):
91 schedule = self.get_object()
92 return HttpResponseRedirect(schedule.initiate_payment(request))
96 def claim(request, key):
97 schedule = models.Schedule.objects.get(key=key, membership=None)
98 schedule.membership, created = models.Membership.objects.get_or_create(user=request.user)
100 return HttpResponseRedirect(schedule.get_absolute_url())
103 def cancel(request, key):
104 schedule = models.Schedule.objects.get(key=key)
105 schedule.is_cancelled = True
107 return HttpResponseRedirect(schedule.get_absolute_url())
110 class DummyPaymentView(TemplateView):
111 template_name = 'club/dummy_payment.html'
113 def get_context_data(self, key):
115 'schedule': models.Schedule.objects.get(key=key),
118 def post(self, request, key):
119 schedule = models.Schedule.objects.get(key=key)
120 schedule.create_payment()
121 return HttpResponseRedirect(schedule.get_absolute_url())
124 class PayUPayment(payu_views.Payment):
128 class PayURecPayment(payu_views.RecPayment):
129 form_class = PayUCardTokenForm
131 def get_schedule(self):
132 return get_object_or_404(models.Schedule, key=self.kwargs['key'])
135 pos_id = payure_method.pos_id
138 def get_success_url(self):
139 return self.get_schedule().pay(self.request)
142 class PayUNotifyView(payu_views.NotifyView):
143 order_model = models.PayUOrder
146 class MembershipView(UpdateView):
149 def get_success_url(self):
150 # TODO: get only current schedule if multiple.
151 return self.object.schedule_set.first().get_absolute_url()
153 def get_object(self):
154 return self.request.user.membership
157 class ScheduleThanksView(DetailView):
158 model = models.Schedule
159 slug_field = slug_url_kwarg = 'key'
160 template_name = 'club/thanks.html'
162 def get_context_data(self, *args, **kwargs):
163 ctx = super().get_context_data(*args, **kwargs)
164 ctx['active_menu_item'] = 'club'