From f1d8b81923966ab35ba1ab5deea3d381cb9cebf7 Mon Sep 17 00:00:00 2001 From: Patrick Jentsch Date: Mon, 3 Apr 2023 15:25:55 +0200 Subject: [PATCH] move settings related json routes to users package --- app/admin/routes.py | 4 +- app/settings/__init__.py | 3 +- app/settings/utils.py | 6 --- app/static/js/Requests/settings/settings.js | 45 --------------------- app/static/js/Requests/users/users.js | 25 +++++++++++- app/templates/_scripts.html.j2 | 1 - app/templates/settings/settings.html.j2 | 12 +++--- app/users/__init__.py | 2 +- app/users/settings/__init__.py | 2 + app/{ => users}/settings/json_routes.py | 21 +++++----- 10 files changed, 47 insertions(+), 74 deletions(-) delete mode 100644 app/settings/utils.py delete mode 100644 app/static/js/Requests/settings/settings.js create mode 100644 app/users/settings/__init__.py rename app/{ => users}/settings/json_routes.py (79%) diff --git a/app/admin/routes.py b/app/admin/routes.py index 9a5401b8..3822dc28 100644 --- a/app/admin/routes.py +++ b/app/admin/routes.py @@ -65,10 +65,10 @@ def user_settings(user_id): user = User.query.get_or_404(user_id) update_account_information_form = UpdateAccountInformationForm(user=user) update_profile_information_form = UpdateProfileInformationForm(user=user) - update_avatar_form = UpdateAvatarForm(user=user) + update_avatar_form = UpdateAvatarForm() update_password_form = UpdatePasswordForm(user=user) update_notifications_form = UpdateNotificationsForm(user=user) - update_user_form = UpdateUserForm(user) + update_user_form = UpdateUserForm(user=user) # region handle update profile information form if update_profile_information_form.submit.data and update_profile_information_form.validate(): diff --git a/app/settings/__init__.py b/app/settings/__init__.py index aaa1cb1b..56265277 100644 --- a/app/settings/__init__.py +++ b/app/settings/__init__.py @@ -2,5 +2,4 @@ from flask import Blueprint bp = Blueprint('settings', __name__) -from . import routes, json_routes - +from . import routes diff --git a/app/settings/utils.py b/app/settings/utils.py deleted file mode 100644 index 53572740..00000000 --- a/app/settings/utils.py +++ /dev/null @@ -1,6 +0,0 @@ -from flask import request, url_for -from app.models import User - - -def user_endpoint_arguments_constructor(): - return {'user_id': request.view_args['user_id']} diff --git a/app/static/js/Requests/settings/settings.js b/app/static/js/Requests/settings/settings.js deleted file mode 100644 index d3137267..00000000 --- a/app/static/js/Requests/settings/settings.js +++ /dev/null @@ -1,45 +0,0 @@ -/***************************************************************************** -* Settings * -* Fetch requests for /settings routes * -*****************************************************************************/ -Requests.settings = {}; - -Requests.settings.entity = {}; - -Requests.settings.entity.delete = (userId) => { - let input = `/settings/${userId}`; - let init = { - method: 'DELETE' - }; - return Requests.JSONfetch(input, init); -} - -Requests.settings.entity.deleteAvatar = (userId) => { - let input = `/settings/${userId}/avatar`; - let init = { - method: 'DELETE' - }; - return Requests.JSONfetch(input, init); -} - -Requests.settings.entity.isPublic = {}; - -Requests.settings.entity.isPublic.update = (userId, isPublic) => { - let input = `/settings/${userId}/is-public`; - let init = { - method: 'PUT', - body: JSON.stringify(isPublic) - }; - return Requests.JSONfetch(input, init); -}; - -Requests.settings.entity.profilePrivacySettings = {}; - -Requests.settings.entity.profilePrivacySettings.update = (userId, profilePrivacySetting, enabled) => { - let input = `/settings/${userId}/profile-privacy-settings/${profilePrivacySetting}`; - let init = { - method: 'PUT', - body: JSON.stringify(enabled) - }; - return Requests.JSONfetch(input, init); -} diff --git a/app/static/js/Requests/users/users.js b/app/static/js/Requests/users/users.js index 00adbee9..9ed77cb0 100644 --- a/app/static/js/Requests/users/users.js +++ b/app/static/js/Requests/users/users.js @@ -6,10 +6,33 @@ Requests.users = {}; Requests.users.entity = {}; -Requests.settings.entity.delete = (userId) => { +Requests.users.entity.delete = (userId) => { let input = `/users/${userId}`; let init = { method: 'DELETE' }; return Requests.JSONfetch(input, init); }; + +Requests.users.entity.settings = {}; + +Requests.users.entity.settings.avatar = {}; + +Requests.users.entity.settings.avatar.delete = (userId) => { + let input = `/users/${userId}/settings/avatar`; + let init = { + method: 'DELETE' + }; + return Requests.JSONfetch(input, init); +} + +Requests.users.entity.settings.profilePrivacy = {}; + +Requests.users.entity.settings.profilePrivacy.update = (userId, profilePrivacySetting, enabled) => { + let input = `/users/${userId}/settings/profile-privacy/${profilePrivacySetting}`; + let init = { + method: 'PUT', + body: JSON.stringify(enabled) + }; + return Requests.JSONfetch(input, init); +} diff --git a/app/templates/_scripts.html.j2 b/app/templates/_scripts.html.j2 index 089a7cd2..dbc26470 100644 --- a/app/templates/_scripts.html.j2 +++ b/app/templates/_scripts.html.j2 @@ -66,7 +66,6 @@ 'js/Requests/corpora/files.js', 'js/Requests/corpora/followers.js', 'js/Requests/jobs/jobs.js', - 'js/Requests/settings/settings.js', 'js/Requests/users/users.js' %} diff --git a/app/templates/settings/settings.html.j2 b/app/templates/settings/settings.html.j2 index 27c9590c..b267dd44 100644 --- a/app/templates/settings/settings.html.j2 +++ b/app/templates/settings/settings.html.j2 @@ -212,7 +212,7 @@ avatarUploadElement.addEventListener('change', () => { }); deleteAvatarButtonElement.addEventListener('click', () => { - Requests.settings.entity.deleteAvatar({{ user.hashid|tojson }}) + Requests.users.entity.settings.avatar.delete({{ user.hashid|tojson }}) .then( (response) => { avatarPreviewElement.src = {{ url_for('static', filename='images/user_avatar.png')|tojson }}; @@ -245,16 +245,16 @@ for (let collapsibleElement of document.querySelectorAll('.collapsible.no-autoin let profileIsPublicSwitchElement = document.querySelector('#profile-is-public-switch'); let profilePrivacySettingCheckboxElements = document.querySelectorAll('.profile-privacy-setting-checkbox'); profileIsPublicSwitchElement.addEventListener('change', (event) => { - let newIsPublic = profileIsPublicSwitchElement.checked; - Requests.settings.entity.isPublic.update({{ user.hashid|tojson }}, newIsPublic) + let newEnabled = profileIsPublicSwitchElement.checked; + Requests.users.entity.settings.profilePrivacy.update({{ user.hashid|tojson }}, 'is-public', newEnabled) .then( (response) => { for (let profilePrivacySettingCheckboxElement of document.querySelectorAll('.profile-privacy-setting-checkbox')) { - profilePrivacySettingCheckboxElement.disabled = !newIsPublic; + profilePrivacySettingCheckboxElement.disabled = !newEnabled; } }, (response) => { - profileIsPublicSwitchElement.checked = !newIsPublic; + profileIsPublicSwitchElement.checked = !newEnabled; } ); }); @@ -262,7 +262,7 @@ for (let profilePrivacySettingCheckboxElement of profilePrivacySettingCheckboxEl profilePrivacySettingCheckboxElement.addEventListener('change', (event) => { let newEnabled = profilePrivacySettingCheckboxElement.checked; let valueName = profilePrivacySettingCheckboxElement.dataset.profilePrivacySettingName; - Requests.settings.entity.profilePrivacySettings.update({{ user.hashid|tojson }}, valueName, newEnabled) + Requests.users.entity.settings.profilePrivacy.update({{ user.hashid|tojson }}, valueName, newEnabled) .catch((response) => { profilePrivacySettingCheckboxElement.checked = !newEnabled; }); diff --git a/app/users/__init__.py b/app/users/__init__.py index ecd6b9da..11a38f25 100644 --- a/app/users/__init__.py +++ b/app/users/__init__.py @@ -3,4 +3,4 @@ from flask import Blueprint bp = Blueprint('users', __name__) from . import events, routes - +from . import settings diff --git a/app/users/settings/__init__.py b/app/users/settings/__init__.py new file mode 100644 index 00000000..1dbe44f0 --- /dev/null +++ b/app/users/settings/__init__.py @@ -0,0 +1,2 @@ +from .. import bp +from . import json_routes diff --git a/app/settings/json_routes.py b/app/users/settings/json_routes.py similarity index 79% rename from app/settings/json_routes.py rename to app/users/settings/json_routes.py index 31002d26..acc7dbc9 100644 --- a/app/settings/json_routes.py +++ b/app/users/settings/json_routes.py @@ -1,5 +1,5 @@ from flask import abort, current_app, request -from flask_login import current_user, login_required, logout_user +from flask_login import login_required from threading import Thread import os from app import db @@ -8,14 +8,15 @@ from app.models import Avatar, User, ProfilePrivacySettings from . import bp -@bp.route('//avatar', methods=['DELETE']) +@bp.route('//settings/avatar', methods=['DELETE']) @content_negotiation(produces='application/json') -def delete_profile_avatar(user_id): +def delete_user_avatar(user_id): def _delete_avatar(app, avatar_id): with app.app_context(): avatar = Avatar.query.get(avatar_id) avatar.delete() db.session.commit() + user = User.query.get_or_404(user_id) if user.avatar is None: abort(404) @@ -30,27 +31,27 @@ def delete_profile_avatar(user_id): return response_data, 202 -@bp.route('//is-public', methods=['PUT']) +@bp.route('//settings/profile-privacy/is-public', methods=['PUT']) @login_required @content_negotiation(consumes='application/json', produces='application/json') -def update_user_is_public(user_id): +def update_user_profile_privacy_setting_is_public(user_id): + user = User.query.get_or_404(user_id) is_public = request.json if not isinstance(is_public, bool): abort(400) - user = User.query.get_or_404(user_id) user.is_public = is_public db.session.commit() response_data = { 'message': 'Profile privacy settings updated', - 'category': 'corpus' + 'category': 'settings' } return response_data, 200 -@bp.route('//profile-privacy-settings/', methods=['PUT']) +@bp.route('//settings/profile-privacy/', methods=['PUT']) @login_required @content_negotiation(consumes='application/json', produces='application/json') -def add_profile_privacy_settings(user_id, profile_privacy_setting_name): +def update_user_profile_privacy_settings(user_id, profile_privacy_setting_name): user = User.query.get_or_404(user_id) enabled = request.json if not isinstance(enabled, bool): @@ -66,6 +67,6 @@ def add_profile_privacy_settings(user_id, profile_privacy_setting_name): db.session.commit() response_data = { 'message': 'Profile privacy settings updated', - 'category': 'corpus' + 'category': 'settings' } return response_data, 200