Move delete user method in users package

This commit is contained in:
Patrick Jentsch 2023-03-30 13:36:11 +02:00
parent 9ce5ff8cba
commit 5c776e0fb6
6 changed files with 53 additions and 30 deletions

View File

@ -8,32 +8,6 @@ from app.models import Avatar, User, ProfilePrivacySettings
from . import bp
@bp.route('/<hashid:user_id>', methods=['DELETE'])
@login_required
@content_negotiation(produces='application/json')
def delete_user(user_id):
def _delete_user(app, user_id):
with app.app_context():
user = User.query.get(user_id)
user.delete()
db.session.commit()
user = User.query.get_or_404(user_id)
if not (user == current_user or current_user.is_administrator()):
abort(403)
thread = Thread(
target=_delete_user,
args=(current_app._get_current_object(), user_id)
)
if user == current_user:
logout_user()
thread.start()
response_data = {
'message': f'User "{user.username}" marked for deletion'
}
return response_data, 202
@bp.route('/<hashid:user_id>/avatar', methods=['DELETE'])
@content_negotiation(produces='application/json')
def delete_profile_avatar(user_id):

View File

@ -1,6 +1,6 @@
/*****************************************************************************
* Users *
* Fetch requests for /users routes *
* Settings *
* Fetch requests for /settings routes *
*****************************************************************************/
Requests.settings = {};

View File

@ -0,0 +1,15 @@
/*****************************************************************************
* Users *
* Fetch requests for /users routes *
*****************************************************************************/
Requests.users = {};
Requests.users.entity = {};
Requests.settings.entity.delete = (userId) => {
let input = `/users/${userId}`;
let init = {
method: 'DELETE'
};
return Requests.JSONfetch(input, init);
};

View File

@ -66,7 +66,8 @@
'js/Requests/corpora/files.js',
'js/Requests/corpora/followers.js',
'js/Requests/jobs/jobs.js',
'js/Requests/settings/settings.js'
'js/Requests/settings/settings.js',
'js/Requests/users/users.js'
%}
<script src="{{ ASSET_URL }}"></script>
{%- endassets %}

View File

@ -221,7 +221,7 @@ deleteAvatarButtonElement.addEventListener('click', () => {
});
document.querySelector('#delete-user').addEventListener('click', (event) => {
Requests.settings.entity.delete({{ user.hashid|tojson }})
Requests.users.entity.delete({{ user.hashid|tojson }})
.then((response) => {window.location.href = '/';});
});

33
app/users/json_routes.py Normal file
View File

@ -0,0 +1,33 @@
from flask import abort, current_app
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 . import bp
@bp.route('/<hashid:user_id>', methods=['DELETE'])
@login_required
@content_negotiation(produces='application/json')
def delete_user(user_id):
def _delete_user(app, user_id):
with app.app_context():
user = User.query.get(user_id)
user.delete()
db.session.commit()
user = User.query.get_or_404(user_id)
if not (user == current_user or current_user.is_administrator()):
abort(403)
thread = Thread(
target=_delete_user,
args=(current_app._get_current_object(), user_id)
)
if user == current_user:
logout_user()
thread.start()
response_data = {
'message': f'User "{user.username}" marked for deletion'
}
return response_data, 202