-
Notifications
You must be signed in to change notification settings - Fork 1
/
forms.py
executable file
·148 lines (122 loc) · 4.31 KB
/
forms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from django import forms
from django.utils import timezone
from plugins.consortial_billing import models as supporter_models, logic
class BaseBandForm(forms.ModelForm):
class Meta:
model = supporter_models.Band
fields = [
'country',
'currency',
'size',
'level',
]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['country'].required = True
self.fields['currency'].required = True
self.fields['size'].required = True
self.fields['level'].required = True
def save(self, commit=True):
"""
Populates a band object matching the form input.
If there is already a matching object, it is returned
rather than a new one to prevent duplicate bands from proliferating.
"""
band = super().save(commit=False)
band.billing_agent = logic.determine_billing_agent(band.country)
if band.category == 'calculated':
# For calculated bands, we try to avoid duplicates to make management easier
band.fee, band.warnings = band.calculate_fee()
matches = supporter_models.Band.objects.filter(
level=band.level,
size=band.size,
country=band.country,
currency=band.currency,
billing_agent=band.billing_agent,
category=band.category,
fee=band.fee,
warnings=band.warnings,
datetime__year=timezone.now().year,
)
if matches:
band = matches.latest()
if commit:
band.save()
return band
class BandForm(BaseBandForm):
class Meta(BaseBandForm.Meta):
fields = [
'country',
'currency',
'size',
'level',
'fee',
'category',
]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['fee'].required = False
self.fields['fee'].disabled = True
class EditBandForm(BaseBandForm):
class Meta(BaseBandForm.Meta):
fields = [
'level',
'size',
'country',
'currency',
'fee',
'category',
'billing_agent',
'warnings',
]
help_texts = {
'billing_agent': 'To change the billing agent, select the appropriate country and currency.',
'fee': 'To edit this field, set the band category to special. To auto-calculate it, set the band category to calculated.',
'category': 'Whether the fee is calculated or special for this supporter',
}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['billing_agent'].disabled = True
self.fields['warnings'].disabled = True
self.fields['fee'].required = True
all_category_choices = supporter_models.BAND_CATEGORY_CHOICES
choices_without_base = [c for c in all_category_choices if c[0] != 'base']
self.fields['category'].widget.choices = choices_without_base
if self.instance:
if self.instance.category == 'special':
self.fields['fee'].disabled = False
elif self.instance.category == 'calculated':
self.fields['fee'].disabled = True
self.fields['fee'].required = False
class SupporterSignupForm(forms.ModelForm):
class Meta:
model = supporter_models.Supporter
fields = [
'name',
'ror',
'address',
'postal_code',
'display',
]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['name'].required = True
class EditSupporterForm(SupporterSignupForm):
class Meta(SupporterSignupForm.Meta):
fields = [
'name',
'ror',
'address',
'postal_code',
'display',
'active',
'internal_notes',
]
class AccountAdminSearchForm(forms.Form):
q = forms.CharField(
required=False,
widget=forms.TextInput(
attrs={'type': 'search'}
),
label='Search existing accounts to add a new contact',
)