From 69387ee183e95287b12d0a19f3c5ca38ea30f1cb Mon Sep 17 00:00:00 2001
From: Patrick Jentsch
Date: Tue, 5 Nov 2019 09:12:06 +0100
Subject: [PATCH] Insert metadata to corpus file in constructor
---
app/corpora/views.py | 1 -
app/models.py | 16 +++++++++++++---
2 files changed, 13 insertions(+), 4 deletions(-)
diff --git a/app/corpora/views.py b/app/corpora/views.py
index 3a06b204..e7862b17 100644
--- a/app/corpora/views.py
+++ b/app/corpora/views.py
@@ -113,7 +113,6 @@ def add_corpus_file(corpus_id):
corpus=corpus, dir=dir, filename=filename,
publishing_year=add_corpus_file_form.publishing_year.data,
title=add_corpus_file_form.title.data)
- corpus_file.insert_metadata()
db.session.add(corpus_file)
db.session.commit()
flash('Corpus file added!')
diff --git a/app/models.py b/app/models.py
index 5c08da59..4987fb4c 100644
--- a/app/models.py
+++ b/app/models.py
@@ -2,6 +2,7 @@ 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
@@ -410,6 +411,18 @@ 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')
@@ -424,9 +437,6 @@ class CorpusFile(db.Model):
db.session.delete(self)
db.session.commit()
- def insert_metadata(self):
- pass
-
class Corpus(db.Model):
"""