f6b3bf9e78559b5a81e49d57543f2cf66cd55637
[wolnelektury.git] / src / club / forms.py
1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. 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, **kwargs):
35         super().__init__(*args, **kwargs)
36         club = models.Club.objects.first()
37         if club is not None:
38             self.fields['custom_amount'].widget.attrs['min'] = club.min_amount
39
40     def clean(self):
41         state = {}
42         state['monthly'] = self.cleaned_data['switch'] == 'monthly'
43         which = 'monthly' if state['monthly'] else 'single'
44         state['amount'] = \
45             self.cleaned_data[f'{which}_amount'] or \
46             self.cleaned_data['custom_amount'] or \
47             self.cleaned_data[f'{which}_amount_selected']
48
49         return state
50
51
52
53 class DonationStep2Form(forms.ModelForm, NewsletterForm):
54     class Meta:
55         model = models.Schedule
56         fields = [
57             'first_name', 'last_name',
58             'email', 'phone',
59             'postal',
60             'postal_code', 'postal_town', 'postal_country',
61             ]
62         widgets = {
63             'amount': forms.HiddenInput,
64             'monthly': forms.HiddenInput,
65         }
66     
67     def __init__(self, referer=None, **kwargs):
68         self.referer = referer
69         super().__init__(**kwargs)
70
71         self.fields['first_name'].required = True
72         self.fields['last_name'].required = True
73         self.fields['phone'].required = True
74         
75         self.consent = []
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(
79                 label=c.text,
80                 required=c.required
81             )
82             self.consent.append((
83                 c, key, (lambda k: lambda: self[k])(key)
84             ))
85
86
87
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)
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