dd088f730f2206863c88cd98ddee99d2ac1b21fa
[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 django.utils.translation import gettext as _
7 from newsletter.forms import NewsletterForm
8 from . import models, payment_methods
9 from .payu.forms import CardTokenForm
10
11
12 class PayUCardTokenForm(CardTokenForm):
13     def get_queryset(self, view):
14         return view.get_schedule().payucardtoken_set
15
16
17
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)
25
26     amount = forms.IntegerField(required=False) # hidden
27
28     class Meta:
29         model = models.Schedule
30         fields = [
31             'amount',
32             'monthly'
33             ]
34
35     def __init__(self, *args, **kwargs):
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
53
54 class DonationStep2Form(forms.ModelForm, NewsletterForm):
55     class Meta:
56         model = models.Schedule
57         fields = [
58             'first_name', 'last_name',
59             'email', 'phone',
60             'postal',
61             'postal_code', 'postal_town', 'postal_country',
62             ]
63         widgets = {
64             'amount': forms.HiddenInput,
65             'monthly': forms.HiddenInput,
66         }
67     
68     def __init__(self, referer=None, **kwargs):
69         self.referer = referer
70         super().__init__(**kwargs)
71
72         self.fields['first_name'].required = True
73         self.fields['last_name'].required = True
74         self.fields['phone'].required = True
75         
76         self.consent = []
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(
80                 label=c.text,
81                 required=c.required
82             )
83             self.consent.append((
84                 c, key, (lambda k: lambda: self[k])(key)
85             ))
86
87
88
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)
93
94         consents = []
95         for consent, key, consent_field in self.consent:
96             if self.cleaned_data[key]:
97                 instance.consent.add(consent)
98
99         return instance
100