-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathforms.py
More file actions
344 lines (278 loc) · 11.1 KB
/
forms.py
File metadata and controls
344 lines (278 loc) · 11.1 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import re
from django import forms
from django.contrib.auth import get_user_model
from django.core.files.images import get_image_dimensions
from django.forms import FileField, Form, ModelForm, ValidationError
from django.urls import reverse_lazy
from django.utils import dateformat, timezone
from django.utils.html import format_html
from django.utils.safestring import mark_safe
from django.utils.text import format_lazy
import html5.forms.widgets as html5_widgets
from dateutil.relativedelta import relativedelta
from coderdojochi.models import CDCUser, Donation, Guardian, Mentor, RaceEthnicity, Session, Student
class CDCForm(Form):
# strip leading or trailing whitespace
def _clean_fields(self):
for name, field in list(self.fields.items()):
# value_from_datadict() gets the data from the data dictionaries.
# Each widget type knows how to retrieve its own data, because some
# widgets split data over several HTML fields.
value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name))
try:
if isinstance(field, FileField):
initial = self.initial.get(name, field.initial)
value = field.clean(value, initial)
else:
if isinstance(value, str):
value = field.clean(value.strip())
else:
value = field.clean(value)
self.cleaned_data[name] = value
if hasattr(self, f"clean_{name}"):
value = getattr(self, f"clean_{name}")()
self.cleaned_data[name] = value
except ValidationError as e:
self.add_error(name, e)
class CDCModelForm(ModelForm):
# strip leading or trailing whitespace
def _clean_fields(self):
for name, field in list(self.fields.items()):
# value_from_datadict() gets the data from the data dictionaries.
# Each widget type knows how to retrieve its own data, because some
# widgets split data over several HTML fields.
value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name))
try:
if isinstance(field, FileField):
initial = self.initial.get(name, field.initial)
value = field.clean(value, initial)
else:
if isinstance(value, str):
# regex normalizes carriage return
# and cuts them to two at most
value = re.sub(r"\r\n", "\n", value)
value = re.sub(r"\n{3,}", "\n\n", value)
value = field.clean(value.strip())
else:
value = field.clean(value)
self.cleaned_data[name] = value
if hasattr(self, f"clean_{name}"):
value = getattr(self, f"clean_{name}")()
self.cleaned_data[name] = value
except ValidationError as e:
self.add_error(name, e)
class Meta:
model = CDCUser
fields = ("first_name", "last_name")
class SignupForm(forms.Form):
first_name = forms.CharField(max_length=30)
last_name = forms.CharField(max_length=30)
class Meta:
model = get_user_model()
def save(self, user):
user.first_name = self.cleaned_data["first_name"]
user.last_name = self.cleaned_data["last_name"]
user.save()
class MentorForm(CDCModelForm):
bio = forms.CharField(
widget=forms.Textarea(attrs={"placeholder": "Short Bio", "class": "form-control", "rows": 4}),
label="Short Bio",
required=False,
)
gender = forms.CharField(
widget=forms.TextInput(attrs={"placeholder": "", "class": "form-control"}), label="Gender", required=True
)
race_ethnicity = forms.ModelMultipleChoiceField(
widget=forms.SelectMultiple, queryset=RaceEthnicity.objects.filter(is_visible=True), required=True
)
birthday = forms.CharField(widget=html5_widgets.DateInput(attrs={"class": "form-control"}), required=True)
work_place = forms.CharField(
widget=forms.TextInput(attrs={"placeholder": "", "class": "form-control"}), label="Work Place", required=False
)
phone = forms.CharField(
widget=forms.TextInput(attrs={"placeholder": "", "class": "form-control"}), label="Phone", required=False
)
home_address = forms.CharField(
widget=forms.TextInput(attrs={"placeholder": "", "class": "form-control"}), label="Home Address", required=False
)
class Meta:
model = Mentor
#"avatar",
fields = ("bio", "gender", "race_ethnicity", "birthday", "phone", "home_address", "work_place")
# def clean_avatar(self):
# avatar = self.cleaned_data["avatar"]
# if avatar is None:
# return avatar
# try:
# w, h = get_image_dimensions(avatar)
# # validate dimensions
# max_width = max_height = 1000
# if w > max_width or h > max_height:
# raise forms.ValidationError(f"Please use an image that is {max_width} x {max_height}px or smaller.")
# min_width = min_height = 500
# if w < min_width or h < min_height:
# raise forms.ValidationError(f"Please use an image that is {min_width} x {min_height}px or larger.")
# # validate content type
# main, sub = avatar.content_type.split("/")
# if not (main == "image" and sub in ["jpeg", "pjpeg", "gif", "png"]):
# raise forms.ValidationError("Please use a JPEG, GIF or PNG image.")
# # validate file size
# if len(avatar) > (2000 * 1024):
# raise forms.ValidationError("Avatar file size may not exceed 2MB.")
# except AttributeError:
# """
# Handles case when we are updating the user profile
# and do not supply a new avatar
# """
# return avatar
class GuardianForm(CDCModelForm):
phone = forms.CharField(
widget=forms.TextInput(attrs={"placeholder": "Phone Number", "class": "form-control"}), label="Phone Number"
)
zip = forms.CharField(
widget=forms.TextInput(attrs={"placeholder": "Zip Code", "class": "form-control"}), label="Zip Code"
)
gender = forms.CharField(
widget=forms.TextInput(attrs={"placeholder": "", "class": "form-control"}), label="Gender", required=True
)
race_ethnicity = forms.ModelMultipleChoiceField(
widget=forms.SelectMultiple, queryset=RaceEthnicity.objects.filter(is_visible=True), required=True
)
birthday = forms.CharField(widget=html5_widgets.DateInput(attrs={"class": "form-control"}), required=True)
class Meta:
model = Guardian
fields = ("phone", "zip", "gender", "race_ethnicity", "birthday")
class StudentForm(CDCModelForm):
PUBLIC = "PU"
CHARTER = "CH"
PRIVATE = "PR"
HOMESCHOOL = "HM"
SCHOOL_TYPE_CHOICES = [
(PUBLIC, "Public"),
(CHARTER, "Charter"),
(PRIVATE, "Private"),
(HOMESCHOOL, "Homeschool"),
]
first_name = forms.CharField(
widget=forms.TextInput(
attrs={
"placeholder": "Jane",
"class": "form-control",
},
),
label="First Name",
)
last_name = forms.CharField(
widget=forms.TextInput(
attrs={
"placeholder": "Doe",
"class": "form-control",
},
),
label="Last Name",
)
gender = forms.CharField(
widget=forms.TextInput(
attrs={
"placeholder": "",
"class": "form-control",
},
),
label="Gender",
)
school_name = forms.CharField(
widget=forms.TextInput(
attrs={
"class": "form-control",
},
),
label="School Name",
required=False,
)
school_type = forms.ChoiceField(
widget=forms.RadioSelect,
choices=SCHOOL_TYPE_CHOICES,
required=False,
)
race_ethnicity = forms.ModelMultipleChoiceField(
widget=forms.CheckboxSelectMultiple,
queryset=RaceEthnicity.objects.filter(is_visible=True),
required=False,
)
birthday = forms.CharField(
widget=html5_widgets.DateInput(
attrs={
"class": "form-control",
"min": dateformat.format(timezone.now() - relativedelta(years=19), "Y-m-d"),
"max": dateformat.format(timezone.now() - relativedelta(years=5), "Y-m-d"),
}
),
)
medications = forms.CharField(
widget=forms.Textarea(
attrs={
"placeholder": "List any medications currently being taken.",
"class": "form-control hidden",
"rows": 5,
}
),
label=format_html(
"{0} {1}",
"Medications",
mark_safe('<span class="btn btn-xs btn-link js-expand-student-form">expand</span>'),
),
required=False,
)
medical_conditions = forms.CharField(
widget=forms.Textarea(
attrs={
"placeholder": "List any medical conditions.",
"class": "form-control hidden",
"rows": 5,
},
),
label=format_html(
"{0} {1}",
"Medical Conditions",
mark_safe('<span class="btn btn-xs btn-link js-expand-student-form">expand</span>'),
),
required=False,
)
photo_release = forms.BooleanField(
widget=forms.CheckboxInput(
attrs={
"required": "required",
},
),
label=(
"I hereby give permission to We All Code to use the "
"student's image and/or likeness in promotional materials."
),
)
consent = forms.BooleanField(
widget=forms.CheckboxInput(
attrs={
"required": "required",
},
),
label=format_lazy(
"I hereby give consent for the student signed up above to participate in We All Code as per the "
'<a href="{0}">terms</a>.',
reverse_lazy("weallcode-privacy"),
),
)
class Meta:
model = Student
exclude = ("guardian", "created_at", "updated_at", "is_active")
class ContactForm(CDCForm):
name = forms.CharField(max_length=100, label="Your name")
email = forms.EmailField(max_length=200, label="Your email address")
message = forms.CharField(widget=forms.Textarea, label="Your message")
human = forms.CharField(max_length=100, label=False, required=False)
class DonationForm(ModelForm):
session = forms.ModelChoiceField(queryset=Session.objects.all(), widget=forms.HiddenInput(), required=True)
user = forms.ModelChoiceField(queryset=CDCUser.objects.all(), required=True)
amount = forms.CharField(label="Amount (dollars)")
class Meta:
model = Donation
fields = ["session", "user", "amount"]