From 29183b8763e67ab2ec1b2e63852dfa8a016dc61c Mon Sep 17 00:00:00 2001
From: Patrick Jentsch
Date: Tue, 5 Nov 2019 10:32:42 +0100
Subject: [PATCH] prepare vrt file in background thread.
---
app/corpora/views.py | 6 ++++++
app/jobs/views.py | 3 ++-
app/models.py | 27 +++++++++++++--------------
app/utils.py | 3 ++-
4 files changed, 23 insertions(+), 16 deletions(-)
diff --git a/app/corpora/views.py b/app/corpora/views.py
index e7862b17..f7bfbd6f 100644
--- a/app/corpora/views.py
+++ b/app/corpora/views.py
@@ -1,3 +1,4 @@
+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
@@ -115,6 +116,11 @@ 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()
flash('Corpus file added!')
return redirect(url_for('corpora.corpus', corpus_id=corpus_id))
return render_template('corpora/add_corpus_file.html.j2',
diff --git a/app/jobs/views.py b/app/jobs/views.py
index ec22ccd7..4c5252e5 100644
--- a/app/jobs/views.py
+++ b/app/jobs/views.py
@@ -24,7 +24,8 @@ def delete_job(job_id):
if not (job.creator == current_user or current_user.is_administrator()):
abort(403)
delete_thread = threading.Thread(target=background_delete_job,
- args=(current_app, job_id))
+ args=(current_app._get_current_object(),
+ job_id))
delete_thread.start()
flash('Job has been deleted!')
return redirect(url_for('main.dashboard'))
diff --git a/app/models.py b/app/models.py
index 4987fb4c..a6af9cd6 100644
--- a/app/models.py
+++ b/app/models.py
@@ -1,14 +1,14 @@
+from datetime import datetime
from flask import current_app
from flask_login import UserMixin, AnonymousUserMixin
from itsdangerous import BadSignature, TimedJSONWebSignatureSerializer
from werkzeug.security import generate_password_hash, check_password_hash
-import xml.etree.ElementTree as ET
from . import db
from . import login_manager
-from datetime import datetime
import os
import shutil
import logging
+import xml.etree.ElementTree as ET
class Permission:
@@ -411,18 +411,6 @@ class CorpusFile(db.Model):
title = db.Column(db.String(64))
corpus_id = db.Column(db.Integer, db.ForeignKey('corpora.id'))
- def __init__(self, **kwargs):
- super(CorpusFile, self).__init__(**kwargs)
- file = os.path.join(current_app.config['OPAQUE_STORAGE_DIRECTORY'],
- self.dir,
- self.filename)
- element_tree = ET.parse(file)
- text_node = element_tree.find('text')
- text_node.set('author', self.author)
- text_node.set('publishing_year', str(self.publishing_year))
- text_node.set('title', self.title)
- element_tree.write(file)
-
def delete(self):
logger = logging.getLogger(__name__)
logger.warning('Called CorpusFile.delete')
@@ -437,6 +425,17 @@ class CorpusFile(db.Model):
db.session.delete(self)
db.session.commit()
+ def insert_metadata(self):
+ file = os.path.join(current_app.config['OPAQUE_STORAGE_DIRECTORY'],
+ self.dir,
+ self.filename)
+ element_tree = ET.parse(file)
+ text_node = element_tree.find('text')
+ text_node.set('author', self.author)
+ text_node.set('publishing_year', str(self.publishing_year))
+ text_node.set('title', self.title)
+ element_tree.write(file)
+
class Corpus(db.Model):
"""
diff --git a/app/utils.py b/app/utils.py
index 6eae9d54..9f74998b 100644
--- a/app/utils.py
+++ b/app/utils.py
@@ -1,5 +1,5 @@
-from .models import Job, User, Corpus, CorpusFile
from . import db
+from .models import Job, User, Corpus, CorpusFile
import logging
@@ -69,3 +69,4 @@ def background_delete_corpus(app, corpus_id):
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()