2020-10-23 11:52:01 +00:00
|
|
|
from flask_wtf import FlaskForm
|
|
|
|
from wtforms import (BooleanField, PasswordField, SelectField, StringField,
|
|
|
|
SubmitField, ValidationError)
|
|
|
|
from wtforms.validators import DataRequired, Email, EqualTo, Length, Regexp
|
2021-09-13 14:36:48 +00:00
|
|
|
from ..auth import USERNAME_REGEX
|
2020-10-27 13:23:23 +00:00
|
|
|
from ..models import User
|
2020-10-23 11:52:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ChangePasswordForm(FlaskForm):
|
|
|
|
password = PasswordField('Old password', validators=[DataRequired()])
|
|
|
|
new_password = PasswordField(
|
|
|
|
'New password',
|
|
|
|
validators=[DataRequired(), EqualTo('password_confirmation',
|
|
|
|
message='Passwords must match.')]
|
|
|
|
)
|
|
|
|
new_password2 = PasswordField(
|
|
|
|
'Confirm new password', validators=[DataRequired()])
|
|
|
|
submit = SubmitField('Change password')
|
|
|
|
|
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):
|
|
|
|
raise ValidationError('Invalid password.')
|
|
|
|
|
|
|
|
|
|
|
|
class EditGeneralSettingsForm(FlaskForm):
|
|
|
|
dark_mode = BooleanField('Dark mode')
|
|
|
|
email = StringField('E-Mail',
|
|
|
|
validators=[DataRequired(), Length(1, 254), Email()])
|
|
|
|
username = StringField(
|
|
|
|
'Benutzername',
|
|
|
|
validators=[DataRequired(),
|
|
|
|
Length(1, 64),
|
2021-09-13 14:36:48 +00:00
|
|
|
Regexp(USERNAME_REGEX,
|
2020-10-23 11:52:01 +00:00
|
|
|
message='Usernames must have only letters, numbers,'
|
|
|
|
' dots or underscores')]
|
|
|
|
)
|
|
|
|
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):
|
|
|
|
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 EditNotificationSettingsForm(FlaskForm):
|
|
|
|
job_status_mail_notifications = SelectField(
|
|
|
|
'Job status mail notifications',
|
|
|
|
choices=[('', 'Choose your option'),
|
|
|
|
('all', 'Notify on all status changes'),
|
|
|
|
('end', 'Notify only when a job ended'),
|
|
|
|
('none', 'No status update notifications')],
|
|
|
|
validators=[DataRequired()])
|
|
|
|
job_status_site_notifications = SelectField(
|
|
|
|
'Job status site notifications',
|
|
|
|
choices=[('', 'Choose your option'),
|
|
|
|
('all', 'Notify on all status changes'),
|
|
|
|
('end', 'Notify only when a job ended'),
|
|
|
|
('none', 'No status update notifications')],
|
|
|
|
validators=[DataRequired()])
|
|
|
|
submit = SubmitField('Save settings')
|