1 from decimal import Decimal
 
   2 from django import forms
 
   4 from .payment_methods import method_by_slug, methods
 
   5 from .payu.forms import CardTokenForm
 
   8 class ScheduleForm(forms.ModelForm):
 
  10         model = models.Schedule
 
  11         fields = ['plan', 'method', 'amount', 'email']
 
  13             'plan': forms.RadioSelect,
 
  14             'method': forms.RadioSelect,
 
  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
 
  25         cleaned_data = super(ScheduleForm, self).clean()
 
  27         if 'plan' in cleaned_data:
 
  28             cleaned_data['amount'] = self.fields['amount'].clean(
 
  29                 self.request.POST['amount-{}'.format(cleaned_data['plan'].id)]
 
  32             if cleaned_data['amount'] < cleaned_data['plan'].min_amount:
 
  35                     'Minimalna kwota dla tego planu to %d zł.' % cleaned_data['plan'].min_amount
 
  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.')
 
  45 class PayUCardTokenForm(CardTokenForm):
 
  46     def get_queryset(self, view):
 
  47         return view.get_schedule().payucardtoken_set