move settings related json routes to users package

This commit is contained in:
Patrick Jentsch 2023-04-03 15:25:55 +02:00
parent 2289106ac7
commit f1d8b81923
10 changed files with 47 additions and 74 deletions

View File

@ -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():

View File

@ -2,5 +2,4 @@ from flask import Blueprint
bp = Blueprint('settings', __name__)
from . import routes, json_routes
from . import routes

View File

@ -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']}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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'
%}
<script src="{{ ASSET_URL }}"></script>

View File

@ -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;
});

View File

@ -3,4 +3,4 @@ from flask import Blueprint
bp = Blueprint('users', __name__)
from . import events, routes
from . import settings

View File

@ -0,0 +1,2 @@
from .. import bp
from . import json_routes

View File

@ -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('/<hashid:user_id>/avatar', methods=['DELETE'])
@bp.route('/<hashid:user_id>/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('/<hashid:user_id>/is-public', methods=['PUT'])
@bp.route('/<hashid:user_id>/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('/<hashid:user_id>/profile-privacy-settings/<string:profile_privacy_setting_name>', methods=['PUT'])
@bp.route('/<hashid:user_id>/settings/profile-privacy/<string:profile_privacy_setting_name>', 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