-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathforms.py
More file actions
135 lines (123 loc) · 6.74 KB
/
forms.py
File metadata and controls
135 lines (123 loc) · 6.74 KB
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
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField, BooleanField,\
RadioField, TextAreaField, HiddenField
from wtforms.validators import DataRequired, Length, Email, EqualTo,\
Optional, Regexp
from flask_wtf.file import FileField, FileAllowed, FileRequired
from config import text_regex
# The following class creates the a new user registration form
# and defines validation rules for each of its fields
class RegistrationForm(FlaskForm):
username = StringField('Username',
validators=[
DataRequired(),
Length(min=1, max=30),
Regexp(text_regex, message="Username cannot "
"start with a space")
])
email = StringField('Email', validators=[DataRequired(), Email()])
password = PasswordField('Password',
validators=[DataRequired(), Length(min=8)])
confirm_password = PasswordField('Confirm Password',
validators=[
DataRequired(),
EqualTo('password')])
submit = SubmitField('Create an Account')
# The following class creates the form to log into the application
# and defines validation rules for each of its fields
class LoginForm(FlaskForm):
email = StringField('Email',
validators=[DataRequired(), Email()])
password = PasswordField('Password',
validators=[DataRequired(), Length(min=8)])
remember = BooleanField('Remember Me')
submit = SubmitField('Login')
# The following class creates the form that will hold the content
# of each entry. Its validation rules are defined here.
class EntryForm(FlaskForm):
name = TextAreaField('Name',
render_kw={"rows": 1,
"spellcheck": "false",
"maxlength": 30,
"data-expandable": "true"},
validators=[Regexp(text_regex, message="Name cannot start with a space"),
DataRequired(),
Length(min=1, max=30)])
description = TextAreaField('Description',
render_kw={"spellcheck": "false",
"maxlength": 2000,
"rows": 1,
"data-expandable": "True"},
validators=[Regexp(text_regex,
message=("Description cannot start"
" with a space or line break")),
DataRequired(),
Length(min=1, max=2000)])
rating = RadioField('Rating',
validators=[DataRequired()],
choices=[('5', 'Outstanding'),
('4', 'Very Good'),
('3', 'Good'),
('2', 'Poor'),
('1', 'Very Poor')])
is_fav = BooleanField('Favorite')
image = FileField('Image',
render_kw={"accept": "image/*"},
validators=[FileAllowed(['jpg', 'gif', 'png', 'jpeg'],
'Images only!')])
tags = StringField('Tags', validators=[Length(min=0, max=100)])
hidden_tags = HiddenField('Hidden Tags',
validators=[Length(min=0, max=100)])
hidden_id = HiddenField('Hidden Id')
# The following class creates the form that allows users to add
# a new review and defines validation rules for each of its fields
class NewEntryForm(FlaskForm):
name = TextAreaField('Name',
render_kw={"rows": 1, "spellcheck": "false", "maxlength": 30},
validators=[Regexp(text_regex,
message=("Name cannot start with a "
"space or line break")),
DataRequired(),
Length(min=1, max=30)])
description = TextAreaField('Description',
render_kw={"rows": 5, "spellcheck": "false", "maxlength": 2000},
validators=[Regexp(text_regex,
message="Description cannot start with a "
"space or line break"),
DataRequired(),
Length(min=1, max=2000)])
rating = RadioField('Rating',
validators=[DataRequired()],
choices=[('5', 'Outstanding'),
('4', 'Very Good'),
('3', 'Good'),
('2', 'Poor'),
('1', 'Very Poor')])
is_fav = BooleanField('Favorite')
image = FileField('Image',
render_kw={"accept": "image/*"},
validators=[FileAllowed(['jpg', 'gif', 'png', 'jpeg'],
'Only image files can be uploaded.')])
hidden_tags = HiddenField('Hidden Tags')
submit = SubmitField('Add Review')
# The following class creates the form to update the user's account
# information and defines validation rules for each of its fields
class UpdateAccount(FlaskForm):
username = StringField('Username',
validators=[Length(min=1, max=30), Optional()])
email = StringField('Email',
validators=[Email(), Optional()])
new_password = PasswordField('New Password',
validators=[Length(min=8), Optional()])
confirm_password = PasswordField('Confirm New Password',
validators=[EqualTo('new_password')])
password = PasswordField('Current Password',
validators=[DataRequired()])
submit = SubmitField('Save Changes')
# The following class creates the form to send feedback
# and defines validation rules for each of its fields
class SendFeedback(FlaskForm):
message = TextAreaField('Anything to report? Send us a message!',
render_kw={"rows": 5, "spellcheck": "false", "maxlength": 5000},
validators=[DataRequired(), Length(min=1, max=5000)])
submit = SubmitField('Send Feedback')