nopaque/app/users/forms.py

144 lines
3.9 KiB
Python
Raw Normal View History

2022-11-30 13:36:42 +00:00
from flask_wtf import FlaskForm
from wtforms import (
2022-12-13 14:01:04 +00:00
BooleanField,
2022-11-30 13:36:42 +00:00
FileField,
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
2022-11-30 13:36:42 +00:00
from app.auth import USERNAME_REGEX
from app.wtf_validators import FileSizeLimit
2022-11-30 13:36:42 +00:00
class EditProfileSettingsForm(FlaskForm):
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(
USERNAME_REGEX,
message=(
'Usernames must have only letters, numbers, dots or '
'underscores'
)
)
]
)
2022-12-13 14:01:04 +00:00
submit = SubmitField()
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 EditPublicProfileInformationForm(FlaskForm):
avatar = FileField(
'Image File',
[FileSizeLimit(max_size_in_mb=2)]
2022-12-13 14:01:04 +00:00
)
2022-11-30 13:36:42 +00:00
full_name = StringField(
'Full name',
validators=[Length(max=128)]
)
2023-03-15 08:18:11 +00:00
style={'style': 'overflow: auto;'}
2022-11-30 13:36:42 +00:00
about_me = TextAreaField(
'About me',
validators=[
Length(max=254)
2023-03-15 08:18:11 +00:00
],
render_kw=style
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()
def validate_image_file(self, field):
if not field.data.filename.lower().endswith('.jpg' or '.png' or '.jpeg'):
raise ValidationError('only .jpg, .png and .jpeg!')
2022-12-13 14:01:04 +00:00
class EditPrivacySettingsForm(FlaskForm):
is_public = BooleanField('Public profile')
show_email = BooleanField('Show email')
show_last_seen = BooleanField('Show last seen')
show_member_since = BooleanField('Show member since')
submit = SubmitField()
2023-03-13 14:04:44 +00:00
class ChangePasswordForm(FlaskForm):
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()
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 EditNotificationSettingsForm(FlaskForm):
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()