2022-02-08 11:26:20 +00:00
|
|
|
from app.auth import USERNAME_REGEX
|
2022-02-09 15:02:37 +00:00
|
|
|
from app.models import User, UserSettingJobStatusMailNotificationLevel
|
2020-10-23 11:52:01 +00:00
|
|
|
from flask_wtf import FlaskForm
|
2022-02-08 11:26:20 +00:00
|
|
|
from wtforms import (
|
|
|
|
BooleanField,
|
|
|
|
PasswordField,
|
|
|
|
SelectField,
|
|
|
|
StringField,
|
|
|
|
SubmitField,
|
|
|
|
ValidationError
|
|
|
|
)
|
2022-04-12 14:11:24 +00:00
|
|
|
from wtforms.validators import DataRequired, InputRequired, Email, EqualTo, Length, Regexp
|
2020-10-23 11:52:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ChangePasswordForm(FlaskForm):
|
|
|
|
password = PasswordField('Old password', validators=[DataRequired()])
|
2022-04-12 14:11:24 +00:00
|
|
|
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')])
|
2022-02-08 11:26:20 +00:00
|
|
|
submit = SubmitField('Submit')
|
2020-10-23 11:52:01 +00:00
|
|
|
|
2021-09-13 14:36:48 +00:00
|
|
|
def __init__(self, user, *args, **kwargs):
|
2020-10-23 11:52:01 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.user = user
|
|
|
|
|
|
|
|
def validate_current_password(self, field):
|
|
|
|
if not self.user.verify_password(field.data):
|
2022-02-08 11:26:20 +00:00
|
|
|
raise ValidationError('Invalid password')
|
2020-10-23 11:52:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
class EditGeneralSettingsForm(FlaskForm):
|
2022-04-12 14:11:24 +00:00
|
|
|
email = StringField('E-Mail', validators=[DataRequired(), Length(1, 254), Email()])
|
2020-10-23 11:52:01 +00:00
|
|
|
username = StringField(
|
2022-02-08 11:26:20 +00:00
|
|
|
'Username',
|
|
|
|
validators=[
|
2022-04-12 14:11:24 +00:00
|
|
|
InputRequired(),
|
2022-02-08 11:26:20 +00:00
|
|
|
Length(1, 64),
|
|
|
|
Regexp(
|
|
|
|
USERNAME_REGEX,
|
|
|
|
message='Usernames must have only letters, numbers, dots or underscores' # noqa
|
|
|
|
)
|
|
|
|
]
|
2020-10-23 11:52:01 +00:00
|
|
|
)
|
|
|
|
submit = SubmitField('Submit')
|
|
|
|
|
2021-09-13 14:36:48 +00:00
|
|
|
def __init__(self, user, *args, **kwargs):
|
2020-10-23 11:52:01 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.user = user
|
|
|
|
|
|
|
|
def validate_email(self, field):
|
2022-02-08 11:26:20 +00:00
|
|
|
if (
|
|
|
|
field.data != self.user.email
|
|
|
|
and User.query.filter_by(email=field.data).first()
|
|
|
|
):
|
|
|
|
raise ValidationError('Email already registered')
|
2020-10-23 11:52:01 +00:00
|
|
|
|
|
|
|
def validate_username(self, field):
|
2022-02-08 11:26:20 +00:00
|
|
|
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')
|
2020-10-23 11:52:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
class EditNotificationSettingsForm(FlaskForm):
|
2022-02-08 11:26:20 +00:00
|
|
|
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())
|
2022-02-09 15:02:37 +00:00
|
|
|
for enum_member in UserSettingJobStatusMailNotificationLevel
|
2022-02-08 11:26:20 +00:00
|
|
|
]
|