mirror of
				https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
				synced 2025-11-03 20:02:47 +00:00 
			
		
		
		
	Update admin user settings
This commit is contained in:
		@@ -1,6 +1,6 @@
 | 
			
		||||
from flask_login import current_user
 | 
			
		||||
from flask_wtf.file import FileField, FileRequired
 | 
			
		||||
from wtforms import (
 | 
			
		||||
    FileField,
 | 
			
		||||
    PasswordField,
 | 
			
		||||
    SelectField,
 | 
			
		||||
    StringField,
 | 
			
		||||
@@ -15,13 +15,11 @@ from wtforms.validators import (
 | 
			
		||||
    Length,
 | 
			
		||||
    Regexp
 | 
			
		||||
)
 | 
			
		||||
from app.forms import NopaqueForm
 | 
			
		||||
from app.forms import NopaqueForm, LimitFileSize
 | 
			
		||||
from app.models import User, UserSettingJobStatusMailNotificationLevel
 | 
			
		||||
from app.auth import USERNAME_REGEX
 | 
			
		||||
from app.wtf_validators import FileSizeLimit
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class EditAccountForm(NopaqueForm):
 | 
			
		||||
class UpdateAccountInformationForm(NopaqueForm):
 | 
			
		||||
    email = StringField(
 | 
			
		||||
        'E-Mail',
 | 
			
		||||
        validators=[DataRequired(), Length(max=254), Email()]
 | 
			
		||||
@@ -32,7 +30,7 @@ class EditAccountForm(NopaqueForm):
 | 
			
		||||
            DataRequired(),
 | 
			
		||||
            Length(max=64),
 | 
			
		||||
            Regexp(
 | 
			
		||||
                USERNAME_REGEX,
 | 
			
		||||
                User.username_pattern,
 | 
			
		||||
                message=(
 | 
			
		||||
                    'Usernames must have only letters, numbers, dots or '
 | 
			
		||||
                    'underscores'
 | 
			
		||||
@@ -42,8 +40,7 @@ class EditAccountForm(NopaqueForm):
 | 
			
		||||
    )
 | 
			
		||||
    submit = SubmitField()
 | 
			
		||||
    
 | 
			
		||||
    def __init__(self, *args, **kwargs):
 | 
			
		||||
        user = kwargs.get('user', current_user._get_current_object())
 | 
			
		||||
    def __init__(self, *args, user=current_user, **kwargs):
 | 
			
		||||
        if 'data' not in kwargs:
 | 
			
		||||
            kwargs['data'] = user.to_json_serializeable()
 | 
			
		||||
        super().__init__(*args, **kwargs)
 | 
			
		||||
@@ -59,15 +56,8 @@ class EditAccountForm(NopaqueForm):
 | 
			
		||||
                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(NopaqueForm):
 | 
			
		||||
    avatar = FileField(
 | 
			
		||||
        'Image File', 
 | 
			
		||||
        [FileSizeLimit(max_size_in_mb=2)]
 | 
			
		||||
    )
 | 
			
		||||
class UpdateProfileInformationForm(NopaqueForm):
 | 
			
		||||
    full_name = StringField(
 | 
			
		||||
        'Full name',
 | 
			
		||||
        validators=[Length(max=128)]
 | 
			
		||||
@@ -98,21 +88,22 @@ class EditProfileForm(NopaqueForm):
 | 
			
		||||
    )
 | 
			
		||||
    submit = SubmitField()
 | 
			
		||||
 | 
			
		||||
    def __init__(self, *args, **kwargs):
 | 
			
		||||
    def __init__(self, *args, user=current_user, **kwargs):
 | 
			
		||||
        if 'data' not in kwargs:
 | 
			
		||||
            user = current_user._get_current_object()
 | 
			
		||||
            kwargs['data'] = user.to_json_serializeable()
 | 
			
		||||
        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 UpdateAvatarForm(NopaqueForm):
 | 
			
		||||
    avatar = FileField('File', validators=[FileRequired(), LimitFileSize(2)])
 | 
			
		||||
    submit = SubmitField()
 | 
			
		||||
 | 
			
		||||
    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!')
 | 
			
		||||
 | 
			
		||||
class ChangePasswordForm(NopaqueForm):
 | 
			
		||||
class UpdatePasswordForm(NopaqueForm):
 | 
			
		||||
    password = PasswordField('Old password', validators=[DataRequired()])
 | 
			
		||||
    new_password = PasswordField(
 | 
			
		||||
        'New password',
 | 
			
		||||
@@ -130,8 +121,7 @@ class ChangePasswordForm(NopaqueForm):
 | 
			
		||||
    )
 | 
			
		||||
    submit = SubmitField()
 | 
			
		||||
 | 
			
		||||
    def __init__(self, *args, **kwargs):
 | 
			
		||||
        user = kwargs.get('user', current_user._get_current_object())
 | 
			
		||||
    def __init__(self, *args, user=current_user, **kwargs):
 | 
			
		||||
        super().__init__(*args, **kwargs)
 | 
			
		||||
        self.user = user
 | 
			
		||||
 | 
			
		||||
@@ -139,11 +129,8 @@ class ChangePasswordForm(NopaqueForm):
 | 
			
		||||
        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(NopaqueForm):
 | 
			
		||||
class UpdateNotificationsForm(NopaqueForm):
 | 
			
		||||
    job_status_mail_notification_level = SelectField(
 | 
			
		||||
        'Job status mail notification level',
 | 
			
		||||
        choices=[
 | 
			
		||||
@@ -154,11 +141,7 @@ class EditNotificationsForm(NopaqueForm):
 | 
			
		||||
    )
 | 
			
		||||
    submit = SubmitField()
 | 
			
		||||
 | 
			
		||||
    def __init__(self, *args, **kwargs):
 | 
			
		||||
    def __init__(self, *args, user=current_user, **kwargs):
 | 
			
		||||
        if 'data' not in kwargs:
 | 
			
		||||
            user = current_user._get_current_object()
 | 
			
		||||
            kwargs['data'] = user.to_json_serializeable()
 | 
			
		||||
        super().__init__(*args, **kwargs)
 | 
			
		||||
 | 
			
		||||
    def validate_on_submit(self):
 | 
			
		||||
        return self.submit.data and self.validate()
 | 
			
		||||
 
 | 
			
		||||
@@ -5,10 +5,11 @@ from app import db
 | 
			
		||||
from app.models import Avatar
 | 
			
		||||
from . import bp
 | 
			
		||||
from .forms import (
 | 
			
		||||
  ChangePasswordForm,
 | 
			
		||||
  EditNotificationsForm,
 | 
			
		||||
  EditAccountForm,
 | 
			
		||||
  EditProfileForm
 | 
			
		||||
    UpdateAvatarForm,
 | 
			
		||||
    UpdatePasswordForm,
 | 
			
		||||
    UpdateNotificationsForm,
 | 
			
		||||
    UpdateAccountInformationForm,
 | 
			
		||||
    UpdateProfileInformationForm
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -16,61 +17,72 @@ from .forms import (
 | 
			
		||||
@register_breadcrumb(bp, '.', '<i class="material-icons left">settings</i>Settings')
 | 
			
		||||
@login_required
 | 
			
		||||
def settings():
 | 
			
		||||
    user = current_user._get_current_object()
 | 
			
		||||
    # region forms
 | 
			
		||||
    edit_account_form = EditAccountForm()
 | 
			
		||||
    edit_profile_form = EditProfileForm()
 | 
			
		||||
    change_password_form = ChangePasswordForm()
 | 
			
		||||
    edit_notifications_form = EditNotificationsForm()
 | 
			
		||||
    # endregion forms
 | 
			
		||||
    # region handle edit profile settings form
 | 
			
		||||
    if edit_account_form.validate_on_submit():
 | 
			
		||||
        user.email = edit_account_form.email.data
 | 
			
		||||
        user.username = edit_account_form.username.data
 | 
			
		||||
    user = current_user
 | 
			
		||||
    update_account_information_form = UpdateAccountInformationForm()
 | 
			
		||||
    update_profile_information_form = UpdateProfileInformationForm()
 | 
			
		||||
    update_avatar_form = UpdateAvatarForm()
 | 
			
		||||
    update_password_form = UpdatePasswordForm()
 | 
			
		||||
    update_notifications_form = UpdateNotificationsForm()
 | 
			
		||||
 | 
			
		||||
    # region handle update profile information form
 | 
			
		||||
    if update_profile_information_form.submit.data and update_profile_information_form.validate():
 | 
			
		||||
        user.about_me = update_profile_information_form.about_me.data
 | 
			
		||||
        user.location = update_profile_information_form.location.data
 | 
			
		||||
        user.organization = update_profile_information_form.organization.data
 | 
			
		||||
        user.website = update_profile_information_form.website.data
 | 
			
		||||
        user.full_name = update_profile_information_form.full_name.data
 | 
			
		||||
        db.session.commit()
 | 
			
		||||
        flash('Your changes have been saved')
 | 
			
		||||
        return redirect(url_for('.settings'))
 | 
			
		||||
    # endregion handle update profile information form
 | 
			
		||||
 | 
			
		||||
    # region handle update avatar form
 | 
			
		||||
    if update_avatar_form.submit.data and update_avatar_form.validate():
 | 
			
		||||
        try:
 | 
			
		||||
            Avatar.create(
 | 
			
		||||
                update_avatar_form.avatar.data,
 | 
			
		||||
                user=user
 | 
			
		||||
            )
 | 
			
		||||
        except (AttributeError, OSError):
 | 
			
		||||
            abort(500)
 | 
			
		||||
        db.session.commit()
 | 
			
		||||
        flash('Your changes have been saved')
 | 
			
		||||
        return redirect(url_for('.settings'))
 | 
			
		||||
    # endregion handle update avatar form
 | 
			
		||||
 | 
			
		||||
    # region handle update account information form
 | 
			
		||||
    if update_account_information_form.submit.data and update_account_information_form.validate():
 | 
			
		||||
        user.email = update_account_information_form.email.data
 | 
			
		||||
        user.username = update_account_information_form.username.data
 | 
			
		||||
        db.session.commit()
 | 
			
		||||
        flash('Profile settings updated')
 | 
			
		||||
        return redirect(url_for('.settings'))
 | 
			
		||||
    # endregion handle edit profile settings forms
 | 
			
		||||
    # region handle edit public profile information form
 | 
			
		||||
    if edit_profile_form.validate_on_submit():
 | 
			
		||||
        if edit_profile_form.avatar.data:
 | 
			
		||||
            try:
 | 
			
		||||
                Avatar.create(
 | 
			
		||||
                    edit_profile_form.avatar.data,
 | 
			
		||||
                    user=user
 | 
			
		||||
                )
 | 
			
		||||
            except (AttributeError, OSError):
 | 
			
		||||
                abort(500)
 | 
			
		||||
        user.about_me = edit_profile_form.about_me.data
 | 
			
		||||
        user.location = edit_profile_form.location.data
 | 
			
		||||
        user.organization = edit_profile_form.organization.data
 | 
			
		||||
        user.website = edit_profile_form.website.data
 | 
			
		||||
        user.full_name = edit_profile_form.full_name.data
 | 
			
		||||
    # endregion handle update account information form
 | 
			
		||||
 | 
			
		||||
    # region handle update password form
 | 
			
		||||
    if update_password_form.submit.data and update_password_form.validate():
 | 
			
		||||
        user.password = update_password_form.new_password.data
 | 
			
		||||
        db.session.commit()
 | 
			
		||||
        flash('Your changes have been saved')
 | 
			
		||||
        return redirect(url_for('.settings'))
 | 
			
		||||
    # endregion handle edit public profile information form
 | 
			
		||||
    # region handle change_password_form POST
 | 
			
		||||
    if change_password_form.validate_on_submit():
 | 
			
		||||
        user.password = change_password_form.new_password.data
 | 
			
		||||
        db.session.commit()
 | 
			
		||||
        flash('Your changes have been saved')
 | 
			
		||||
        return redirect(url_for('.settings'))
 | 
			
		||||
    # endregion handle change_password_form POST
 | 
			
		||||
    # region handle edit_notification_settings_form POST
 | 
			
		||||
    if edit_notifications_form.validate_on_submit():
 | 
			
		||||
    # endregion handle update password form
 | 
			
		||||
 | 
			
		||||
    # region handle update notifications form
 | 
			
		||||
    if update_notifications_form.submit.data and update_notifications_form.validate():
 | 
			
		||||
        user.setting_job_status_mail_notification_level = \
 | 
			
		||||
            edit_notifications_form.job_status_mail_notification_level.data
 | 
			
		||||
            update_notifications_form.job_status_mail_notification_level.data
 | 
			
		||||
        db.session.commit()
 | 
			
		||||
        flash('Your changes have been saved')
 | 
			
		||||
        return redirect(url_for('.settings'))
 | 
			
		||||
    # endregion handle edit_notification_settings_form POST
 | 
			
		||||
    # endregion handle update notifications form
 | 
			
		||||
 | 
			
		||||
    return render_template(
 | 
			
		||||
        'settings/settings.html.j2',
 | 
			
		||||
        title='Settings',
 | 
			
		||||
        change_password_form=change_password_form,
 | 
			
		||||
        edit_account_form=edit_account_form,
 | 
			
		||||
        edit_notifications_form=edit_notifications_form,
 | 
			
		||||
        edit_profile_form=edit_profile_form,
 | 
			
		||||
        update_account_information_form=update_account_information_form,
 | 
			
		||||
        update_avatar_form=update_avatar_form,
 | 
			
		||||
        update_notifications_form=update_notifications_form,
 | 
			
		||||
        update_password_form=update_password_form,
 | 
			
		||||
        update_profile_information_form=update_profile_information_form,
 | 
			
		||||
        user=user
 | 
			
		||||
    )
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user