From 83c05d93b0e5a0f58657c342e46834475b6ae7c3 Mon Sep 17 00:00:00 2001 From: Patrick Jentsch Date: Thu, 23 Apr 2020 08:24:11 +0200 Subject: [PATCH] Make delete user function a background task --- app/admin/views.py | 9 +++------ app/models.py | 11 +---------- app/profile/background_functions.py | 9 --------- app/profile/tasks.py | 18 ++++++++++++++++++ app/profile/views.py | 9 +++------ docker-compose.yml | 1 - 6 files changed, 25 insertions(+), 32 deletions(-) delete mode 100644 app/profile/background_functions.py create mode 100644 app/profile/tasks.py diff --git a/app/admin/views.py b/app/admin/views.py index 5ca0b74b..45b06820 100644 --- a/app/admin/views.py +++ b/app/admin/views.py @@ -1,13 +1,12 @@ -from flask import current_app, flash, redirect, render_template, url_for +from flask import flash, redirect, render_template, url_for from flask_login import login_required -from threading import Thread from . import admin from .forms import EditUserForm from .tables import AdminUserItem, AdminUserTable from .. import db from ..decorators import admin_required from ..models import Role, User -from ..profile.background_functions import delete_user_ +from ..profile import tasks as profile_tasks @admin.route('/') @@ -39,9 +38,7 @@ def user(user_id): @admin_required def delete_user(user_id): user = User.query.get_or_404(user_id) - thread = Thread(target=delete_user_, - args=(current_app._get_current_object(), user.id)) - thread.start() + profile_tasks.delete_user(user_id) flash('User has been deleted!') return redirect(url_for('admin.index')) diff --git a/app/models.py b/app/models.py index 0df3ded0..03d23892 100644 --- a/app/models.py +++ b/app/models.py @@ -4,9 +4,7 @@ from flask_login import UserMixin, AnonymousUserMixin from itsdangerous import BadSignature, TimedJSONWebSignatureSerializer from werkzeug.security import generate_password_hash, check_password_hash from werkzeug.utils import secure_filename -from . import db, logger, login_manager -import os -import shutil +from . import db, login_manager class Permission: @@ -212,13 +210,6 @@ class User(UserMixin, db.Model): job.delete() for corpus in self.corpora: corpus.delete() - path = os.path.join(current_app.config['NOPAQUE_STORAGE'], - str(self.id)) - try: - shutil.rmtree(path) - except Exception as e: - logger.warning(e) - pass db.session.delete(self) db.session.commit() diff --git a/app/profile/background_functions.py b/app/profile/background_functions.py deleted file mode 100644 index e8e7f2af..00000000 --- a/app/profile/background_functions.py +++ /dev/null @@ -1,9 +0,0 @@ -from ..models import User - - -def delete_user_(app, user_id): - with app.app_context(): - user = User.query.get(user_id) - if user is None: - raise Exception('User {} not found!'.format(user_id)) - user.delete() diff --git a/app/profile/tasks.py b/app/profile/tasks.py new file mode 100644 index 00000000..c27d6b3b --- /dev/null +++ b/app/profile/tasks.py @@ -0,0 +1,18 @@ +from .. import logger +from ..decorators import background +from ..models import User +import os +import shutil + + +@background +def delete_user(app, user_id): + logger.warning('aufgerufen') + with app.app_context(): + user = User.query.get(user_id) + if user is None: + raise Exception('User {} not found!'.format(user_id)) + logger.warning('deleting user') + path = os.path.join(app.config['NOPAQUE_STORAGE'], str(user.id)) + shutil.rmtree(path, ignore_errors=True) + user.delete() diff --git a/app/profile/views.py b/app/profile/views.py index 383da741..08868d42 100644 --- a/app/profile/views.py +++ b/app/profile/views.py @@ -1,8 +1,7 @@ -from flask import current_app, flash, redirect, render_template, url_for +from flask import flash, redirect, render_template, url_for from flask_login import current_user, login_required, logout_user -from threading import Thread from . import profile -from .background_functions import delete_user_ +from . import tasks from .forms import EditEmailForm, EditGeneralSettingsForm, EditPasswordForm from .. import db @@ -56,9 +55,7 @@ def delete(): """ View to delete yourslef and all associated data. """ - thread = Thread(target=delete_user_, - args=(current_app._get_current_object(), current_user.id)) - thread.start() + tasks.delete_user(current_user.id) logout_user() flash('Your account has been deleted!') return redirect(url_for('main.index')) diff --git a/docker-compose.yml b/docker-compose.yml index 97c9cc53..94da5713 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -45,7 +45,6 @@ services: - "./logs:/home/nopaqued/logs" - "../opaque_daemon/docker-entrypoint.sh:/usr/local/bin/docker-entrypoint.sh" - "../opaque_daemon/nopaqued.py:/home/nopaqued/nopaqued.py" - - "../opaque_daemon/merge_corpus_files.py:/home/nopaqued/merge_corpus_files.py" - "../opaque_daemon/requirements.txt:/home/nopaqued/requirements.txt" - "$HOME/.docker:/home/nopaqued/.docker" db: