mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2024-12-24 18:34:18 +00:00
Use background tasks for file operations.
This commit is contained in:
parent
da077cae3f
commit
958779ec61
25
app/corpora/background_tasks.py
Normal file
25
app/corpora/background_tasks.py
Normal file
@ -0,0 +1,25 @@
|
||||
from app.models import Corpus, CorpusFile
|
||||
|
||||
|
||||
def delete_corpus_(app, corpus_id):
|
||||
with app.app_context():
|
||||
corpus = Corpus.query.filter_by(id=corpus_id).first()
|
||||
if corpus is None:
|
||||
raise Exception('Corpus {} not found!'.format(corpus_id))
|
||||
corpus.delete()
|
||||
|
||||
|
||||
def delete_corpus_file_(app, corpus_file_id):
|
||||
with app.app_context():
|
||||
corpus_file = CorpusFile.query.filter_by(id=corpus_file_id).first()
|
||||
if corpus_file is None:
|
||||
raise Exception('Corpus file {} not found!'.format(corpus_file_id))
|
||||
corpus_file.delete()
|
||||
|
||||
|
||||
def edit_corpus_file_(app, corpus_file_id):
|
||||
with app.app_context():
|
||||
corpus_file = CorpusFile.query.filter_by(id=corpus_file_id).first()
|
||||
if corpus_file is None:
|
||||
raise Exception('Corpus file {} not found!'.format(corpus_file_id))
|
||||
corpus_file.insert_metadata()
|
@ -1,11 +1,12 @@
|
||||
from app import db
|
||||
from app.models import Corpus, CorpusFile
|
||||
from app.utils import background_prepare_corpus_file
|
||||
from flask import (abort, current_app, flash, redirect, request,
|
||||
render_template, url_for, send_from_directory)
|
||||
from flask_login import current_user, login_required
|
||||
from werkzeug.utils import secure_filename
|
||||
from . import corpora
|
||||
from .background_tasks import (delete_corpus_, delete_corpus_file_,
|
||||
edit_corpus_file_)
|
||||
from .forms import (AddCorpusFileForm, AddCorpusForm, EditCorpusFileForm,
|
||||
QueryDownloadForm, QueryForm)
|
||||
import os
|
||||
@ -80,29 +81,16 @@ def corpus_analysis(corpus_id):
|
||||
title='Corpus: {}'.format(corpus.title))
|
||||
|
||||
|
||||
@corpora.route('/<int:corpus_id>/prepare')
|
||||
@login_required
|
||||
def prepare_corpus(corpus_id):
|
||||
corpus = Corpus.query.get_or_404(corpus_id)
|
||||
if not (corpus.creator == current_user or current_user.is_administrator()):
|
||||
abort(403)
|
||||
if len(corpus.files.all()) > 0:
|
||||
corpus.status = 'preparable'
|
||||
db.session.commit()
|
||||
flash('Corpus marked for preparation!')
|
||||
else:
|
||||
flash('Can not prepare corpus, please add corpus file(s).')
|
||||
return redirect(url_for('corpora.corpus', corpus_id=corpus_id))
|
||||
|
||||
|
||||
@corpora.route('/<int:corpus_id>/delete')
|
||||
@login_required
|
||||
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)
|
||||
delete_thread = threading.Thread(corpus.delete())
|
||||
delete_thread.start()
|
||||
thread = threading.Thread(target=delete_corpus_,
|
||||
args=(current_app._get_current_object(),
|
||||
corpus.id))
|
||||
thread.start()
|
||||
flash('Corpus deleted!')
|
||||
return redirect(url_for('main.dashboard'))
|
||||
|
||||
@ -132,11 +120,10 @@ def add_corpus_file(corpus_id):
|
||||
title=add_corpus_file_form.title.data)
|
||||
db.session.add(corpus_file)
|
||||
db.session.commit()
|
||||
background_thread = threading.Thread(
|
||||
args=(current_app._get_current_object(), corpus_file.id),
|
||||
target=background_prepare_corpus_file
|
||||
)
|
||||
background_thread.start()
|
||||
thread = threading.Thread(target=edit_corpus_file_,
|
||||
args=(current_app._get_current_object(),
|
||||
corpus_file.id))
|
||||
thread.start()
|
||||
flash('Corpus file added!')
|
||||
return redirect(url_for('corpora.corpus', corpus_id=corpus_id))
|
||||
return render_template('corpora/add_corpus_file.html.j2',
|
||||
@ -153,8 +140,10 @@ def delete_corpus_file(corpus_id, corpus_file_id):
|
||||
if not (corpus_file.corpus.creator == current_user
|
||||
or current_user.is_administrator()):
|
||||
abort(403)
|
||||
delete_thread = threading.Thread(corpus_file.delete())
|
||||
delete_thread.start()
|
||||
thread = threading.Thread(target=delete_corpus_file_,
|
||||
args=(current_app._get_current_object(),
|
||||
corpus_file.id))
|
||||
thread.start()
|
||||
flash('Corpus file deleted!')
|
||||
return redirect(url_for('corpora.corpus', corpus_id=corpus_id))
|
||||
|
||||
@ -188,10 +177,28 @@ def edit_corpus_file(corpus_id, corpus_file_id):
|
||||
corpus_file.author = edit_corpus_file_form.author.data
|
||||
corpus_file.publishing_year = edit_corpus_file_form.publishing_year.data
|
||||
corpus_file.title = edit_corpus_file_form.title.data
|
||||
corpus_file.insert_metadata()
|
||||
db.session.commit()
|
||||
thread = threading.Thread(target=edit_corpus_file_,
|
||||
args=(current_app._get_current_object(),
|
||||
corpus_file.id))
|
||||
thread.start()
|
||||
flash('Corpus file edited!')
|
||||
return redirect(url_for('corpora.corpus', corpus_id=corpus_id))
|
||||
return render_template('corpora/edit_corpus_file.html.j2',
|
||||
edit_corpus_file_form=edit_corpus_file_form,
|
||||
corpus_file=corpus_file, title='Edit corpus file')
|
||||
|
||||
|
||||
@corpora.route('/<int:corpus_id>/prepare')
|
||||
@login_required
|
||||
def prepare_corpus(corpus_id):
|
||||
corpus = Corpus.query.get_or_404(corpus_id)
|
||||
if not (corpus.creator == current_user or current_user.is_administrator()):
|
||||
abort(403)
|
||||
if len(corpus.files.all()) > 0:
|
||||
corpus.status = 'preparable'
|
||||
db.session.commit()
|
||||
flash('Corpus marked for preparation!')
|
||||
else:
|
||||
flash('Can not prepare corpus, please add corpus file(s).')
|
||||
return redirect(url_for('corpora.corpus', corpus_id=corpus_id))
|
||||
|
15
app/utils.py
15
app/utils.py
@ -55,18 +55,3 @@ def background_delete_job(app, job_id):
|
||||
job.delete_job()
|
||||
deleted = True
|
||||
logger.warning('Loop has ended.')
|
||||
|
||||
|
||||
def background_delete_corpus(app, corpus_id):
|
||||
logger = logging.getLogger(__name__)
|
||||
with app.app_context():
|
||||
logger.warning('Called by delete_thread.')
|
||||
logger.warning('Corpus id is: {}.'.format(corpus_id))
|
||||
corpus = Corpus.query.filter_by(id=corpus_id).first()
|
||||
corpus.delete()
|
||||
|
||||
|
||||
def background_prepare_corpus_file(app, corpus_file_id):
|
||||
with app.app_context():
|
||||
corpus_file = CorpusFile.query.filter_by(id=corpus_file_id).first()
|
||||
corpus_file.insert_metadata()
|
||||
|
Loading…
Reference in New Issue
Block a user