better check
[wolnelektury.git] / src / club / forms.py
index 1906bc9..ed18e22 100644 (file)
@@ -3,46 +3,35 @@
 #
 from decimal import Decimal
 from django import forms
+from django.utils.translation import ugettext as _
+from newsletter.forms import NewsletterForm
 from . import models
-from .payment_methods import method_by_slug, methods
 from .payu.forms import CardTokenForm
 
 
-class ScheduleForm(forms.ModelForm):
+class ScheduleForm(forms.ModelForm, NewsletterForm):
     class Meta:
         model = models.Schedule
-        fields = ['plan', 'method', 'amount', 'email']
+        fields = ['monthly', 'amount', 'email']
         widgets = {
-            'plan': forms.RadioSelect,
-            'method': forms.RadioSelect,
+            'amount': forms.HiddenInput,
+            'monthly': forms.HiddenInput,
         }
 
-    def __init__(self, *args, request=None, **kwargs):
-        super(ScheduleForm, self).__init__(*args, **kwargs)
-        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)]
+    def clean_amount(self):
+        value = self.cleaned_data['amount']
+        club = models.Club.objects.first()
+        if club and value < club.min_amount:
+            raise forms.ValidationError(
+                _('Minimal amount is %(amount)d PLN.') % {
+                    'amount': club.min_amount
+                }
             )
+        return value
 
-            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', 'Wybrana metoda płatności nie jest dostępna dla tego planu.')
-
+    def save(self, *args, **kwargs):
+        NewsletterForm.save(self, *args, **kwargs)
+        return super().save(*args, **kwargs)
 
 
 class PayUCardTokenForm(CardTokenForm):