-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathwtform_fields.py
More file actions
42 lines (30 loc) · 1.79 KB
/
wtform_fields.py
File metadata and controls
42 lines (30 loc) · 1.79 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
from flask_wtf import FlaskForm
from wtforms import StringField,PasswordField,SubmitField
from wtforms.validators import InputRequired, Length, EqualTo,ValidationError
from models import User
def invalid_credentials(form, field):
""" Username and password checker """
password_entered = field.data
username_entered = form.username.data
# Check username is invalid
user_object = User.query.filter_by(username=username_entered).first()
if user_object is None:
raise ValidationError("Username or password is incorrect")
# Check password in invalid
elif password_entered != user_object.password:
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")])
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 already taken")
class LoginForm(FlaskForm):
""" Login form """
username = StringField('username', validators=[InputRequired(message="Username required")])
password = PasswordField('password', validators=[InputRequired(message="Password required"), invalid_credentials])
submit_button = SubmitField('Login')