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