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, **kwargs):
35 super().__init__(*args, **kwargs)
36 club = models.Club.objects.first()
38 self.fields['custom_amount'].widget.attrs['min'] = club.min_amount
42 state['monthly'] = self.cleaned_data['switch'] == 'monthly'
43 which = 'monthly' if state['monthly'] else 'single'
45 self.cleaned_data[f'{which}_amount'] or \
46 self.cleaned_data['custom_amount'] or \
47 self.cleaned_data[f'{which}_amount_selected']
53 class DonationStep2Form(forms.ModelForm, NewsletterForm):
55 model = models.Schedule
57 'first_name', 'last_name',
60 'postal_code', 'postal_town', 'postal_country',
63 'amount': forms.HiddenInput,
64 'monthly': forms.HiddenInput,
67 def __init__(self, referer=None, **kwargs):
68 self.referer = referer
69 super().__init__(**kwargs)
71 self.fields['first_name'].required = True
72 self.fields['last_name'].required = True
73 self.fields['phone'].required = True
76 for c in models.Consent.objects.filter(active=True).order_by('order'):
77 key = f'consent{c.id}'
78 self.fields[key] = forms.BooleanField(
83 c, key, (lambda k: lambda: self[k])(key)
88 def save(self, *args, **kwargs):
89 NewsletterForm.save(self, *args, **kwargs)
90 self.instance.source = self.referer or ''
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)