X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/ca336bd1f9658cf713681d1412d4153e5c4d9c93..74f7584b18b4386433b4c02336f5adafcae530c5:/src/club/forms.py diff --git a/src/club/forms.py b/src/club/forms.py index bede0cb62..d1bebe85a 100644 --- a/src/club/forms.py +++ b/src/club/forms.py @@ -1,31 +1,57 @@ +# This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later. +# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. +# +from decimal import Decimal from django import forms -from . import models -from .payment_methods import method_by_slug +from django.utils.translation import ugettext as _ +from newsletter.forms import NewsletterForm +from . import models, payment_methods from .payu.forms import CardTokenForm -class ScheduleForm(forms.ModelForm): +class ScheduleForm(forms.ModelForm, NewsletterForm): + data_processing = '''Administratorem danych osobowych jest Fundacja Nowoczesna Polska (ul. Marszałkowska 84/92 lok. 125, 00-514 Warszawa). Podanie danych osobowych jest dobrowolne, ale konieczne do przeprowadzenia wpłaty. Dane są przetwarzane w zakresie niezbędnym do zaksięgowania darowizny i przekazywania Tobie powiadomień dotyczących wpłaty, a także wysyłania Tobie wiadomości mailowych promujących zbiórki i inne formy wsparcia Fundacji. W przypadku wyrażenia dodatkowej zgody adres e-mail zostanie wykorzystany także w zakresie niezbędnym do wysyłania newslettera odbiorcom. Osobom, których dane są zbierane, przysługuje prawo dostępu do treści swoich danych oraz ich poprawiania.''' + class Meta: model = models.Schedule - fields = ['plan', 'method', 'amount', 'email'] + fields = ['monthly', 'amount', 'email', 'method'] widgets = { - 'plan': forms.RadioSelect, - 'method': forms.RadioSelect, + 'amount': forms.HiddenInput, + 'monthly': forms.HiddenInput, + 'method': forms.HiddenInput, } - def __init__(self, *args, request=None, **kwargs): - super(ScheduleForm, self).__init__(*args, **kwargs) - self.request = request - self.fields['plan'].empty_label = None - - def clean(self): - cleaned_data = super(ScheduleForm, self).clean() - 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) + def __init__(self, referer=None, **kwargs): + self.referer = referer + super().__init__(**kwargs) + + 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 + + def clean_method(self): + value = self.cleaned_data['method'] + monthly = self.cleaned_data['monthly'] + for m in payment_methods.methods: + if m.slug == value: + if (monthly and m.is_recurring) or (not monthly and m.is_onetime): + return value + if monthly: + return payment_methods.recurring_payment_method.slug + else: + return payment_methods.single_payment_method.slug + + def save(self, *args, **kwargs): + NewsletterForm.save(self, *args, **kwargs) + self.instance.source = self.referer or '' + return super().save(*args, **kwargs) class PayUCardTokenForm(CardTokenForm):