X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/4b86e623b0ff7a5a53bdb29df06eab039ebe4e1e..65c9feda79d2b2c94cb7dcd561318a83fd796f0a:/src/club/forms.py diff --git a/src/club/forms.py b/src/club/forms.py index adf8959fc..b818d1c12 100644 --- a/src/club/forms.py +++ b/src/club/forms.py @@ -1,8 +1,8 @@ -# -*- coding: utf-8 +from decimal import Decimal from django import forms from . import models -from . import widgets -from .payment_methods import method_by_slug +from .payment_methods import method_by_slug, methods +from .payu.forms import CardTokenForm class ScheduleForm(forms.ModelForm): @@ -14,16 +14,34 @@ class ScheduleForm(forms.ModelForm): 'method': forms.RadioSelect, } - def __init__(self, *args, **kwargs): + def __init__(self, *args, request=None, **kwargs): super(ScheduleForm, self).__init__(*args, **kwargs) - self.fields['plan'].empty_label = None + self.request = request + self.plans = models.Plan.objects.all() + self.payment_methods = methods + self.fields['amount'].required = False def clean(self): cleaned_data = super(ScheduleForm, self).clean() + + if 'plan' in cleaned_data: + cleaned_data['amount'] = self.fields['amount'].clean( + self.request.POST['amount-{}'.format(cleaned_data['plan'].id)] + ) + + if cleaned_data['amount'] < cleaned_data['plan'].min_amount: + self.add_error( + 'amount', + 'Minimalna kwota dla tego planu to %d zł.' % cleaned_data['plan'].min_amount + ) + if 'method' in cleaned_data: method = method_by_slug[cleaned_data['method']] if method not in cleaned_data['plan'].payment_methods(): - self.add_error('method', 'Metoda płatności niedostępna dla tego planu.') - if cleaned_data['amount'] < cleaned_data['plan'].min_amount: - self.add_error('amount', 'Minimalna kwota dla tego planu to %d zł.' % cleaned_data['plan'].min_amount) + self.add_error('method', 'Wybrana metoda płatności nie jest dostępna dla tego planu.') + + +class PayUCardTokenForm(CardTokenForm): + def get_queryset(self, view): + return view.get_schedule().payucardtoken_set