PayU payments working.
[wolnelektury.git] / src / club / forms.py
1 from django import forms
2 from . import models
3 from .payment_methods import method_by_slug 
4 from .payu.forms import CardTokenForm
5
6
7 class ScheduleForm(forms.ModelForm):
8     class Meta:
9         model = models.Schedule
10         fields = ['plan', 'method', 'amount', 'email']
11         widgets = {
12             'plan': forms.RadioSelect,
13             'method': forms.RadioSelect,
14         }
15
16     def __init__(self, *args, request=None, **kwargs):
17         super(ScheduleForm, self).__init__(*args, **kwargs)
18         self.request = request
19         self.fields['plan'].empty_label = None
20
21     def clean(self):
22         cleaned_data = super(ScheduleForm, self).clean()
23         if 'method' in cleaned_data:
24             method = method_by_slug[cleaned_data['method']]
25             if method not in cleaned_data['plan'].payment_methods():
26                 self.add_error('method', 'Metoda płatności niedostępna dla tego planu.')
27         if cleaned_data['amount'] < cleaned_data['plan'].min_amount:
28             self.add_error('amount', 'Minimalna kwota dla tego planu to %d zł.' % cleaned_data['plan'].min_amount)
29
30
31 class PayUCardTokenForm(CardTokenForm):
32     def get_queryset(self, view):
33         return view.get_schedule().payucardtoken_set