<reponame>Moremar/py-library-tuto
from flask_wtf import FlaskForm
from flask_wtf.file import FileField, FileAllowed
from wtforms import StringField, PasswordField, SubmitField, BooleanField, ValidationError
from wtforms.validators import DataRequired, Length, Email, EqualTo
from flask_login import current_user

# import user DB model for unique validation
from myblog.models import User


# Form for the registration of a new user
class SignupForm(FlaskForm):
    username = StringField('Username', validators=[DataRequired(), Length(min=5, max=20)])
    email = StringField('Email', validators=[DataRequired(), Email()])
    password = PasswordField('Password', validators=[DataRequired(), Length(min=6)])
    confirm_password = PasswordField('Confirm Password', validators=[DataRequired(), EqualTo("password")])
    submit = SubmitField('Signup')

    # custom validator for "username" field, automatically called (because it is called validate_username)
    def validate_username(self, username):
        user = User.query.filter_by(username=username.data).first()
        if user:
            raise ValidationError('This username is already used, please choose a different one.')

    # custom validator for "email" field, automatically called (because it is called validate_email)
    def validate_email(self, email):
        user = User.query.filter_by(email=email.data).first()
        if user:
            raise ValidationError('This email is already used, please choose a different one.')


# Form for the login of a registered user
class LoginForm(FlaskForm):
    email = StringField('Email', validators=[DataRequired(), Email()])
    password = PasswordField('Password', validators=[DataRequired(), Length(min=6)])
    remember_me = BooleanField('Remember me')
    submit = SubmitField('Login')


# Form for the registration of a new user
class UpdateAccountForm(FlaskForm):
    username = StringField('Username', validators=[DataRequired(), Length(min=5, max=20)])
    email = StringField('Email', validators=[DataRequired(), Email()])
    picture = FileField('Update profile picture', validators=[FileAllowed(['jpg', 'png'])])
    submit = SubmitField('Update')

    # custom validator for "username" field, automatically called (because it is called validate_username)
    def validate_username(self, username):
        # if the username did not change, dont validate
        if username.data != current_user.username:
            user = User.query.filter_by(username=username.data).first()
            if user:
                raise ValidationError('This username is already used, please choose a different one.')

    # custom validator for "email" field, automatically called (because it is called validate_email)
    def validate_email(self, email):
        # if the email did not change, dont validate
        if email.data != current_user.email:
            user = User.query.filter_by(email=email.data).first()
            if user:
                raise ValidationError('This email is already used, please choose a different one.')


# Form to request a password reset for a given email
class RequestResetForm(FlaskForm):
    email = StringField('Email', validators=[DataRequired(), Email()])
    submit = SubmitField('Request password reset')

    # custom validator for "email" field
    def validate_email(self, email):
        user = User.query.filter_by(email=email.data).first()
        if user is None:
            raise ValidationError('There is no account associated with this email.')


# Form to actually reset the password for a given email
class ResetPasswordForm(FlaskForm):
    password = PasswordField('Password', validators=[DataRequired(), Length(min=6)])
    confirm_password = PasswordField('Confirm Password', validators=[DataRequired(), EqualTo("password")])
    submit = SubmitField('Reset password')

from flask_wtf import FlaskForm
from flask_wtf.file import FileField, FileAllowed
from wtforms import StringField, PasswordField, SubmitField, BooleanField, ValidationError
from wtforms.validators import DataRequired, Length, Email, EqualTo
from flask_login import current_user

# import user DB model for unique validation
from myblog.models import User


# Form for the registration of a new user
class SignupForm(FlaskForm):
    username = StringField('Username', validators=[DataRequired(), Length(min=5, max=20)])
    email = StringField('Email', validators=[DataRequired(), Email()])
    password = PasswordField('Password', validators=[DataRequired(), Length(min=6)])
    confirm_password = PasswordField('Confirm Password', validators=[DataRequired(), EqualTo("password")])
    submit = SubmitField('Signup')

    # custom validator for "username" field, automatically called (because it is called validate_username)
    def validate_username(self, username):
        user = User.query.filter_by(username=username.data).first()
        if user:
            raise ValidationError('This username is already used, please choose a different one.')

    # custom validator for "email" field, automatically called (because it is called validate_email)
    def validate_email(self, email):
        user = User.query.filter_by(email=email.data).first()
        if user:
            raise ValidationError('This email is already used, please choose a different one.')


# Form for the login of a registered user
class LoginForm(FlaskForm):
    email = StringField('Email', validators=[DataRequired(), Email()])
    password = PasswordField('Password', validators=[DataRequired(), Length(min=6)])
    remember_me = BooleanField('Remember me')
    submit = SubmitField('Login')


# Form for the registration of a new user
class UpdateAccountForm(FlaskForm):
    username = StringField('Username', validators=[DataRequired(), Length(min=5, max=20)])
    email = StringField('Email', validators=[DataRequired(), Email()])
    picture = FileField('Update profile picture', validators=[FileAllowed(['jpg', 'png'])])
    submit = SubmitField('Update')

    # custom validator for "username" field, automatically called (because it is called validate_username)
    def validate_username(self, username):
        # if the username did not change, dont validate
        if username.data != current_user.username:
            user = User.query.filter_by(username=username.data).first()
            if user:
                raise ValidationError('This username is already used, please choose a different one.')

    # custom validator for "email" field, automatically called (because it is called validate_email)
    def validate_email(self, email):
        # if the email did not change, dont validate
        if email.data != current_user.email:
            user = User.query.filter_by(email=email.data).first()
            if user:
                raise ValidationError('This email is already used, please choose a different one.')


# Form to request a password reset for a given email
class RequestResetForm(FlaskForm):
    email = StringField('Email', validators=[DataRequired(), Email()])
    submit = SubmitField('Request password reset')

    # custom validator for "email" field
    def validate_email(self, email):
        user = User.query.filter_by(email=email.data).first()
        if user is None:
            raise ValidationError('There is no account associated with this email.')


# Form to actually reset the password for a given email
class ResetPasswordForm(FlaskForm):
    password = PasswordField('Password', validators=[DataRequired(), Length(min=6)])
    confirm_password = PasswordField('Confirm Password', validators=[DataRequired(), EqualTo("password")])
    submit = SubmitField('Reset password')
# import user DB model for unique validation # Form for the registration of a new user # custom validator for "username" field, automatically called (because it is called validate_username) # custom validator for "email" field, automatically called (because it is called validate_email) # Form for the login of a registered user # Form for the registration of a new user # custom validator for "username" field, automatically called (because it is called validate_username) # if the username did not change, dont validate # custom validator for "email" field, automatically called (because it is called validate_email) # if the email did not change, dont validate # Form to request a password reset for a given email # custom validator for "email" field # Form to actually reset the password for a given email