From 5b6eae76455e1b9f59a4c8d93fb3cf153b90afb9 Mon Sep 17 00:00:00 2001 From: Patrick Jentsch Date: Thu, 23 Mar 2023 17:42:51 +0100 Subject: [PATCH] Cleanup --- app/admin/routes.py | 32 ++++--- app/settings/forms.py | 55 ++++++++++-- app/settings/routes.py | 100 +++++++++------------ app/templates/admin/corpora.html.j2 | 9 +- app/templates/admin/user.html.j2 | 20 +++-- app/templates/admin/users.html.j2 | 9 +- app/templates/settings/settings.html.j2 | 110 ++++++++++-------------- 7 files changed, 177 insertions(+), 158 deletions(-) diff --git a/app/admin/routes.py b/app/admin/routes.py index c1014250..0b4ac8fc 100644 --- a/app/admin/routes.py +++ b/app/admin/routes.py @@ -2,8 +2,8 @@ from flask import flash, redirect, render_template, request, url_for from flask_breadcrumbs import register_breadcrumb from app import db, hashids from app.models import Corpus, Role, User, UserSettingJobStatusMailNotificationLevel -from app.settings.forms import EditNotificationSettingsForm -from app.settings.forms import EditProfileSettingsForm +from app.settings.forms import EditNotificationsForm +from app.settings.forms import EditAccountForm from . import bp from .forms import AdminEditUserForm from app.users.utils import ( @@ -24,22 +24,22 @@ def admin(): @bp.route('/corpora') @register_breadcrumb(bp, '.corpora', 'Corpora') def corpora(): - corpora = [x.to_json_serializeable(backrefs=True) for x in Corpus.query.all()] + corpora = Corpus.query.all() return render_template( 'admin/corpora.html.j2', - corpora=corpora, - title='Corpora' + title='Corpora', + corpora=corpora ) @bp.route('/users') @register_breadcrumb(bp, '.users', 'groupUsers') def users(): - users = [x.to_json_serializeable(backrefs=True) for x in User.query.all()] + users = User.query.all() return render_template( 'admin/users.html.j2', - users=users, - title='Users' + title='Users', + users=users ) @@ -47,13 +47,11 @@ def users(): @register_breadcrumb(bp, '.users.entity', '', dynamic_list_constructor=user_dlc) def user(user_id): user = User.query.get_or_404(user_id) - return render_template('admin/user.html.j2', title=user.username, user=user) - - -@bp.route('/users//dashboard') -@register_breadcrumb(bp, '.users.entity.dashboard', 'Dashboard', endpoint_arguments_constructor=user_eac) -def user_dashboard(user_id): - return render_template('main/dashboard.html.j2', title='Dashboard') + return render_template( + 'admin/user.html.j2', + title=user.username, + user=user + ) @bp.route('/users//edit', methods=['GET', 'POST']) @@ -64,12 +62,12 @@ def edit_user(user_id): data={'confirmed': user.confirmed, 'role': user.role.hashid}, prefix='admin-edit-user-form' ) - edit_profile_settings_form = EditProfileSettingsForm( + edit_profile_settings_form = EditAccountForm( user, data=user.to_json_serializeable(), prefix='edit-profile-settings-form' ) - edit_notification_settings_form = EditNotificationSettingsForm( + edit_notification_settings_form = EditNotificationsForm( data=user.to_json_serializeable(), prefix='edit-notification-settings-form' ) diff --git a/app/settings/forms.py b/app/settings/forms.py index a814feee..7a38e074 100644 --- a/app/settings/forms.py +++ b/app/settings/forms.py @@ -1,3 +1,4 @@ +from flask_login import current_user from flask_wtf import FlaskForm from wtforms import ( BooleanField, @@ -20,7 +21,8 @@ from app.models import User, UserSettingJobStatusMailNotificationLevel from app.auth import USERNAME_REGEX from app.wtf_validators import FileSizeLimit -class EditProfileSettingsForm(FlaskForm): + +class EditAccountForm(FlaskForm): email = StringField( 'E-Mail', validators=[DataRequired(), Length(max=254), Email()] @@ -41,7 +43,12 @@ class EditProfileSettingsForm(FlaskForm): ) submit = SubmitField() - def __init__(self, user, *args, **kwargs): + 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 @@ -55,7 +62,11 @@ class EditProfileSettingsForm(FlaskForm): and User.query.filter_by(username=field.data).first()): raise ValidationError('Username already in use') -class EditPublicProfileInformationForm(FlaskForm): + def validate_on_submit(self): + return self.submit.data and self.validate() + + +class EditProfileForm(FlaskForm): show_email = BooleanField('Email') show_last_seen = BooleanField('Last seen') show_member_since = BooleanField('Member since') @@ -67,13 +78,11 @@ class EditPublicProfileInformationForm(FlaskForm): 'Full name', validators=[Length(max=128)] ) - style={'style': 'overflow: auto;'} about_me = TextAreaField( 'About me', validators=[ Length(max=254) - ], - render_kw=style + ] ) website = StringField( 'Website', @@ -93,13 +102,24 @@ class EditPublicProfileInformationForm(FlaskForm): 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( @@ -118,7 +138,10 @@ class ChangePasswordForm(FlaskForm): ) submit = SubmitField() - def __init__(self, user, *args, **kwargs): + 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 @@ -126,8 +149,11 @@ class ChangePasswordForm(FlaskForm): 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 EditNotificationSettingsForm(FlaskForm): + +class EditNotificationsForm(FlaskForm): job_status_mail_notification_level = SelectField( 'Job status mail notification level', choices=[ @@ -137,3 +163,14 @@ class EditNotificationSettingsForm(FlaskForm): 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() diff --git a/app/settings/routes.py b/app/settings/routes.py index a4e1e124..dba854b0 100644 --- a/app/settings/routes.py +++ b/app/settings/routes.py @@ -1,20 +1,14 @@ -from flask import ( - abort, - flash, - redirect, - render_template, - url_for -) +from flask import abort, flash, redirect, render_template, url_for from flask_breadcrumbs import register_breadcrumb from flask_login import current_user, login_required from app import db -from app.models import Avatar, ProfilePrivacySettings +from app.models import Avatar from . import bp from .forms import ( ChangePasswordForm, - EditNotificationSettingsForm, - EditProfileSettingsForm, - EditPublicProfileInformationForm + EditNotificationsForm, + EditAccountForm, + EditProfileForm ) @@ -22,81 +16,73 @@ from .forms import ( @register_breadcrumb(bp, '.', 'settingsSettings') @login_required def settings(): + user = current_user._get_current_object() # region forms - edit_profile_settings_form = EditProfileSettingsForm( - current_user, - data=current_user.to_json_serializeable(), - prefix='edit-profile-settings-form' - ) - edit_public_profile_information_form = EditPublicProfileInformationForm( - data=current_user.to_json_serializeable(), - prefix='edit-public-profile-information-form' - ) - change_password_form = ChangePasswordForm( - current_user, - prefix='change-password-form' - ) - edit_notification_settings_form = EditNotificationSettingsForm( - data=current_user.to_json_serializeable(), - prefix='edit-notification-settings-form' - ) + 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_profile_settings_form.validate_on_submit(): - current_user.email = edit_profile_settings_form.email.data - current_user.username = edit_profile_settings_form.username.data + if edit_account_form.validate_on_submit(): + user.email = edit_account_form.email.data + user.username = edit_account_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_public_profile_information_form.submit.data and edit_public_profile_information_form.validate(): - print(edit_public_profile_information_form.show_email.data) - if edit_public_profile_information_form.show_email.data: - current_user.add_profile_privacy_setting(ProfilePrivacySettings.SHOW_EMAIL) + if edit_profile_form.validate_on_submit(): + if edit_profile_form.show_email.data: + user.add_profile_privacy_setting('SHOW_EMAIL') else: - current_user.remove_profile_privacy_setting(ProfilePrivacySettings.SHOW_EMAIL) - if edit_public_profile_information_form.show_last_seen.data: - current_user.add_profile_privacy_setting(ProfilePrivacySettings.SHOW_LAST_SEEN) + user.remove_profile_privacy_setting('SHOW_EMAIL') + if edit_profile_form.show_last_seen.data: + user.add_profile_privacy_setting('SHOW_LAST_SEEN') else: - current_user.remove_profile_privacy_setting(ProfilePrivacySettings.SHOW_LAST_SEEN) - if edit_public_profile_information_form.show_member_since.data: - current_user.add_profile_privacy_setting(ProfilePrivacySettings.SHOW_MEMBER_SINCE) + user.remove_profile_privacy_setting('SHOW_LAST_SEEN') + if edit_profile_form.show_member_since.data: + user.add_profile_privacy_setting('SHOW_MEMBER_SINCE') else: - current_user.remove_profile_privacy_setting(ProfilePrivacySettings.SHOW_MEMBER_SINCE) - if edit_public_profile_information_form.avatar.data: + user.remove_profile_privacy_setting('SHOW_MEMBER_SINCE') + if edit_profile_form.avatar.data: try: - Avatar.create(edit_public_profile_information_form.avatar.data, user=current_user) + Avatar.create( + edit_profile_form.avatar.data, + user=user + ) except (AttributeError, OSError): abort(500) - current_user.about_me = edit_public_profile_information_form.about_me.data - current_user.location = edit_public_profile_information_form.location.data - current_user.organization = edit_public_profile_information_form.organization.data - current_user.website = edit_public_profile_information_form.website.data - current_user.full_name = edit_public_profile_information_form.full_name.data + 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 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.submit.data and change_password_form.validate(): - current_user.password = change_password_form.new_password.data + 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_notification_settings_form.submit and edit_notification_settings_form.validate(): - current_user.setting_job_status_mail_notification_level = edit_notification_settings_form.job_status_mail_notification_level.data + if edit_notifications_form.validate_on_submit(): + user.setting_job_status_mail_notification_level = \ + edit_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 return render_template( 'settings/settings.html.j2', - edit_profile_settings_form=edit_profile_settings_form, - edit_public_profile_information_form=edit_public_profile_information_form, + title='Settings', change_password_form=change_password_form, - edit_notification_settings_form=edit_notification_settings_form, - title='Settings' + edit_account_form=edit_account_form, + edit_notifications_form=edit_notifications_form, + edit_profile_form=edit_profile_form, + user=user ) diff --git a/app/templates/admin/corpora.html.j2 b/app/templates/admin/corpora.html.j2 index 99fcc661..cc4a177c 100644 --- a/app/templates/admin/corpora.html.j2 +++ b/app/templates/admin/corpora.html.j2 @@ -18,6 +18,13 @@ {% endblock scripts %} diff --git a/app/templates/admin/user.html.j2 b/app/templates/admin/user.html.j2 index f8943430..403828f2 100644 --- a/app/templates/admin/user.html.j2 +++ b/app/templates/admin/user.html.j2 @@ -9,12 +9,18 @@

{{ title }}

- {{ user.role.name }} - {{ 'confirmed' if user.confirmed else 'unconfirmed' }} -

{{ user.about_me }}

+

+ {{ user.role.name }} + {% if user.confirmed %} + confirmed + {% else %} + unconfirmed + {% endif %} +

+

{{ user.about_me }}

-
 
+
 
    @@ -34,8 +40,8 @@
  • Email: {{ user.email }}
  • Id: {{ user.id }}
  • Hashid: {{ user.hashid }}
  • -
  • Member since: {{ user.member_since }}
  • -
  • Last seen: {{ user.last_seen }}
  • +
  • Member since: {{ user.member_since.strftime('%Y-%m-%d') }}
  • +
  • Last seen: {{ user.last_seen.strftime('%Y-%m-%d') }}
@@ -89,7 +95,7 @@

Do you really want to delete the user {{ user.username }}? All associated data will be permanently deleted!

diff --git a/app/templates/admin/users.html.j2 b/app/templates/admin/users.html.j2 index 25b27c95..a71c8f59 100644 --- a/app/templates/admin/users.html.j2 +++ b/app/templates/admin/users.html.j2 @@ -23,6 +23,13 @@ {% endblock scripts %} diff --git a/app/templates/settings/settings.html.j2 b/app/templates/settings/settings.html.j2 index 282d4185..b219d3e6 100644 --- a/app/templates/settings/settings.html.j2 +++ b/app/templates/settings/settings.html.j2 @@ -18,7 +18,7 @@

-
    +
    • User Settings @@ -28,48 +28,26 @@
      - {{ edit_profile_settings_form.hidden_tag() }} - {{ wtf.render_field(edit_profile_settings_form.username, material_icon='person') }} - {{ wtf.render_field(edit_profile_settings_form.email, material_icon='email') }} + {{ edit_account_form.hidden_tag() }} + {{ wtf.render_field(edit_account_form.username, material_icon='person') }} + {{ wtf.render_field(edit_account_form.email, material_icon='email') }}
      - {{ wtf.render_field(edit_profile_settings_form.submit, material_icon='send') }} + {{ wtf.render_field(edit_account_form.submit, material_icon='send') }}
    • - {#
    • -
      Privacy Settingskeyboard_arrow_right
      -
      -
      - {{ edit_privacy_settings_form.hidden_tag() }} -
      - {{ wtf.render_field(edit_privacy_settings_form.is_public, id='public-profile') }} -
      -
      -
      - {{ wtf.render_field(edit_privacy_settings_form.show_email, data_action='disable', disabled=true) }} -
      - {{ wtf.render_field(edit_privacy_settings_form.show_last_seen, data_action='disable', disabled=true) }} -
      - {{ wtf.render_field(edit_privacy_settings_form.show_member_since, data_action='disable', disabled=true) }} -
      -
      - {{ wtf.render_field(edit_privacy_settings_form.submit, material_icon='send') }} -
      -
      -
      -
    • #}
    • Public Profilekeyboard_arrow_right
      - {{ edit_public_profile_information_form.hidden_tag() }} + {{ edit_profile_form.hidden_tag() }}
      @@ -82,42 +60,42 @@


      - {{ wtf.render_field(edit_public_profile_information_form.full_name, material_icon='badge') }} - {{ wtf.render_field(edit_public_profile_information_form.about_me, material_icon='description', id='about-me-textfield') }} - {{ wtf.render_field(edit_public_profile_information_form.website, material_icon='laptop') }} - {{ wtf.render_field(edit_public_profile_information_form.organization, material_icon='business') }} - {{ wtf.render_field(edit_public_profile_information_form.location, material_icon='location_on') }} + {{ wtf.render_field(edit_profile_form.full_name, material_icon='badge') }} + {{ wtf.render_field(edit_profile_form.about_me, material_icon='description', id='about-me-textfield') }} + {{ wtf.render_field(edit_profile_form.website, material_icon='laptop') }} + {{ wtf.render_field(edit_profile_form.organization, material_icon='business') }} + {{ wtf.render_field(edit_profile_form.location, material_icon='location_on') }}

      - user-image + user-image
      - {{ wtf.render_field(edit_public_profile_information_form.avatar, accept='image/jpeg, image/png, image/gif', placeholder='Choose an image file', id='avatar-upload') }} + {{ wtf.render_field(edit_profile_form.avatar, accept='image/jpeg, image/png, image/gif', placeholder='Choose an image file', id='avatar-upload') }}
      delete @@ -126,7 +104,7 @@

      - {{ wtf.render_field(edit_public_profile_information_form.submit, material_icon='send') }} + {{ wtf.render_field(edit_profile_form.submit, material_icon='send') }}
      @@ -144,7 +122,7 @@

      -
        +
        • Notification Settings @@ -152,10 +130,10 @@
          - {{ edit_notification_settings_form.hidden_tag() }} - {{ wtf.render_field(edit_notification_settings_form.job_status_mail_notification_level, material_icon='notifications') }} + {{ edit_notifications_form.hidden_tag() }} + {{ wtf.render_field(edit_notifications_form.job_status_mail_notification_level, material_icon='notifications') }}
          - {{ wtf.render_field(edit_notification_settings_form.submit, material_icon='send') }} + {{ wtf.render_field(edit_notifications_form.submit, material_icon='send') }}
          @@ -204,7 +182,7 @@