ScoDoc/app/auth/forms.py

56 lines
1.9 KiB
Python
Raw Permalink Normal View History

2021-05-29 18:22:51 +02:00
# -*- coding: UTF-8 -*
"""Formulaires authentification
TODO: à revoir complètement pour reprendre ZScoUsers et les pages d'authentification
"""
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SubmitField
from wtforms.validators import ValidationError, DataRequired, Email, EqualTo
from app.auth.models import User
_ = lambda x: x # sans babel
_l = _
class LoginForm(FlaskForm):
user_name = StringField(_l("Username"), validators=[DataRequired()])
2021-05-29 18:22:51 +02:00
password = PasswordField(_l("Password"), validators=[DataRequired()])
remember_me = BooleanField(_l("Remember Me"))
submit = SubmitField(_l("Sign In"))
class UserCreationForm(FlaskForm):
user_name = StringField(_l("Username"), validators=[DataRequired()])
2021-05-29 18:22:51 +02:00
email = StringField(_l("Email"), validators=[DataRequired(), Email()])
password = PasswordField(_l("Password"), validators=[DataRequired()])
password2 = PasswordField(
_l("Repeat Password"), validators=[DataRequired(), EqualTo("password")]
)
submit = SubmitField(_l("Register"))
def validate_user_name(self, user_name):
user = User.query.filter_by(user_name=user_name.data).first()
2021-05-29 18:22:51 +02:00
if user is not None:
raise ValidationError(_("Please use a different user_name."))
2021-05-29 18:22:51 +02:00
def validate_email(self, email):
user = User.query.filter_by(email=email.data).first()
if user is not None:
raise ValidationError(_("Please use a different email address."))
class ResetPasswordRequestForm(FlaskForm):
email = StringField(_l("Email"), validators=[DataRequired(), Email()])
submit = SubmitField(_l("Request Password Reset"))
class ResetPasswordForm(FlaskForm):
password = PasswordField(_l("Password"), validators=[DataRequired()])
password2 = PasswordField(
_l("Repeat Password"), validators=[DataRequired(), EqualTo("password")]
)
submit = SubmitField(_l("Request Password Reset"))