nopaque/app/auth/forms.py

105 lines
3.0 KiB
Python
Raw Normal View History

2023-03-31 07:14:21 +00:00
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
2023-03-31 07:14:21 +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(
2023-03-29 12:32:35 +00:00
User.username_pattern,
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
2023-03-31 07:14:21 +00:00
def __init__(self, *args, **kwargs):
if 'prefix' not in kwargs:
kwargs['prefix'] = 'registration-form'
super().__init__(*args, **kwargs)
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
2023-03-31 07:14:21 +00:00
class LoginForm(FlaskForm):
2022-09-02 11:07:30 +00:00
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
2023-03-31 07:14:21 +00:00
def __init__(self, *args, **kwargs):
if 'prefix' not in kwargs:
kwargs['prefix'] = 'login-form'
super().__init__(*args, **kwargs)
2020-02-19 15:24:58 +00:00
2023-03-31 07:14:21 +00:00
class ResetPasswordRequestForm(FlaskForm):
2022-09-02 11:07:30 +00:00
email = StringField('Email', validators=[InputRequired(), Email()])
submit = SubmitField()
2023-03-31 07:14:21 +00:00
def __init__(self, *args, **kwargs):
if 'prefix' not in kwargs:
kwargs['prefix'] = 'reset-password-request-form'
super().__init__(*args, **kwargs)
2022-09-02 11:07:30 +00:00
2023-03-31 07:14:21 +00:00
class ResetPasswordForm(FlaskForm):
2022-09-02 11:07:30 +00:00
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()
2023-03-31 07:14:21 +00:00
def __init__(self, *args, **kwargs):
if 'prefix' not in kwargs:
kwargs['prefix'] = 'reset-password-form'
super().__init__(*args, **kwargs)