remember url on donation step 1
[wolnelektury.git] / src / club / forms.py
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.
3 #
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
9
10
11 class PayUCardTokenForm(CardTokenForm):
12     def get_queryset(self, view):
13         return view.get_schedule().payucardtoken_set
14
15
16
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)
24
25     amount = forms.IntegerField(required=False) # hidden
26
27     class Meta:
28         model = models.Schedule
29         fields = [
30             'amount',
31             'monthly'
32             ]
33
34     def __init__(self, *args, referer=None, **kwargs):
35         self.referer = referer
36         super().__init__(*args, **kwargs)
37         club = models.Club.objects.first()
38         if club is not None:
39             self.fields['custom_amount'].widget.attrs['min'] = club.min_amount
40
41     def clean(self):
42         state = {}
43         state['monthly'] = self.cleaned_data['switch'] == 'monthly'
44         which = 'monthly' if state['monthly'] else 'single'
45         state['amount'] = \
46             self.cleaned_data[f'{which}_amount'] or \
47             self.cleaned_data['custom_amount'] or \
48             self.cleaned_data[f'{which}_amount_selected']
49
50         return state
51
52     def save(self, *args, **kwargs):
53         self.instance.source = self.referer
54         return super().save(*args, **kwargs)
55
56
57 class DonationStep2Form(forms.ModelForm, NewsletterForm):
58     class Meta:
59         model = models.Schedule
60         fields = [
61             'first_name', 'last_name',
62             'email', 'phone',
63             'postal',
64             'postal_code', 'postal_town', 'postal_country',
65             ]
66         widgets = {
67             'amount': forms.HiddenInput,
68             'monthly': forms.HiddenInput,
69         }
70     
71     def __init__(self, **kwargs):
72         super().__init__(**kwargs)
73
74         self.fields['first_name'].required = True
75         self.fields['last_name'].required = True
76         self.fields['phone'].required = True
77         
78         self.consent = []
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(
82                 label=c.text,
83                 required=c.required
84             )
85             self.consent.append((
86                 c, key, (lambda k: lambda: self[k])(key)
87             ))
88
89     def save(self, *args, **kwargs):
90         NewsletterForm.save(self, *args, **kwargs)
91         instance = super().save(*args, **kwargs)
92
93         consents = []
94         for consent, key, consent_field in self.consent:
95             if self.cleaned_data[key]:
96                 instance.consent.add(consent)
97
98         return instance
99