bump project
This commit is contained in:
45
app/admin/forms.py
Normal file
45
app/admin/forms.py
Normal file
@@ -0,0 +1,45 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
|
||||
from flask_wtf import FlaskForm
|
||||
from wtforms import EmailField, PasswordField, SelectField, StringField, SubmitField
|
||||
from wtforms.validators import DataRequired, Email, EqualTo, Length, ValidationError
|
||||
|
||||
from app.models import Account, Role
|
||||
|
||||
|
||||
class AccountCreateForm(FlaskForm):
|
||||
full_name = StringField("Full name", validators=[DataRequired(), Length(max=160)])
|
||||
username = StringField("Username", validators=[DataRequired(), Length(min=3, max=80)])
|
||||
email = EmailField("Email", validators=[DataRequired(), Email(), Length(max=255)])
|
||||
role = SelectField(
|
||||
"Role",
|
||||
choices=[(Role.OPERATOR.value, "Operator"), (Role.VIEWER.value, "Viewer"), (Role.ADMIN.value, "Admin")],
|
||||
validators=[DataRequired()],
|
||||
)
|
||||
password = PasswordField("Password", validators=[DataRequired(), Length(min=12, max=128)])
|
||||
confirm_password = PasswordField(
|
||||
"Confirm password",
|
||||
validators=[DataRequired(), EqualTo("password", message="Passwords must match.")],
|
||||
)
|
||||
submit = SubmitField("Create account")
|
||||
|
||||
def validate_username(self, field) -> None:
|
||||
username = field.data.strip().lower()
|
||||
if not re.fullmatch(r"[a-z0-9_.-]+", username):
|
||||
raise ValidationError("Use only letters, numbers, dots, hyphens, and underscores.")
|
||||
if Account.query.filter_by(username=username).first():
|
||||
raise ValidationError("That username is already in use.")
|
||||
field.data = username
|
||||
|
||||
def validate_email(self, field) -> None:
|
||||
email = field.data.strip().lower()
|
||||
if Account.query.filter_by(email=email).first():
|
||||
raise ValidationError("That email is already in use.")
|
||||
field.data = email
|
||||
|
||||
def validate_password(self, field) -> None:
|
||||
password = field.data
|
||||
if not re.search(r"[A-Z]", password) or not re.search(r"[a-z]", password) or not re.search(r"\d", password):
|
||||
raise ValidationError("Use at least one uppercase letter, one lowercase letter, and one number.")
|
||||
Reference in New Issue
Block a user