mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2025-06-12 17:10:41 +00:00
Create and use a decorator for background functions
This commit is contained in:
@ -1,30 +0,0 @@
|
||||
from ..models import Corpus, CorpusFile
|
||||
|
||||
|
||||
def delete_corpus_(app, corpus_id):
|
||||
with app.app_context():
|
||||
corpus = Corpus.query.get(corpus_id)
|
||||
if corpus is None:
|
||||
# raise Exception('Corpus {} not found!'.format(corpus_id))
|
||||
pass
|
||||
else:
|
||||
corpus.delete()
|
||||
|
||||
|
||||
def delete_corpus_file_(app, corpus_file_id):
|
||||
with app.app_context():
|
||||
corpus_file = CorpusFile.query.get(corpus_file_id)
|
||||
if corpus_file is None:
|
||||
# raise Exception('Corpus file {} not found!'.format(corpus_file_id))
|
||||
pass
|
||||
else:
|
||||
corpus_file.delete()
|
||||
|
||||
|
||||
def edit_corpus_file_(app, corpus_file_id):
|
||||
with app.app_context():
|
||||
corpus_file = CorpusFile.query.get(corpus_file_id)
|
||||
if corpus_file is None:
|
||||
raise Exception('Corpus file {} not found!'.format(corpus_file_id))
|
||||
else:
|
||||
corpus_file.insert_metadata()
|
41
app/corpora/tasks.py
Normal file
41
app/corpora/tasks.py
Normal file
@ -0,0 +1,41 @@
|
||||
from ..decorators import background
|
||||
from ..models import Corpus, CorpusFile
|
||||
import os
|
||||
import shutil
|
||||
|
||||
|
||||
@background
|
||||
def delete_corpus(app, corpus_id):
|
||||
with app.app_context():
|
||||
corpus = Corpus.query.get(corpus_id)
|
||||
if corpus is None:
|
||||
return
|
||||
path = os.path.join(app.config['NOPAQUE_STORAGE'], str(corpus.user_id),
|
||||
'corpora', str(corpus.id))
|
||||
shutil.rmtree(path, ignore_errors=True)
|
||||
corpus.delete()
|
||||
|
||||
|
||||
@background
|
||||
def delete_corpus_file(app, corpus_file_id):
|
||||
with app.app_context():
|
||||
corpus_file = CorpusFile.query.get(corpus_file_id)
|
||||
if corpus_file is None:
|
||||
return
|
||||
path = os.path.join(app.config['NOPAQUE_STORAGE'], corpus_file.dir,
|
||||
corpus_file.filename)
|
||||
try:
|
||||
os.remove(path)
|
||||
except Exception:
|
||||
pass
|
||||
else:
|
||||
corpus_file.delete()
|
||||
|
||||
|
||||
@background
|
||||
def edit_corpus_file(app, corpus_file_id):
|
||||
with app.app_context():
|
||||
corpus_file = CorpusFile.query.get(corpus_file_id)
|
||||
if corpus_file is None:
|
||||
raise Exception('Corpus file {} not found!'.format(corpus_file_id))
|
||||
corpus_file.insert_metadata()
|
@ -1,10 +1,8 @@
|
||||
from flask import (abort, current_app, flash, make_response, redirect, request,
|
||||
render_template, url_for, send_from_directory)
|
||||
from flask_login import current_user, login_required
|
||||
from threading import Thread
|
||||
from . import corpora
|
||||
from .background_functions import (delete_corpus_, delete_corpus_file_,
|
||||
edit_corpus_file_)
|
||||
from . import tasks
|
||||
from .forms import (AddCorpusFileForm, AddCorpusForm, EditCorpusFileForm,
|
||||
QueryDownloadForm, QueryForm, DisplayOptionsForm,
|
||||
InspectDisplayOptionsForm)
|
||||
@ -78,9 +76,7 @@ def delete_corpus(corpus_id):
|
||||
corpus = Corpus.query.get_or_404(corpus_id)
|
||||
if not (corpus.creator == current_user or current_user.is_administrator()):
|
||||
abort(403)
|
||||
thread = Thread(target=delete_corpus_,
|
||||
args=(current_app._get_current_object(), corpus.id))
|
||||
thread.start()
|
||||
tasks.delete_corpus(corpus_id)
|
||||
flash('Corpus deleted!')
|
||||
return redirect(url_for('main.dashboard'))
|
||||
|
||||
@ -119,10 +115,7 @@ def add_corpus_file(corpus_id):
|
||||
title=add_corpus_file_form.title.data)
|
||||
db.session.add(corpus_file)
|
||||
db.session.commit()
|
||||
thread = Thread(target=edit_corpus_file_,
|
||||
args=(current_app._get_current_object(),
|
||||
corpus_file.id))
|
||||
thread.start()
|
||||
tasks.edit_corpus_file(corpus_file.id)
|
||||
flash('Corpus file added!')
|
||||
return make_response(
|
||||
{'redirect_url': url_for('corpora.corpus', corpus_id=corpus.id)},
|
||||
@ -142,9 +135,7 @@ def delete_corpus_file(corpus_id, corpus_file_id):
|
||||
if not (corpus_file.corpus.creator == current_user
|
||||
or current_user.is_administrator()):
|
||||
abort(403)
|
||||
thread = Thread(target=delete_corpus_file_,
|
||||
args=(current_app._get_current_object(), corpus_file.id))
|
||||
thread.start()
|
||||
tasks.delete_corpus_file(corpus_file_id)
|
||||
flash('Corpus file deleted!')
|
||||
return redirect(url_for('corpora.corpus', corpus_id=corpus_id))
|
||||
|
||||
@ -191,10 +182,7 @@ def edit_corpus_file(corpus_id, corpus_file_id):
|
||||
corpus_file.school = edit_corpus_file_form.school.data
|
||||
corpus_file.title = edit_corpus_file_form.title.data
|
||||
db.session.commit()
|
||||
thread = Thread(target=edit_corpus_file_,
|
||||
args=(current_app._get_current_object(),
|
||||
corpus_file.id))
|
||||
thread.start()
|
||||
tasks.edit_corpus_file(corpus_file_id)
|
||||
flash('Corpus file edited!')
|
||||
return redirect(url_for('corpora.corpus', corpus_id=corpus_id))
|
||||
# If no form is submitted or valid, fill out fields with current values
|
||||
|
Reference in New Issue
Block a user