nopaque/app/settings/forms.py

163 lines
4.7 KiB
Python
Raw Normal View History

2023-03-23 16:42:51 +00:00
from flask_login import current_user
2023-03-31 07:14:21 +00:00
from flask_wtf import FlaskForm
2023-03-29 12:32:35 +00:00
from flask_wtf.file import FileField, FileRequired
2022-11-30 13:36:42 +00:00
from wtforms import (
2023-03-13 14:04:44 +00:00
PasswordField,
SelectField,
2022-11-30 13:36:42 +00:00
StringField,
SubmitField,
TextAreaField,
ValidationError
)
from wtforms.validators import (
DataRequired,
2022-11-30 13:36:42 +00:00
Email,
2023-03-13 14:04:44 +00:00
EqualTo,
2022-11-30 13:36:42 +00:00
Length,
Regexp
)
2023-03-13 14:04:44 +00:00
from app.models import User, UserSettingJobStatusMailNotificationLevel
2023-03-31 07:14:21 +00:00
from app.wtforms.validators import FileSize
2022-11-30 13:36:42 +00:00
2023-03-23 16:42:51 +00:00
2023-03-31 07:14:21 +00:00
class UpdateAccountInformationForm(FlaskForm):
2022-11-30 13:36:42 +00:00
email = StringField(
'E-Mail',
validators=[DataRequired(), Length(max=254), Email()]
2022-11-30 13:36:42 +00:00
)
username = StringField(
'Username',
validators=[
DataRequired(),
2022-11-30 13:36:42 +00:00
Length(max=64),
Regexp(
2023-03-29 12:32:35 +00:00
User.username_pattern,
2022-11-30 13:36:42 +00:00
message=(
'Usernames must have only letters, numbers, dots or '
'underscores'
)
)
]
)
2022-12-13 14:01:04 +00:00
submit = SubmitField()
2023-03-29 12:32:35 +00:00
def __init__(self, *args, user=current_user, **kwargs):
2023-03-23 16:42:51 +00:00
if 'data' not in kwargs:
kwargs['data'] = user.to_json_serializeable()
2023-03-31 07:14:21 +00:00
if 'prefix' not in kwargs:
kwargs['prefix'] = 'update-account-information-form'
2022-12-13 14:01:04 +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')
2023-03-23 16:42:51 +00:00
2023-03-31 07:14:21 +00:00
class UpdateProfileInformationForm(FlaskForm):
2022-11-30 13:36:42 +00:00
full_name = StringField(
'Full name',
validators=[Length(max=128)]
)
about_me = TextAreaField(
'About me',
validators=[
Length(max=254)
2023-03-23 16:42:51 +00:00
]
2022-11-30 13:36:42 +00:00
)
website = StringField(
'Website',
validators=[
Length(max=254)
]
)
organization = StringField(
'Organization',
validators=[
Length(max=128)
]
)
location = StringField(
'Location',
validators=[
Length(max=128)
]
)
submit = SubmitField()
2023-03-29 12:32:35 +00:00
def __init__(self, *args, user=current_user, **kwargs):
2023-03-23 16:42:51 +00:00
if 'data' not in kwargs:
kwargs['data'] = user.to_json_serializeable()
2023-03-31 07:14:21 +00:00
if 'prefix' not in kwargs:
kwargs['prefix'] = 'update-profile-information-form'
2023-03-23 16:42:51 +00:00
super().__init__(*args, **kwargs)
2022-12-13 14:01:04 +00:00
2023-03-31 07:14:21 +00:00
class UpdateAvatarForm(FlaskForm):
avatar = FileField('File', validators=[FileRequired(), FileSize(2)])
2023-03-29 12:32:35 +00:00
submit = SubmitField()
2023-03-23 16:42:51 +00:00
2023-03-29 12:32:35 +00:00
def validate_avatar(self, field):
valid_mimetypes = ['image/jpeg', 'image/png']
if field.data.mimetype not in valid_mimetypes:
raise ValidationError('JPEG and PNG files only!')
2023-03-23 16:42:51 +00:00
2023-03-31 07:14:21 +00:00
def __init__(self, *args, **kwargs):
if 'prefix' not in kwargs:
kwargs['prefix'] = 'update-avatar-form'
super().__init__(*args, **kwargs)
class UpdatePasswordForm(FlaskForm):
2023-03-13 14:04:44 +00:00
password = PasswordField('Old password', validators=[DataRequired()])
new_password = PasswordField(
'New password',
validators=[
DataRequired(),
EqualTo('new_password_2', message='Passwords must match')
]
)
new_password_2 = PasswordField(
'New password confirmation',
validators=[
DataRequired(),
EqualTo('new_password', message='Passwords must match')
]
)
submit = SubmitField()
2023-03-29 12:32:35 +00:00
def __init__(self, *args, user=current_user, **kwargs):
2023-03-31 07:14:21 +00:00
if 'prefix' not in kwargs:
kwargs['prefix'] = 'update-password-form'
2023-03-13 14:04:44 +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')
2023-03-23 16:42:51 +00:00
2023-03-31 07:14:21 +00:00
class UpdateNotificationsForm(FlaskForm):
2023-03-13 14:04:44 +00:00
job_status_mail_notification_level = SelectField(
'Job status mail notification level',
choices=[
(x.name, x.name.capitalize())
for x in UserSettingJobStatusMailNotificationLevel
],
validators=[DataRequired()]
)
submit = SubmitField()
2023-03-23 16:42:51 +00:00
2023-03-29 12:32:35 +00:00
def __init__(self, *args, user=current_user, **kwargs):
2023-03-23 16:42:51 +00:00
if 'data' not in kwargs:
kwargs['data'] = user.to_json_serializeable()
2023-03-31 07:14:21 +00:00
if 'prefix' not in kwargs:
kwargs['prefix'] = 'update-notifications-form'
2023-03-23 16:42:51 +00:00
super().__init__(*args, **kwargs)