Housekeeping.
[wolnelektury.git] / src / club / forms.py
1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
4 from decimal import Decimal
5 from django import forms
6 from . import models
7 from .payment_methods import method_by_slug, methods
8 from .payu.forms import CardTokenForm
9
10
11 class ScheduleForm(forms.ModelForm):
12     class Meta:
13         model = models.Schedule
14         fields = ['plan', 'method', 'amount', 'email']
15         widgets = {
16             'plan': forms.RadioSelect,
17             'method': forms.RadioSelect,
18         }
19
20     def __init__(self, *args, request=None, **kwargs):
21         super(ScheduleForm, self).__init__(*args, **kwargs)
22         self.request = request
23         self.plans = models.Plan.objects.all()
24         self.payment_methods = methods
25         self.fields['amount'].required = False
26
27     def clean(self):
28         cleaned_data = super(ScheduleForm, self).clean()
29
30         if 'plan' in cleaned_data:
31             cleaned_data['amount'] = self.fields['amount'].clean(
32                 self.request.POST['amount-{}'.format(cleaned_data['plan'].id)]
33             )
34
35             if cleaned_data['amount'] < cleaned_data['plan'].min_amount:
36                 self.add_error(
37                     'amount',
38                     'Minimalna kwota dla tego planu to %d zł.' % cleaned_data['plan'].min_amount
39                 )
40
41         if 'method' in cleaned_data:
42             method = method_by_slug[cleaned_data['method']]
43             if method not in cleaned_data['plan'].payment_methods():
44                 self.add_error('method', 'Wybrana metoda płatności nie jest dostępna dla tego planu.')
45
46
47
48 class PayUCardTokenForm(CardTokenForm):
49     def get_queryset(self, view):
50         return view.get_schedule().payucardtoken_set