forked from CCExtractor/sample-platform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforms.py
executable file
·219 lines (168 loc) · 7.81 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
"""contains all the forms related to authentication and account functionality."""
from __future__ import annotations
from typing import Any, Callable, Optional, Type
from flask_wtf import FlaskForm
from wtforms import PasswordField, SelectField, StringField, SubmitField
from wtforms.fields.html5 import EmailField
from wtforms.fields.simple import PasswordField
from wtforms.validators import DataRequired, Email, ValidationError
import mod_auth.models
from mod_auth.models import Role, User
def unique_username(form, field) -> None:
"""
Check if a user already exists with this name.
:param form: The form which is being passed in
:type form: Form
:param field: The data value for the 'name' inserted by new User
:type field : StringField
"""
user = User.query.filter(User.name == field.data).first()
if user is not None:
raise ValidationError('There is already a user with this name')
def valid_password(form: CompleteSignupForm, field: PasswordField) -> None:
"""
Check for validity of a password.
:param form: The form which is being passed in
:type form: Form
:param field: The data value for the 'password' inserted by User
:type field : PasswordField
"""
from run import config
min_pwd_len = int(config['MIN_PWD_LEN'])
max_pwd_len = int(config['MAX_PWD_LEN'])
pass_size = len(field.data)
if pass_size == 0:
raise ValidationError('new password cannot be empty')
if pass_size < min_pwd_len or pass_size > max_pwd_len:
raise ValidationError(
f'Password needs to be between {min_pwd_len} and {max_pwd_len} characters long (you entered {pass_size})'
)
def email_not_in_use(has_user_field: bool = False) -> Callable:
"""
Check if the passed email is already in use.
:param has_user_field : Whether an email has an existing User (False by
default)
:type has_user_field : boolean
"""
def _email_not_in_use(form, field):
user_id = -1 if not has_user_field else form.user.id
user = User.query.filter(User.email == field.data).first()
if user is not None and user.id != user_id and len(field.data) > 0:
raise ValidationError('This address is already in use')
return _email_not_in_use
class LoginForm(FlaskForm):
"""Render form for User to enter Log in credentials."""
email = EmailField('Email', [
DataRequired(message='Email address is not filled in'),
Email(message='Entered value is not a valid email address')
])
password = PasswordField('Password', [DataRequired(message='Password cannot be empty.')])
submit = SubmitField('Login')
class SignupForm(FlaskForm):
"""Sign up form for new Users."""
email = EmailField('Email', [
DataRequired(message='Email address is not filled in'),
Email(message='Entered value is not a valid email address')
])
submit = SubmitField('Register')
class DeactivationForm(FlaskForm):
"""Deactivate existing account."""
submit = SubmitField('Deactivate account')
class RoleChangeForm(FlaskForm):
"""Change the Role."""
role = SelectField('Select a role', [DataRequired(message='Role is not filled in.')], coerce=str)
submit = SubmitField('Change role')
class CompleteSignupForm(FlaskForm):
"""Complete Sign up form for new users."""
name = StringField('Name', [DataRequired(message='Name is not filled in.')])
password = PasswordField('Password', [DataRequired(message='Password is not filled in.'), valid_password])
password_repeat = PasswordField('Repeat password', [DataRequired(message='Repeated password is not filled in.')])
submit = SubmitField('Register')
@staticmethod
def validate_password_repeat(form: CompleteSignupForm, field: PasswordField) -> None:
"""
Validate if the repeated password is the same as 'password'.
:param form: The form which is being passed in
:type form: CompleteSignupForm
:param field : The data value for the 'password' entered by User
:type field : PasswordField
"""
if field.data != form.password.data:
raise ValidationError('The password needs to match the new password')
class AccountForm(FlaskForm):
"""Form for editing current Account."""
def __init__(self, formdata=None, obj=None, prefix='', *args, **kwargs) -> None:
super(AccountForm, self).__init__(formdata=formdata, obj=obj, prefix=prefix, *args, **kwargs)
self.user = obj
current_password = PasswordField('Current password', [DataRequired(message='current password cannot be empty')])
new_password = PasswordField('New password')
new_password_repeat = PasswordField('Repeat new password')
name = StringField('Name', [DataRequired(message='Name is not filled in.')])
email = EmailField('Email', [
DataRequired(message='email address is not filled in'),
Email(message='entered value is not a valid email address'),
email_not_in_use(True)
])
submit = SubmitField('Update account')
@staticmethod
def validate_current_password(form, field) -> None:
"""
Validate current password entered with the password stored in database.
:param form: The form which is being passed in
:type form: AccountForm
:param field: The data value for the 'password' entered by User
:type field : PasswordField
"""
if form.user is None:
raise ValidationError('User instance not passed to form validation')
if not form.user.is_password_valid(field.data):
raise ValidationError('Invalid password')
@staticmethod
def validate_new_password(form, field) -> None:
"""
Validate the new password entered.
:param form: The form which is being passed in
:type form: AccountForm
:param field: The data value for the 'password' entered by User
:type field : PasswordField
"""
if len(field.data) == 0 and len(form.new_password_repeat.data) == 0:
return
valid_password(form, field)
@staticmethod
def validate_new_password_repeat(form, field) -> None:
"""
Validate new password repeat and checks if it matches 'new_password'.
:param form: The form which is being passed in
:type form: AccountForm
:param field: The data value for the 'password' entered by User
:type field : PasswordField
"""
if form.email is not None:
if len(field.data) == 0 and len(form.new_password.data) == 0:
return
if field.data != form.new_password.data:
raise ValidationError('The password needs to match the new password')
class ResetForm(FlaskForm):
"""Form for resetting password."""
email = EmailField('Email', [
DataRequired(message='Email address is not filled in'),
Email(message='Entered value is not a valid email address')
])
submit = SubmitField('Request reset instructions')
class CompleteResetForm(FlaskForm):
"""Reset password form after clicking on the link in the email."""
password = PasswordField('Password', [DataRequired(message='Password is not filled in.'), valid_password])
password_repeat = PasswordField('Repeat password', [DataRequired(message='Repeated password is not filled in.')])
submit = SubmitField('Reset password')
@staticmethod
def validate_password_repeat(form, field) -> None:
"""
Validate new password repeat and checks if it matches 'password'.
:param form: The form which is being passed in
:type form: CompleteResetForm
:param field: The data value for the 'password' entered by User
:type field : PasswordField
"""
if field.data != form.password.data:
raise ValidationError('The password needs to match the new password')