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 recurring_payment_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_context_data(self, **kwargs):
52 c = super(JoinView, self).get_context_data(**kwargs)
53 c['membership'] = getattr(self.request.user, 'membership', None)
54 c['active_menu_item'] = 'club'
55 c['club'] = models.Club.objects.first()
57 c['ambassador'] = models.Ambassador.objects.all().order_by('?').first()
60 def get_initial(self):
61 if self.request.user.is_authenticated and self.request.user.email:
63 'email': self.request.user.email,
66 def form_valid(self, form):
67 retval = super(JoinView, self).form_valid(form)
68 if self.request.user.is_authenticated:
69 form.instance.membership, created = models.Membership.objects.get_or_create(user=self.request.user)
73 def get_success_url(self):
74 return self.object.initiate_payment(self.request)
77 @method_decorator(never_cache, name='dispatch')
78 class ScheduleView(DetailView):
79 model = models.Schedule
80 slug_field = slug_url_kwarg = 'key'
81 template_name = 'club/schedule.html'
83 def get_context_data(self, *args, **kwargs):
84 ctx = super().get_context_data(*args, **kwargs)
85 ctx['active_menu_item'] = 'club'
88 def post(self, request, key):
89 schedule = self.get_object()
90 return HttpResponseRedirect(schedule.initiate_payment(request))
94 def claim(request, key):
95 schedule = models.Schedule.objects.get(key=key, membership=None)
96 schedule.membership, created = models.Membership.objects.get_or_create(user=request.user)
98 return HttpResponseRedirect(schedule.get_absolute_url())
101 def cancel(request, key):
102 schedule = models.Schedule.objects.get(key=key)
103 schedule.is_cancelled = True
105 return HttpResponseRedirect(schedule.get_absolute_url())
108 class DummyPaymentView(TemplateView):
109 template_name = 'club/dummy_payment.html'
111 def get_context_data(self, key):
113 'schedule': models.Schedule.objects.get(key=key),
116 def post(self, request, key):
117 schedule = models.Schedule.objects.get(key=key)
118 schedule.create_payment()
119 return HttpResponseRedirect(schedule.get_absolute_url())
122 class PayUPayment(payu_views.Payment):
126 class PayURecPayment(payu_views.RecPayment):
127 form_class = PayUCardTokenForm
129 def get_schedule(self):
130 return get_object_or_404(models.Schedule, key=self.kwargs['key'])
133 pos_id = recurring_payment_method.pos_id
136 def get_success_url(self):
137 return self.get_schedule().pay(self.request)
140 class PayUNotifyView(payu_views.NotifyView):
141 order_model = models.PayUOrder
144 class MembershipView(UpdateView):
147 def get_success_url(self):
148 # TODO: get only current schedule if multiple.
149 return self.object.schedule_set.first().get_absolute_url()
151 def get_object(self):
152 return self.request.user.membership
155 class ScheduleThanksView(DetailView):
156 model = models.Schedule
157 slug_field = slug_url_kwarg = 'key'
158 template_name = 'club/thanks.html'
160 def get_context_data(self, *args, **kwargs):
161 ctx = super().get_context_data(*args, **kwargs)
162 ctx['active_menu_item'] = 'club'