-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwtform_fields.py
More file actions
41 lines (29 loc) · 1.77 KB
/
wtform_fields.py
File metadata and controls
41 lines (29 loc) · 1.77 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
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField
from wtforms.validators import InputRequired, Length, EqualTo, ValidationError
from passlib.hash import pbkdf2_sha256
from models import User
def invalid_credentials(form, field):
""" Username and password checker """
password = field.data
username = form.username.data
# Check username is invalid
user_data = User.query.filter_by(username=username).first()
if user_data is None:
raise ValidationError("Username or password is incorrect")
# Check password in invalid
elif not pbkdf2_sha256.verify(password, user_data.hashed_pswd):
raise ValidationError("Username or password is incorrect")
class RegistrationForm(FlaskForm):
""" Registration form"""
username = StringField('username', validators=[InputRequired(message="Username required"), Length(min=4, max=25, message="Username must be between 4 and 25 characters")])
password = PasswordField('password', validators=[InputRequired(message="Password required"), Length(min=4, max=25, message="Password must be between 4 and 25 characters")])
confirm_pswd = PasswordField('confirm_pswd', validators=[InputRequired(message="Password required"), EqualTo('password', message="Passwords must match")])
def validate_username(self, username):
user_object = User.query.filter_by(username=username.data).first()
if user_object:
raise ValidationError("Username already exists. Select a different username.")
class LoginForm(FlaskForm):
""" Login form """
username = StringField('username', validators=[InputRequired(message="Username required")])
password = PasswordField('password', validators=[InputRequired(message="Password required"), invalid_credentials])