-
Notifications
You must be signed in to change notification settings - Fork 0
/
forms.py
118 lines (101 loc) · 5.34 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
# django specific imports
from django import forms
# Third party library specific imports
import pycountry
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Div, Submit, Button, Field
from crispy_forms.bootstrap import FormActions
# project specific imports
from LoadIQWeb.models import Customer, Building
class AddUserForm(forms.Form):
user_group = forms.CharField(widget=forms.HiddenInput())
user_username = forms.RegexField(
label="Username",
max_length=30,
regex=r"^[\w.@+-]+$",
required=True,
)
user_first_name = forms.CharField(required=True, max_length=45, label=_("First Name"))
user_last_name = forms.CharField(required=True, max_length=45, label=_("Last Name"))
user_email = forms.EmailField(max_length=254, label=_("Email"))
user_password1 = forms.CharField(widget=forms.PasswordInput, label=_("Password"))
user_password2 = forms.CharField(widget=forms.PasswordInput, label=_("Confirm Password"))
user_customer = forms.CharField(widget=forms.HiddenInput())
helper = FormHelper()
helper.form_class = 'form-horizontal'
helper.form_id = 'addUserForm'
helper.form_action = '/users/add_user/'
helper.layout = Layout(
Div(Field('user_username', css_class='form-control'), css_class='form-group'),
Div(Field('user_first_name', css_class='form-control'), css_class='form-group'),
Div(Field('user_last_name', css_class='form-control'), css_class='form-group'),
Div(Field('user_email', css_class='form-control'), css_class='form-group'),
Div(Field('user_password1', css_class='form-control'), css_class='form-group'),
Div(Field('user_password2', css_class='form-control'), css_class='form-group'),
Field('user_customer'),
Field('user_group'),
Div(FormActions(
Button('add-user-submit', 'Submit', css_class="btn btn-shadow btn-danger center-block"),
), css_class='form-group')
)
def clean_user_username(self):
"""
Validate that the cp_username is alphanumeric and is not already in use.
"""
existing = User.objects.filter(username__iexact=self.cleaned_data['user_username'])
if existing.exists():
raise forms.ValidationError(_("A user with that username already exists."))
else:
return self.cleaned_data['user_username']
def clean(self):
"""
Verifiy that the values entered into the two password fields
match. Note that an error here will end up in
``non_field_errors()`` because it doesn't apply to a single
field.
"""
if 'user_password1' in self.cleaned_data and 'user_password2' in self.cleaned_data:
if self.cleaned_data['user_password1'] != self.cleaned_data['user_password2']:
raise forms.ValidationError(_("The two password fields didn't match."))
return self.cleaned_data
class AddBuildingAdmin(AddUserForm):
user_building = DynamicChoiceField(required=False, label=_("Building"),
widget=forms.Select(attrs={'id': 'selectBuildingForAdmin'}))
helper = FormHelper()
helper.form_class = 'form-horizontal'
helper.form_id = 'addBuildingAdminForm'
helper.form_action = '/users/add_building_admin/'
helper.layout = Layout(
Div(Field('user_building', css_class='form-control'), css_class='form-group'),
Div(Field('user_username', css_class='form-control', id='building_admin_user'), css_class='form-group'),
Div(Field('user_first_name', css_class='form-control', id='building_admin_first_name'), css_class='form-group'),
Div(Field('user_last_name', css_class='form-control', id='building_admin_last_name'), css_class='form-group'),
Div(Field('user_email', css_class='form-control', id='building_admin_email'), css_class='form-group'),
Div(Field('user_password1', css_class='form-control', id='building_admin_password1'), css_class='form-group'),
Div(Field('user_password2', css_class='form-control', id='building_admin_password2'), css_class='form-group'),
Field('user_customer', id='building_admin_customer'),
Field('user_group', id='building_admin_user_group'),
Div(FormActions(
Button('add-building-admin-submit', 'Submit', css_class="btn btn-shadow btn-danger center-block"),
), css_class='form-group')
)
def clean_user_username(self):
"""
Validate that the cp_username is alphanumeric and is not already in use.
"""
existing = User.objects.filter(username__iexact=self.cleaned_data['user_username'])
if existing.exists():
raise forms.ValidationError(_("A user with that username already exists."))
else:
return self.cleaned_data['user_username']
def clean(self):
"""
Verifiy that the values entered into the two password fields
match. Note that an error here will end up in
``non_field_errors()`` because it doesn't apply to a single
field.
"""
if 'user_password1' in self.cleaned_data and 'user_password2' in self.cleaned_data:
if self.cleaned_data['user_password1'] != self.cleaned_data['user_password2']:
raise forms.ValidationError(_("The two password fields didn't match."))
return self.cleaned_data