-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathforms.py
52 lines (38 loc) · 1.59 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
from django import forms
from django.core.exceptions import ValidationError
from apps.ldap.utils import user_exists
from .utils import valid_password
usernameWhitelist = set(".-_")
def validate_username_chars(value):
if not all(c.isalnum() or c in usernameWhitelist for c in value):
raise ValidationError(
"Username must consist of alphanumeric characters and ., -, or _"
)
def validate_username_not_in_use(value):
if user_exists(value):
raise ValidationError(f"Username {value} is taken")
class RemoteEmailRequestForm(forms.Form):
email = forms.EmailField(label="Email")
class NewUserForm(forms.Form):
full_name = forms.CharField(label="Full Name")
student_id = forms.IntegerField(
label="Student ID", min_value=100000, max_value=10000000000
)
email = forms.EmailField()
username = forms.CharField(
validators=[validate_username_chars, validate_username_not_in_use]
)
password = forms.CharField(widget=forms.PasswordInput())
# enroll_jobs = forms.BooleanField(required=False, label="Jobs@ List Opt-in")
agree_rules = forms.BooleanField(required=True)
def clean(self):
form_data = super().clean()
password = form_data.get("password")
if not valid_password(password):
raise ValidationError("Password must meet requirements")
return form_data
class NewUserFormOfficerVerified(NewUserForm):
officer_username = forms.CharField(label="Officer Username")
officer_password = forms.CharField(
widget=forms.PasswordInput(), label="Officer Password"
)