1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
 
   2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 
   4 from decimal import Decimal
 
   5 from django import forms
 
   7 from .payment_methods import method_by_slug, methods
 
   8 from .payu.forms import CardTokenForm
 
  11 class ScheduleForm(forms.ModelForm):
 
  13         model = models.Schedule
 
  14         fields = ['plan', 'method', 'amount', 'email']
 
  16             'plan': forms.RadioSelect,
 
  17             'method': forms.RadioSelect,
 
  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
 
  28         cleaned_data = super(ScheduleForm, self).clean()
 
  30         if 'plan' in cleaned_data:
 
  31             cleaned_data['amount'] = self.fields['amount'].clean(
 
  32                 self.request.POST['amount-{}'.format(cleaned_data['plan'].id)]
 
  35             if cleaned_data['amount'] < cleaned_data['plan'].min_amount:
 
  38                     'Minimalna kwota dla tego planu to %d zł.' % cleaned_data['plan'].min_amount
 
  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.')
 
  48 class PayUCardTokenForm(CardTokenForm):
 
  49     def get_queryset(self, view):
 
  50         return view.get_schedule().payucardtoken_set