mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2024-11-15 01:05:42 +00:00
173 lines
4.9 KiB
Python
173 lines
4.9 KiB
Python
from flask_login import current_user
|
|
from flask_wtf import FlaskForm
|
|
from wtforms import (
|
|
FileField,
|
|
PasswordField,
|
|
SelectField,
|
|
StringField,
|
|
SubmitField,
|
|
TextAreaField,
|
|
ValidationError
|
|
)
|
|
from wtforms.validators import (
|
|
DataRequired,
|
|
Email,
|
|
EqualTo,
|
|
Length,
|
|
Regexp
|
|
)
|
|
from app.models import User, UserSettingJobStatusMailNotificationLevel
|
|
from app.auth import USERNAME_REGEX
|
|
from app.wtf_validators import FileSizeLimit
|
|
|
|
|
|
class EditAccountForm(FlaskForm):
|
|
email = StringField(
|
|
'E-Mail',
|
|
validators=[DataRequired(), Length(max=254), Email()]
|
|
)
|
|
username = StringField(
|
|
'Username',
|
|
validators=[
|
|
DataRequired(),
|
|
Length(max=64),
|
|
Regexp(
|
|
USERNAME_REGEX,
|
|
message=(
|
|
'Usernames must have only letters, numbers, dots or '
|
|
'underscores'
|
|
)
|
|
)
|
|
]
|
|
)
|
|
submit = SubmitField()
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
user = kwargs.get('user', current_user._get_current_object())
|
|
if 'data' not in kwargs:
|
|
kwargs['data'] = user.to_json_serializeable()
|
|
if 'prefix' not in kwargs:
|
|
kwargs['prefix'] = 'edit-profile-settings-form'
|
|
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')
|
|
|
|
def validate_on_submit(self):
|
|
return self.submit.data and self.validate()
|
|
|
|
|
|
class EditProfileForm(FlaskForm):
|
|
avatar = FileField(
|
|
'Image File',
|
|
[FileSizeLimit(max_size_in_mb=2)]
|
|
)
|
|
full_name = StringField(
|
|
'Full name',
|
|
validators=[Length(max=128)]
|
|
)
|
|
about_me = TextAreaField(
|
|
'About me',
|
|
validators=[
|
|
Length(max=254)
|
|
]
|
|
)
|
|
website = StringField(
|
|
'Website',
|
|
validators=[
|
|
Length(max=254)
|
|
]
|
|
)
|
|
organization = StringField(
|
|
'Organization',
|
|
validators=[
|
|
Length(max=128)
|
|
]
|
|
)
|
|
location = StringField(
|
|
'Location',
|
|
validators=[
|
|
Length(max=128)
|
|
]
|
|
)
|
|
submit = SubmitField()
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
if 'data' not in kwargs:
|
|
user = current_user._get_current_object()
|
|
kwargs['data'] = user.to_json_serializeable()
|
|
if 'prefix' not in kwargs:
|
|
kwargs['prefix'] = 'edit-public-profile-information-form'
|
|
super().__init__(*args, **kwargs)
|
|
|
|
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!')
|
|
|
|
def validate_on_submit(self):
|
|
return self.submit.data and self.validate()
|
|
|
|
|
|
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, *args, **kwargs):
|
|
user = kwargs.get('user', current_user._get_current_object())
|
|
if 'prefix' not in kwargs:
|
|
kwargs['prefix'] = 'change-password-form'
|
|
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')
|
|
|
|
def validate_on_submit(self):
|
|
return self.submit.data and self.validate()
|
|
|
|
|
|
class EditNotificationsForm(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()
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
if 'data' not in kwargs:
|
|
user = current_user._get_current_object()
|
|
kwargs['data'] = user.to_json_serializeable()
|
|
if 'prefix' not in kwargs:
|
|
kwargs['prefix'] = 'edit-notification-settings-form'
|
|
super().__init__(*args, **kwargs)
|
|
|
|
def validate_on_submit(self):
|
|
return self.submit.data and self.validate()
|