-
Notifications
You must be signed in to change notification settings - Fork 2
/
forms.py
195 lines (145 loc) · 8.84 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
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
# -*- coding: utf-8 -*-
#
# forms.py
#
# Copyright 2021 Filippo Maria LAURIA <[email protected]>
#
# Computer and Communication Networks (CCN)
# Institute of Informatics and Telematics (IIT)
# Italian National Council of Research (CNR)
#
#
# This file is part of the Minimal FreeRADIUS GUI (MFG).
#
# The Minimal FreeRADIUS GUI (MFG) is free software: you can redistribute
# it and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# The Minimal FreeRADIUS GUI (MFG) is distributed in the hope that it will
# be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Minimal FreeRADIUS GUI (MFG).
# If not, see <http://www.gnu.org/licenses/>.
#
from flask_wtf import FlaskForm
from wtforms import IntegerField, BooleanField, PasswordField, StringField, HiddenField, SelectMultipleField, \
DateField, SelectField
from wtforms.validators import Length, Regexp, EqualTo, Email, InputRequired
from wtforms.widgets import EmailInput
from wtforms_sqlalchemy.orm import model_form
from mfg import db
from mfg.models import Organization, Domain
from mfg.helpers.validators import UserAlreadyExists, EmailAlreadyUsed
from mfg.helpers.widgets import DateInput
class SingleEmailForm(FlaskForm):
email = StringField('Email', [Length(min=4, max=64), Email(), InputRequired()],
render_kw={'class': 'form-control', 'placeholder': 'Email'}, widget=EmailInput())
class MultipleSelectForm(FlaskForm):
field = SelectMultipleField('Select multiple values', coerce=int, choices=[],
render_kw={'class': 'form-select'})
class UidForm(FlaskForm):
uid = HiddenField()
class LoginForm(FlaskForm):
username = StringField('Username', [Length(min=4, max=64)], render_kw={'class': 'form-control',
'placeholder': 'Username'})
password = PasswordField('Password', [Length(min=4, max=64)], render_kw={'class': 'form-control',
'placeholder': 'Password'})
remember = BooleanField('Remember me')
field_args = {
'shortname': {
'validators': [Length(min=2, max=64)], 'render_kw': {'class': 'form-control',
'placeholder': 'Organization Short Name'}
},
'fullname': {
'validators': [Length(min=4, max=253)], 'render_kw': {'class': 'form-control',
'placeholder': 'Organization Name'}
}
}
CreateOrganizationForm = model_form(Organization, base_class=FlaskForm, db_session=db.session,
field_args=field_args, exclude=['users'])
domain_name_pattern = '^([a-zA-Z0-9._-])+$'
domain_name_message = 'only upper and lower case letters, numbers and . _ - are allowed'
field_args = {
'domain_name': {
'validators': [Length(min=4, max=253), Regexp(domain_name_pattern, message=domain_name_message)],
'render_kw': {'class': 'form-control', 'placeholder': 'Domain name', 'pattern': domain_name_pattern,
'title': domain_name_message}
},
}
DomainForm = model_form(Domain, base_class=FlaskForm, db_session=db.session, field_args=field_args, exclude=['id'])
class PasswordForm(FlaskForm):
password1 = PasswordField('Password', [Length(min=4, max=64),
EqualTo('password2', message='Passwords must match')],
render_kw={'class': 'form-control', 'placeholder': 'Password'})
password2 = PasswordField('Repeat Password', [Length(min=4, max=64)],
render_kw={'class': 'form-control', 'placeholder': 'Repeat Password'})
class ChangePasswordForm(PasswordForm):
current_password = PasswordField('Current Password', [InputRequired(), Length(min=4, max=64)],
render_kw={'class': 'form-control', 'placeholder': 'Current Password'})
class UserPersonInfoForm(FlaskForm):
firstname = StringField('First Name', [Length(min=4, max=64), InputRequired()],
render_kw={'class': 'form-control', 'placeholder': 'First Name'})
lastname = StringField('Last Name', [Length(min=4, max=64), InputRequired()],
render_kw={'class': 'form-control', 'placeholder': 'Last Name'})
class AutoSignupForm(UserPersonInfoForm, PasswordForm):
pass
class SelectOrganizationForm(FlaskForm):
organization = SelectField('Organization', [InputRequired()], render_kw={'class': 'form-select'})
class AutoSignupWithOrganizationSelectionForm(AutoSignupForm, SelectOrganizationForm):
pass
class FirstAccessForm(UserPersonInfoForm, PasswordForm, CreateOrganizationForm):
email = StringField('Email', [Length(min=4, max=64), Email(), InputRequired(), EmailAlreadyUsed()],
render_kw={'class': 'form-control', 'placeholder': 'Email'}, widget=EmailInput())
username = StringField('Username', [Length(min=4, max=64), InputRequired(), UserAlreadyExists()],
render_kw={'class': 'form-control', 'placeholder': 'Username'})
class UserForm(UserPersonInfoForm, PasswordForm):
email = StringField('Email', [Length(min=4, max=64), Email(), InputRequired(), EmailAlreadyUsed()],
render_kw={'class': 'form-control', 'placeholder': 'Email'}, widget=EmailInput())
organization = SelectField('Organization', [InputRequired()])
username = StringField('Username', [Length(min=4, max=64), InputRequired(), UserAlreadyExists()],
render_kw={'class': 'form-control', 'placeholder': 'Username'})
registration_method = SelectField('Registration Method', [InputRequired()],
choices=[('password_by_admin', 'Specify a password for the new user'),
('link_via_mail', 'Send an activation link via mail')])
self_renew = BooleanField('Once expired, can password be self-renewed?')
expires_in = IntegerField('Password for this account expires every X days', [InputRequired()],
render_kw={'class': 'form-control', 'placeholder': 'X'})
auto_disable = BooleanField('Should this account automatically disable?')
disable_on = DateField('The account disables on', widget=DateInput(), render_kw={'class': 'form-control'})
class ChangeEmailForm(FlaskForm):
field = StringField('Email', [Length(min=4, max=64), Email(), InputRequired()],
render_kw={'class': 'form-control', 'placeholder': 'Email'}, widget=EmailInput(),
description='Please specify a valid email address')
class RecoverWithEmailForm(ChangeEmailForm):
pass
class RecoverWithUsernameForm(FlaskForm):
field = StringField('Username', [Length(min=4, max=64), InputRequired()],
render_kw={'class': 'form-control', 'placeholder': 'Username'},
description='Please specify a valid username')
class SearchFiltersForm(FlaskForm):
organizations = SelectMultipleField('Select multiple organizations', coerce=int, choices=[],
render_kw={'class': 'form-select'})
expired = BooleanField('Show expired', default=False)
disabled = BooleanField('Show disabled', default=False)
expiring_soon = BooleanField('Show expiring soon', default=False)
contact_persons = BooleanField('Show contact persons', default=False)
class SearchByUsernameForm(FlaskForm):
username = StringField('Username', [Length(min=4, max=64), InputRequired()],
render_kw={'class': 'form-control', 'placeholder': 'Username',
'list': 'search-by-username-datalist'})
class GroupForm(FlaskForm):
groupname = StringField('Group name', [Length(min=4, max=64), InputRequired()],
render_kw={'class': 'form-control', 'placeholder': 'group name'})
group_id = HiddenField()
class RadTableForm(FlaskForm):
attribute = StringField('Attribute', [Length(min=4, max=64), InputRequired()],
render_kw={'class': 'form-control', 'placeholder': 'attribute', 'list': 'attributes-list'})
op = StringField('Op', [Length(min=1, max=2), InputRequired()],
render_kw={'class': 'form-control', 'placeholder': 'op', 'list': 'operators-list'})
value = StringField('Value', [Length(min=0, max=253)],
render_kw={'class': 'form-control', 'placeholder': 'value'})
attribute_id = HiddenField()