-
Notifications
You must be signed in to change notification settings - Fork 1
/
forms.py
135 lines (111 loc) · 3.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
import re
from flask_wtf import FlaskForm
from flask_login import current_user
from wtforms import StringField, PasswordField, TextAreaField, BooleanField
from wtforms.validators import DataRequired, Email, Length, EqualTo, Regexp, NumberRange, ValidationError
from models import User
def name_exists(form, field):
if User.select().where(User.username==field.data).exists():
raise ValidationError("Username has already been taken.")
def email_exists(form, field):
if User.select().where(User.email==field.data).exists():
raise ValidationError("Email has already been registered.")
def name_change_check(form, field):
if field.data and User.select().where( (User.username==field.data) & (User.id != current_user.id) ).exists():
raise ValidationError("Username taken.")
def email_change_check(form, field):
if field.data and User.select().where( (User.email==field.data) & (User.id != current_user.id) ).exists():
raise ValidationError("Email already registered.")
# Password Strength not used.
def password_strength_check(form, field):
"""
A password is considered strong if it has:
1 digit or more
1 symbol or more
1 uppercase letter or more
1 lowercase letter or more
"""
digit_error = re.search(r"\d", field.data) is None
uppercase_error = re.search(r"[A-Z]", field.data) is None
lowercase_error = re.search(r"[a-z]", field.data) is None
symbol_error = re.search(r"\W", field.data) is None
if digit_error or uppercase_error or lowercase_error or symbol_error:
raise ValidationError("Password must contain uppercases, lowercases, digits and symbols")
class RegisterForm(FlaskForm):
username = StringField(
'Username',
validators=[
DataRequired(),
Regexp(
r'^[a-zA-Z0-9_]+$',
message="Username can only contain letters, numbers and underscores."
),
name_exists
])
email = StringField(
'Email',
validators=[ DataRequired(), Email(), email_exists ]
)
password = PasswordField(
'Password',
validators=[
DataRequired(),
Length(min=6, max=50, message="Password must be at least 6 characters long!"),
EqualTo('password2', message="Passwords do not match!"),
])
password2 = PasswordField(
'Confirm',
validators=[DataRequired()]
)
class LoginForm(FlaskForm):
username = StringField( 'Username', validators=[DataRequired()] )
password = PasswordField( 'Password', validators=[DataRequired()] )
class MemoForm(FlaskForm):
title = StringField('Day Title')
content = TextAreaField('How did it go?')
money_made = StringField( 'Income', validators=[Regexp(r'^[0-9]*$', message="Must be a number.")])
foods = StringField( 'Foods', validators=[
Regexp(
r'^([a-zA-Z0-9 ]+[,]?)*$',
message="Foods should be separated by a comma, containing letters and numbers only."
)
])
activities = StringField( 'Activities', validators=[
Regexp(
r'^([a-zA-Z0-9 ]+[,]?)*$',
message="Activities should be separated by a comma, containing letters and numbers only."
)
])
class SettingsForm(FlaskForm):
username = StringField(
'Username',
validators=[
Regexp(
r'^[a-zA-Z0-9_]*$',
message="Username can only contain letters, numbers and underscores."
),
name_change_check
])
email = StringField(
'Email',
validators=[ Email(), email_change_check ]
)
password = PasswordField(
'Old Password',
validators=[
DataRequired()
])
new_password = PasswordField(
'New Password',
validators=[
Length(min=6, max=50, message="Password must be at least 6 characters long!"),
EqualTo('confirm_password', message="Passwords do not match!")
])
confirm_password = PasswordField(
'Confirm Password'
)
class NewPwdForm(FlaskForm):
email = StringField(
'Email Address',
validators = [ DataRequired(), Email() ]
)