-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform_fields.py
72 lines (54 loc) · 2.22 KB
/
form_fields.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
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField
from wtforms.validators import InputRequired, Length, EqualTo, ValidationError
from models import User
from passlib.hash import pbkdf2_sha256
class RegistrationForm(FlaskForm):
""" Registration Form """
username = StringField('username_label',
validators=[
InputRequired(message="Username required"),
Length(min=4, max=25, message="Username must be between 4 and 25 characters")
]
)
password = PasswordField('password_label',
validators=[
InputRequired(message="Password required"),
Length(min=4, max=25, message="Username must be between 4 and 25 characters")
]
)
confirm_pswd = PasswordField('confirm_pswd_label',
validators=[
InputRequired(message="Password required"),
EqualTo('password', message="Passwords must match")
])
submit_button = SubmitField('Create')
def validate_username(self, username):
user_object = User.query.filter_by(username=username.data).first()
if user_object:
raise ValidationError('Username is incorrect')
# Even if we add params here, we don't need to pass the values explicitly, Flask wtforms automatically sends the data (form and field)
def invalid_credentials(form, field):
"""Username and password checker"""
username_entered = form.username.data
password_entered = field.data
# check whether the data is valid or not
user_object = User.query.filter_by(username=username_entered).first()
if user_object is None:
raise ValidationError('Username or password is incorrect')
elif not pbkdf2_sha256.verify(password_entered, user_object.password):
raise ValidationError('Username or password is incorrect')
class LoginForm(FlaskForm):
""" Login Form """
username = StringField('username_label',
validators=[
InputRequired(message="Username required"),
]
)
password = PasswordField('password_label',
validators=[
InputRequired(message="Password required"),
invalid_credentials
]
)
submit_button = SubmitField('Submit')