From 77fc8a42f11bd6f2bfb277c762f2e189bb164cb2 Mon Sep 17 00:00:00 2001 From: Patrick Jentsch Date: Thu, 6 Apr 2023 08:42:21 +0200 Subject: [PATCH] Move the last bits of the settings package to the user package --- app/static/js/Requests/users/settings.js | 16 ++++++++++ app/static/js/Requests/users/users.js | 19 ++---------- app/templates/_scripts.html.j2 | 3 +- app/templates/users/settings/settings.html.j2 | 2 +- app/users/__init__.py | 2 +- app/users/json_routes.py | 29 +++++++++++++++-- app/users/settings/json_routes.py | 31 ++----------------- 7 files changed, 52 insertions(+), 50 deletions(-) create mode 100644 app/static/js/Requests/users/settings.js diff --git a/app/static/js/Requests/users/settings.js b/app/static/js/Requests/users/settings.js new file mode 100644 index 00000000..0a92a09c --- /dev/null +++ b/app/static/js/Requests/users/settings.js @@ -0,0 +1,16 @@ +/***************************************************************************** +* Settings * +* Fetch requests for /users//settings routes * +*****************************************************************************/ +Requests.users.entity.settings = {}; + +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/static/js/Requests/users/users.js b/app/static/js/Requests/users/users.js index 9ed77cb0..053fd687 100644 --- a/app/static/js/Requests/users/users.js +++ b/app/static/js/Requests/users/users.js @@ -14,25 +14,12 @@ Requests.users.entity.delete = (userId) => { return Requests.JSONfetch(input, init); }; -Requests.users.entity.settings = {}; +Requests.users.entity.avatar = {}; -Requests.users.entity.settings.avatar = {}; - -Requests.users.entity.settings.avatar.delete = (userId) => { - let input = `/users/${userId}/settings/avatar`; +Requests.users.entity.avatar.delete = (userId) => { + let input = `/users/${userId}/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 dbc26470..9dd81933 100644 --- a/app/templates/_scripts.html.j2 +++ b/app/templates/_scripts.html.j2 @@ -66,7 +66,8 @@ 'js/Requests/corpora/files.js', 'js/Requests/corpora/followers.js', 'js/Requests/jobs/jobs.js', - 'js/Requests/users/users.js' + 'js/Requests/users/users.js', + 'js/Requests/users/settings.js' %} {%- endassets %} diff --git a/app/templates/users/settings/settings.html.j2 b/app/templates/users/settings/settings.html.j2 index 91b2864d..f862840d 100644 --- a/app/templates/users/settings/settings.html.j2 +++ b/app/templates/users/settings/settings.html.j2 @@ -213,7 +213,7 @@ avatarUploadElement.addEventListener('change', () => { }); document.querySelector('#delete-avatar').addEventListener('click', () => { - Requests.users.entity.settings.avatar.delete({{ user.hashid|tojson }}) + Requests.users.entity.avatar.delete({{ user.hashid|tojson }}) .then( (response) => { avatarPreviewElement.src = {{ url_for('static', filename='images/user_avatar.png')|tojson }}; diff --git a/app/users/__init__.py b/app/users/__init__.py index 11a38f25..a1ed4f2e 100644 --- a/app/users/__init__.py +++ b/app/users/__init__.py @@ -2,5 +2,5 @@ from flask import Blueprint bp = Blueprint('users', __name__) -from . import events, routes +from . import events, json_routes, routes from . import settings diff --git a/app/users/json_routes.py b/app/users/json_routes.py index d228f8f3..571fa78c 100644 --- a/app/users/json_routes.py +++ b/app/users/json_routes.py @@ -3,7 +3,7 @@ from flask_login import current_user, login_required, logout_user from threading import Thread from app import db from app.decorators import content_negotiation -from app.models import User +from app.models import Avatar, User from . import bp @@ -22,7 +22,7 @@ def delete_user(user_id): abort(403) thread = Thread( target=_delete_user, - args=(current_app._get_current_object(), user_id) + args=(current_app._get_current_object(), user.id) ) if user == current_user: logout_user() @@ -31,3 +31,28 @@ def delete_user(user_id): 'message': f'User "{user.username}" marked for deletion' } return response_data, 202 + + +@bp.route('//avatar', methods=['DELETE']) +@content_negotiation(produces='application/json') +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) + if not (user == current_user or current_user.is_administrator()): + abort(403) + thread = Thread( + target=_delete_avatar, + args=(current_app._get_current_object(), user.avatar.id) + ) + thread.start() + response_data = { + 'message': f'Avatar marked for deletion' + } + return response_data, 202 diff --git a/app/users/settings/json_routes.py b/app/users/settings/json_routes.py index 7fdfa7fc..1d0c4d9e 100644 --- a/app/users/settings/json_routes.py +++ b/app/users/settings/json_routes.py @@ -1,38 +1,11 @@ -from flask import abort, current_app, request +from flask import abort, request from flask_login import current_user, login_required -from threading import Thread -import os from app import db from app.decorators import content_negotiation -from app.models import Avatar, User, ProfilePrivacySettings +from app.models import User, ProfilePrivacySettings from . import bp -@bp.route('//settings/avatar', methods=['DELETE']) -@content_negotiation(produces='application/json') -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) - if not (user == current_user or current_user.is_administrator()): - abort(403) - thread = Thread( - target=_delete_avatar, - args=(current_app._get_current_object(), user.avatar.id) - ) - thread.start() - response_data = { - 'message': f'Avatar marked for deletion' - } - return response_data, 202 - - @bp.route('//settings/profile-privacy/is-public', methods=['PUT']) @login_required @content_negotiation(consumes='application/json', produces='application/json')