1 # This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
4 from decimal import Decimal
5 from django import forms
6 from newsletter.forms import NewsletterForm
7 from . import models, payment_methods
8 from .payu.forms import CardTokenForm
11 class PayUCardTokenForm(CardTokenForm):
12 def get_queryset(self, view):
13 return view.get_schedule().payucardtoken_set
17 class DonationStep1Form(forms.ModelForm):
18 switch = forms.CharField()
19 single_amount = forms.IntegerField(required=False)
20 monthly_amount = forms.IntegerField(required=False)
21 single_amount_selected = forms.IntegerField(required=False)
22 monthly_amount_selected = forms.IntegerField(required=False)
23 custom_amount = forms.IntegerField(required=False)
25 amount = forms.IntegerField(required=False) # hidden
28 model = models.Schedule
34 def __init__(self, *args, referer=None, **kwargs):
35 self.referer = referer
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']
52 def save(self, *args, **kwargs):
53 self.instance.source = self.referer
54 return super().save(*args, **kwargs)
57 class DonationStep2Form(forms.ModelForm, NewsletterForm):
59 model = models.Schedule
61 'first_name', 'last_name',
64 'postal_code', 'postal_town', 'postal_country',
67 'amount': forms.HiddenInput,
68 'monthly': forms.HiddenInput,
71 def __init__(self, **kwargs):
72 super().__init__(**kwargs)
74 self.fields['first_name'].required = True
75 self.fields['last_name'].required = True
76 self.fields['phone'].required = True
79 for c in models.Consent.objects.filter(active=True).order_by('order'):
80 key = f'consent{c.id}'
81 self.fields[key] = forms.BooleanField(
86 c, key, (lambda k: lambda: self[k])(key)
89 def save(self, *args, **kwargs):
90 NewsletterForm.save(self, *args, **kwargs)
91 instance = super().save(*args, **kwargs)
94 for consent, key, consent_field in self.consent:
95 if self.cleaned_data[key]:
96 instance.consent.add(consent)