Make delete user function a background task

This commit is contained in:
Patrick Jentsch 2020-04-23 08:24:11 +02:00
parent edc0b34032
commit 83c05d93b0
6 changed files with 25 additions and 32 deletions

View File

@ -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 flask_login import login_required
from threading import Thread
from . import admin from . import admin
from .forms import EditUserForm from .forms import EditUserForm
from .tables import AdminUserItem, AdminUserTable from .tables import AdminUserItem, AdminUserTable
from .. import db from .. import db
from ..decorators import admin_required from ..decorators import admin_required
from ..models import Role, User from ..models import Role, User
from ..profile.background_functions import delete_user_ from ..profile import tasks as profile_tasks
@admin.route('/') @admin.route('/')
@ -39,9 +38,7 @@ def user(user_id):
@admin_required @admin_required
def delete_user(user_id): def delete_user(user_id):
user = User.query.get_or_404(user_id) user = User.query.get_or_404(user_id)
thread = Thread(target=delete_user_, profile_tasks.delete_user(user_id)
args=(current_app._get_current_object(), user.id))
thread.start()
flash('User has been deleted!') flash('User has been deleted!')
return redirect(url_for('admin.index')) return redirect(url_for('admin.index'))

View File

@ -4,9 +4,7 @@ from flask_login import UserMixin, AnonymousUserMixin
from itsdangerous import BadSignature, TimedJSONWebSignatureSerializer from itsdangerous import BadSignature, TimedJSONWebSignatureSerializer
from werkzeug.security import generate_password_hash, check_password_hash from werkzeug.security import generate_password_hash, check_password_hash
from werkzeug.utils import secure_filename from werkzeug.utils import secure_filename
from . import db, logger, login_manager from . import db, login_manager
import os
import shutil
class Permission: class Permission:
@ -212,13 +210,6 @@ class User(UserMixin, db.Model):
job.delete() job.delete()
for corpus in self.corpora: for corpus in self.corpora:
corpus.delete() 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.delete(self)
db.session.commit() db.session.commit()

View File

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

18
app/profile/tasks.py Normal file
View File

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

View File

@ -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 flask_login import current_user, login_required, logout_user
from threading import Thread
from . import profile from . import profile
from .background_functions import delete_user_ from . import tasks
from .forms import EditEmailForm, EditGeneralSettingsForm, EditPasswordForm from .forms import EditEmailForm, EditGeneralSettingsForm, EditPasswordForm
from .. import db from .. import db
@ -56,9 +55,7 @@ def delete():
""" """
View to delete yourslef and all associated data. View to delete yourslef and all associated data.
""" """
thread = Thread(target=delete_user_, tasks.delete_user(current_user.id)
args=(current_app._get_current_object(), current_user.id))
thread.start()
logout_user() logout_user()
flash('Your account has been deleted!') flash('Your account has been deleted!')
return redirect(url_for('main.index')) return redirect(url_for('main.index'))

View File

@ -45,7 +45,6 @@ services:
- "./logs:/home/nopaqued/logs" - "./logs:/home/nopaqued/logs"
- "../opaque_daemon/docker-entrypoint.sh:/usr/local/bin/docker-entrypoint.sh" - "../opaque_daemon/docker-entrypoint.sh:/usr/local/bin/docker-entrypoint.sh"
- "../opaque_daemon/nopaqued.py:/home/nopaqued/nopaqued.py" - "../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" - "../opaque_daemon/requirements.txt:/home/nopaqued/requirements.txt"
- "$HOME/.docker:/home/nopaqued/.docker" - "$HOME/.docker:/home/nopaqued/.docker"
db: db: