X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/74f7584b18b4386433b4c02336f5adafcae530c5..HEAD:/src/club/forms.py diff --git a/src/club/forms.py b/src/club/forms.py index d1bebe85a..df92524b9 100644 --- a/src/club/forms.py +++ b/src/club/forms.py @@ -1,59 +1,99 @@ -# This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later. -# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. +# This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later. +# Copyright © Fundacja Wolne Lektury. See NOTICE for more information. # from decimal import Decimal from django import forms -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, 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 PayUCardTokenForm(CardTokenForm): + def get_queryset(self, view): + return view.get_schedule().payucardtoken_set + + + +class DonationStep1Form(forms.ModelForm): + switch = forms.CharField() + single_amount = forms.IntegerField(required=False) + monthly_amount = forms.IntegerField(required=False) + single_amount_selected = forms.IntegerField(required=False) + monthly_amount_selected = forms.IntegerField(required=False) + custom_amount = forms.IntegerField(required=False) + + amount = forms.IntegerField(required=False) # hidden class Meta: model = models.Schedule - fields = ['monthly', 'amount', 'email', 'method'] + fields = [ + 'amount', + 'monthly' + ] + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + club = models.Club.objects.first() + if club is not None: + self.fields['custom_amount'].widget.attrs['min'] = club.min_amount + + def clean(self): + state = {} + state['monthly'] = self.cleaned_data['switch'] == 'monthly' + which = 'monthly' if state['monthly'] else 'single' + state['amount'] = \ + self.cleaned_data[f'{which}_amount'] or \ + self.cleaned_data['custom_amount'] or \ + self.cleaned_data[f'{which}_amount_selected'] + + return state + + + +class DonationStep2Form(forms.ModelForm, NewsletterForm): + class Meta: + model = models.Schedule + fields = [ + 'first_name', 'last_name', + 'email', 'phone', + 'postal', + 'postal_code', 'postal_town', 'postal_country', + ] widgets = { 'amount': forms.HiddenInput, 'monthly': forms.HiddenInput, - 'method': forms.HiddenInput, } - + 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 - } + self.fields['first_name'].required = True + self.fields['last_name'].required = True + self.fields['phone'].required = True + + self.consent = [] + for c in models.Consent.objects.filter(active=True).order_by('order'): + key = f'consent{c.id}' + self.fields[key] = forms.BooleanField( + label=c.text, + required=c.required ) - 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 - + self.consent.append(( + c, key, (lambda k: lambda: self[k])(key) + )) + + + def save(self, *args, **kwargs): NewsletterForm.save(self, *args, **kwargs) self.instance.source = self.referer or '' - return super().save(*args, **kwargs) + instance = super().save(*args, **kwargs) + consents = [] + for consent, key, consent_field in self.consent: + if self.cleaned_data[key]: + instance.consent.add(consent) + + return instance -class PayUCardTokenForm(CardTokenForm): - def get_queryset(self, view): - return view.get_schedule().payucardtoken_set