nopaque/app/auth/forms.py

86 lines
2.3 KiB
Python
Raw Normal View History

from flask_wtf import FlaskForm
from wtforms import (
BooleanField,
PasswordField,
StringField,
SubmitField,
ValidationError
)
2022-09-02 11:07:30 +00:00
from wtforms.validators import InputRequired, Email, EqualTo, Length, Regexp
from app.models import User
from . import USERNAME_REGEX
2019-07-08 09:08:24 +00:00
class RegistrationForm(FlaskForm):
2022-09-02 11:07:30 +00:00
email = StringField(
'Email',
validators=[InputRequired(), Email(), Length(max=254)]
)
username = StringField(
'Username',
validators=[
2022-04-12 14:11:24 +00:00
InputRequired(),
2022-09-02 11:07:30 +00:00
Length(max=64),
Regexp(
USERNAME_REGEX,
2022-09-02 11:07:30 +00:00
message=(
'Usernames must have only letters, numbers, dots or '
'underscores'
)
)
]
)
2022-09-02 11:07:30 +00:00
password = PasswordField(
'Password',
validators=[
InputRequired(),
EqualTo('password_2', message='Passwords must match')
]
)
password_2 = PasswordField(
'Password confirmation',
validators=[
InputRequired(),
EqualTo('password', message='Passwords must match')
]
)
submit = SubmitField()
2019-07-08 12:06:35 +00:00
def validate_email(self, field):
if User.query.filter_by(email=field.data.lower()).first():
raise ValidationError('Email already registered')
2019-07-08 12:06:35 +00:00
def validate_username(self, field):
if User.query.filter_by(username=field.data).first():
raise ValidationError('Username already in use')
2020-02-19 15:24:58 +00:00
2022-09-02 11:07:30 +00:00
class LoginForm(FlaskForm):
user = StringField('Email or username', validators=[InputRequired()])
password = PasswordField('Password', validators=[InputRequired()])
remember_me = BooleanField('Keep me logged in')
submit = SubmitField()
2020-02-19 15:24:58 +00:00
class ResetPasswordRequestForm(FlaskForm):
2022-09-02 11:07:30 +00:00
email = StringField('Email', validators=[InputRequired(), Email()])
submit = SubmitField()
class ResetPasswordForm(FlaskForm):
password = PasswordField(
'New password',
validators=[
InputRequired(),
EqualTo('password_2', message='Passwords must match')
]
)
password_2 = PasswordField(
'New password confirmation',
validators=[
InputRequired(),
EqualTo('password', message='Passwords must match')
]
)
submit = SubmitField()