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
6 from django.utils.translation import gettext as _
7 from newsletter.forms import NewsletterForm
8 from . import models, payment_methods
9 from .payu.forms import CardTokenForm
12 class PayUCardTokenForm(CardTokenForm):
13 def get_queryset(self, view):
14 return view.get_schedule().payucardtoken_set
18 class DonationStep1Form(forms.ModelForm):
19 switch = forms.CharField()
20 single_amount = forms.IntegerField(required=False)
21 monthly_amount = forms.IntegerField(required=False)
22 single_amount_selected = forms.IntegerField(required=False)
23 monthly_amount_selected = forms.IntegerField(required=False)
24 custom_amount = forms.IntegerField(required=False)
26 amount = forms.IntegerField(required=False) # hidden
29 model = models.Schedule
35 def __init__(self, *args, **kwargs):
36 super().__init__(*args, **kwargs)
37 club = models.Club.objects.first()
39 self.fields['custom_amount'].widget.attrs['min'] = club.min_amount
43 state['monthly'] = self.cleaned_data['switch'] == 'monthly'
44 which = 'monthly' if state['monthly'] else 'single'
46 self.cleaned_data[f'{which}_amount'] or \
47 self.cleaned_data['custom_amount'] or \
48 self.cleaned_data[f'{which}_amount_selected']
54 class DonationStep2Form(forms.ModelForm, NewsletterForm):
56 model = models.Schedule
58 'first_name', 'last_name',
61 'postal_code', 'postal_town', 'postal_country',
64 'amount': forms.HiddenInput,
65 'monthly': forms.HiddenInput,
68 def __init__(self, referer=None, **kwargs):
69 self.referer = referer
70 super().__init__(**kwargs)
72 self.fields['first_name'].required = True
73 self.fields['last_name'].required = True
74 self.fields['phone'].required = True
77 for c in models.Consent.objects.filter(active=True).order_by('order'):
78 key = f'consent{c.id}'
79 self.fields[key] = forms.BooleanField(
84 c, key, (lambda k: lambda: self[k])(key)
89 def save(self, *args, **kwargs):
90 NewsletterForm.save(self, *args, **kwargs)
91 self.instance.source = self.referer or ''
92 instance = super().save(*args, **kwargs)
95 for consent, key, consent_field in self.consent:
96 if self.cleaned_data[key]:
97 instance.consent.add(consent)