Insert metadata to corpus file in constructor

This commit is contained in:
Patrick Jentsch 2019-11-05 09:12:06 +01:00
parent 8ef6d3a04b
commit 69387ee183
2 changed files with 13 additions and 4 deletions

View File

@ -113,7 +113,6 @@ def add_corpus_file(corpus_id):
corpus=corpus, dir=dir, filename=filename, corpus=corpus, dir=dir, filename=filename,
publishing_year=add_corpus_file_form.publishing_year.data, publishing_year=add_corpus_file_form.publishing_year.data,
title=add_corpus_file_form.title.data) title=add_corpus_file_form.title.data)
corpus_file.insert_metadata()
db.session.add(corpus_file) db.session.add(corpus_file)
db.session.commit() db.session.commit()
flash('Corpus file added!') flash('Corpus file added!')

View File

@ -2,6 +2,7 @@ from flask import current_app
from flask_login import UserMixin, AnonymousUserMixin 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
import xml.etree.ElementTree as ET
from . import db from . import db
from . import login_manager from . import login_manager
from datetime import datetime from datetime import datetime
@ -410,6 +411,18 @@ class CorpusFile(db.Model):
title = db.Column(db.String(64)) title = db.Column(db.String(64))
corpus_id = db.Column(db.Integer, db.ForeignKey('corpora.id')) 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): def delete(self):
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
logger.warning('Called CorpusFile.delete') logger.warning('Called CorpusFile.delete')
@ -424,9 +437,6 @@ class CorpusFile(db.Model):
db.session.delete(self) db.session.delete(self)
db.session.commit() db.session.commit()
def insert_metadata(self):
pass
class Corpus(db.Model): class Corpus(db.Model):
""" """