from app.auth import USERNAME_REGEX from app.models import User, UserSettingJobStatusMailNotificationLevel from flask_wtf import FlaskForm from wtforms import ( BooleanField, PasswordField, SelectField, StringField, SubmitField, ValidationError ) from wtforms.validators import DataRequired, InputRequired, Email, EqualTo, Length, Regexp class ChangePasswordForm(FlaskForm): password = PasswordField('Old password', validators=[DataRequired()]) new_password = PasswordField('New password', validators=[DataRequired(), EqualTo('new_password_confirmation', message='Passwords must match')]) new_password_confirmation = PasswordField('Confirm new password', validators=[DataRequired(), EqualTo('new_password', message='Passwords must match')]) submit = SubmitField('Submit') def __init__(self, user, *args, **kwargs): super().__init__(*args, **kwargs) self.user = user def validate_current_password(self, field): if not self.user.verify_password(field.data): raise ValidationError('Invalid password') class EditGeneralSettingsForm(FlaskForm): email = StringField('E-Mail', validators=[DataRequired(), Length(1, 254), Email()]) username = StringField( 'Username', validators=[ InputRequired(), Length(1, 64), Regexp( USERNAME_REGEX, message='Usernames must have only letters, numbers, dots or underscores' # noqa ) ] ) submit = SubmitField('Submit') def __init__(self, user, *args, **kwargs): super().__init__(*args, **kwargs) self.user = user def validate_email(self, field): if ( field.data != self.user.email and User.query.filter_by(email=field.data).first() ): raise ValidationError('Email already registered') def validate_username(self, field): if ( field.data != self.user.username and User.query.filter_by(username=field.data).first() ): raise ValidationError('Username already in use') class EditInterfaceSettingsForm(FlaskForm): dark_mode = BooleanField('Dark mode') submit = SubmitField('Submit') class EditNotificationSettingsForm(FlaskForm): job_status_mail_notification_level = SelectField( 'Job status mail notification level', choices=[('', 'Choose your option')], validators=[DataRequired()] ) submit = SubmitField('Submit') def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.job_status_mail_notification_level.choices += [ (enum_member.name, enum_member.name.capitalize()) for enum_member in UserSettingJobStatusMailNotificationLevel ]