2022-02-08 11:26:20 +00:00
|
|
|
from app import db
|
|
|
|
from app.models import Corpus, CorpusFile, CorpusStatus
|
|
|
|
from flask import (
|
|
|
|
abort,
|
|
|
|
current_app,
|
|
|
|
flash,
|
|
|
|
make_response,
|
|
|
|
redirect,
|
|
|
|
render_template,
|
|
|
|
url_for,
|
|
|
|
send_from_directory
|
|
|
|
)
|
2020-04-06 12:12:22 +00:00
|
|
|
from flask_login import current_user, login_required
|
2021-09-13 09:45:43 +00:00
|
|
|
from . import bp
|
2020-04-21 16:34:21 +00:00
|
|
|
from . import tasks
|
2022-02-08 11:26:20 +00:00
|
|
|
from .forms import (
|
|
|
|
AddCorpusFileForm,
|
|
|
|
AddCorpusForm,
|
|
|
|
EditCorpusFileForm,
|
|
|
|
ImportCorpusForm
|
|
|
|
)
|
2020-04-06 12:12:22 +00:00
|
|
|
import os
|
2020-10-29 14:20:30 +00:00
|
|
|
import shutil
|
2022-04-19 09:48:44 +00:00
|
|
|
import tempfile
|
2020-10-29 14:20:30 +00:00
|
|
|
import xml.etree.ElementTree as ET
|
2020-04-06 12:12:22 +00:00
|
|
|
|
|
|
|
|
2021-09-13 09:45:43 +00:00
|
|
|
@bp.route('/add', methods=['GET', 'POST'])
|
2020-04-06 12:12:22 +00:00
|
|
|
@login_required
|
|
|
|
def add_corpus():
|
2021-11-16 14:23:57 +00:00
|
|
|
form = AddCorpusForm(prefix='add-corpus-form')
|
2020-11-13 09:01:51 +00:00
|
|
|
if form.validate_on_submit():
|
2021-11-16 14:23:57 +00:00
|
|
|
corpus = Corpus(
|
2021-11-30 15:22:16 +00:00
|
|
|
user=current_user,
|
2021-11-16 14:23:57 +00:00
|
|
|
description=form.description.data,
|
|
|
|
title=form.title.data
|
|
|
|
)
|
2020-04-06 12:12:22 +00:00
|
|
|
db.session.add(corpus)
|
2021-02-01 11:51:07 +00:00
|
|
|
db.session.flush()
|
|
|
|
db.session.refresh(corpus)
|
2020-04-06 12:12:22 +00:00
|
|
|
try:
|
2022-02-03 11:39:16 +00:00
|
|
|
corpus.makedirs()
|
2021-11-16 14:23:57 +00:00
|
|
|
except OSError as e:
|
2022-02-03 11:39:16 +00:00
|
|
|
current_app.logger.error(e)
|
2021-02-01 11:51:07 +00:00
|
|
|
db.session.rollback()
|
2022-02-08 11:26:20 +00:00
|
|
|
flash('Internal Server Error', category='error')
|
2020-11-13 09:01:51 +00:00
|
|
|
abort(500)
|
2022-02-03 11:39:16 +00:00
|
|
|
db.session.commit()
|
2022-02-08 11:26:20 +00:00
|
|
|
flash(f'Corpus "{corpus.title}" added', category='corpus')
|
2022-02-03 11:39:16 +00:00
|
|
|
return redirect(url_for('.corpus', corpus_id=corpus.id))
|
|
|
|
return render_template(
|
|
|
|
'corpora/add_corpus.html.j2',
|
|
|
|
form=form,
|
|
|
|
title='Add corpus'
|
|
|
|
)
|
2020-04-06 12:12:22 +00:00
|
|
|
|
|
|
|
|
2021-09-13 09:45:43 +00:00
|
|
|
@bp.route('/import', methods=['GET', 'POST'])
|
2020-10-29 14:20:30 +00:00
|
|
|
@login_required
|
|
|
|
def import_corpus():
|
2022-04-19 09:48:44 +00:00
|
|
|
form = ImportCorpusForm(prefix='import-corpus-form')
|
2020-11-13 09:01:51 +00:00
|
|
|
if form.is_submitted():
|
|
|
|
if not form.validate():
|
|
|
|
return make_response(form.errors, 400)
|
2021-11-16 14:23:57 +00:00
|
|
|
corpus = Corpus(
|
2021-11-30 15:22:16 +00:00
|
|
|
user=current_user,
|
2021-11-16 14:23:57 +00:00
|
|
|
description=form.description.data,
|
|
|
|
title=form.title.data
|
|
|
|
)
|
2020-10-29 14:20:30 +00:00
|
|
|
db.session.add(corpus)
|
2022-04-19 09:48:44 +00:00
|
|
|
db.session.flush(objects=[corpus])
|
2021-02-01 11:51:07 +00:00
|
|
|
db.session.refresh(corpus)
|
2020-10-29 14:20:30 +00:00
|
|
|
try:
|
2022-04-19 09:48:44 +00:00
|
|
|
corpus.makedirs()
|
2021-11-16 14:23:57 +00:00
|
|
|
except OSError as e:
|
2022-02-08 11:26:20 +00:00
|
|
|
current_app.logger.error(e)
|
2021-02-01 11:51:07 +00:00
|
|
|
db.session.rollback()
|
2022-02-08 11:26:20 +00:00
|
|
|
flash('Internal Server Error', category='error')
|
|
|
|
return make_response({'redirect_url': url_for('.import_corpus')}, 500) # noqa
|
2022-04-19 09:48:44 +00:00
|
|
|
# Save the uploaded zip file in a temporary directory
|
|
|
|
tmp_dir_base = os.path.join(current_app.config['NOPAQUE_DATA_DIR'], 'tmp') # noqa
|
|
|
|
with tempfile.TemporaryDirectory(dir=tmp_dir_base) as tmp_dir:
|
|
|
|
archive_file = os.path.join(tmp_dir, 'corpus.zip')
|
|
|
|
try:
|
|
|
|
form.archive.data.save(archive_file)
|
|
|
|
except OSError as e:
|
|
|
|
current_app.logger.error(e)
|
|
|
|
db.session.rollback()
|
|
|
|
flash('Internal Server Error1', category='error')
|
|
|
|
return make_response({'redirect_url': url_for('.import_corpus')}, 500) # noqa
|
|
|
|
shutil.unpack_archive(archive_file, extract_dir=tmp_dir)
|
|
|
|
for vrt_filename in [x for x in os.listdir(tmp_dir) if x.endswith('.vrt')]:
|
|
|
|
vrt_file = os.path.join(tmp_dir, vrt_filename)
|
|
|
|
element_tree = ET.parse(vrt_file)
|
2020-11-13 09:01:51 +00:00
|
|
|
text_node = element_tree.find('text')
|
|
|
|
corpus_file = CorpusFile(
|
2022-04-19 09:48:44 +00:00
|
|
|
author=text_node.get('author'),
|
2020-11-13 09:01:51 +00:00
|
|
|
corpus=corpus,
|
2022-04-19 09:48:44 +00:00
|
|
|
filename=vrt_filename,
|
|
|
|
mimetype='application/vrt+xml',
|
|
|
|
publishing_year=int(text_node.get('publishing_year')),
|
|
|
|
title=text_node.get('title')
|
2020-11-13 09:01:51 +00:00
|
|
|
)
|
2022-04-19 09:48:44 +00:00
|
|
|
if 'address' not in text_node.attrib:
|
|
|
|
corpus_file.address = text_node.get('address')
|
|
|
|
if 'booktitle' not in text_node.attrib:
|
|
|
|
corpus_file.booktitle = text_node.get('booktitle')
|
|
|
|
if 'chapter' not in text_node.attrib:
|
|
|
|
corpus_file.chapter = text_node.get('chapter')
|
|
|
|
if 'editor' not in text_node.attrib:
|
|
|
|
corpus_file.editor = text_node.get('editor')
|
|
|
|
if 'institution' not in text_node.attrib:
|
|
|
|
corpus_file.institution = text_node.get('institution')
|
|
|
|
if 'journal' not in text_node.attrib:
|
|
|
|
corpus_file.journal = text_node.get('journal')
|
|
|
|
if 'pages' not in text_node.attrib:
|
|
|
|
corpus_file.pages = text_node.get('pages')
|
|
|
|
if 'publisher' not in text_node.attrib:
|
|
|
|
corpus_file.publisher = text_node.get('publisher')
|
|
|
|
if 'school' not in text_node.attrib:
|
|
|
|
corpus_file.school = text_node.get('school')
|
2020-11-13 09:01:51 +00:00
|
|
|
db.session.add(corpus_file)
|
2022-04-19 09:48:44 +00:00
|
|
|
db.session.flush(objects=[corpus_file])
|
|
|
|
db.session.refresh(corpus)
|
|
|
|
try:
|
|
|
|
shutil.copy2(vrt_file, corpus_file.path)
|
|
|
|
except Exception as e:
|
|
|
|
db.session.rollback()
|
|
|
|
flash('Internal Server Error2', category='error')
|
|
|
|
return make_response({'redirect_url': url_for('.import_corpus')}, 500) # noqa
|
|
|
|
db.session.commit()
|
|
|
|
flash(f'Corpus "{corpus.title}" imported', 'corpus')
|
|
|
|
return make_response({'redirect_url': url_for('.corpus', corpus_id=corpus.id)}, 201)
|
2022-02-08 11:26:20 +00:00
|
|
|
return render_template(
|
|
|
|
'corpora/import_corpus.html.j2',
|
|
|
|
form=form,
|
|
|
|
title='Import Corpus'
|
|
|
|
)
|
2020-10-29 14:20:30 +00:00
|
|
|
|
|
|
|
|
2021-11-30 15:22:16 +00:00
|
|
|
@bp.route('/<hashid:corpus_id>')
|
2020-04-06 12:12:22 +00:00
|
|
|
@login_required
|
|
|
|
def corpus(corpus_id):
|
|
|
|
corpus = Corpus.query.get_or_404(corpus_id)
|
2021-11-30 15:22:16 +00:00
|
|
|
if not (corpus.user == current_user or current_user.is_administrator()):
|
2020-04-06 12:12:22 +00:00
|
|
|
abort(403)
|
2022-02-08 11:26:20 +00:00
|
|
|
return render_template(
|
|
|
|
'corpora/corpus.html.j2',
|
|
|
|
corpus=corpus,
|
|
|
|
title='Corpus'
|
|
|
|
)
|
2020-03-28 18:29:19 +00:00
|
|
|
|
|
|
|
|
2021-11-30 15:22:16 +00:00
|
|
|
@bp.route('/<hashid:corpus_id>/analyse')
|
2021-11-16 14:23:57 +00:00
|
|
|
@login_required
|
|
|
|
def analyse_corpus(corpus_id):
|
|
|
|
corpus = Corpus.query.get_or_404(corpus_id)
|
|
|
|
return render_template(
|
|
|
|
'corpora/analyse_corpus.html.j2',
|
|
|
|
corpus=corpus,
|
|
|
|
title=f'Analyse Corpus {corpus.title}'
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-04-19 09:48:44 +00:00
|
|
|
@bp.route('/<hashid:corpus_id>/build')
|
|
|
|
@login_required
|
|
|
|
def build_corpus(corpus_id):
|
|
|
|
corpus = Corpus.query.get_or_404(corpus_id)
|
|
|
|
if not (corpus.user == current_user or current_user.is_administrator()):
|
|
|
|
abort(403)
|
|
|
|
if corpus.files.all():
|
|
|
|
tasks.build_corpus(corpus_id)
|
|
|
|
flash(
|
|
|
|
f'Corpus "{corpus.title}" marked for building',
|
|
|
|
category='corpus'
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
flash(
|
|
|
|
f'Can\'t build corpus "{corpus.title}": No corpus file(s)',
|
|
|
|
category='error'
|
|
|
|
)
|
|
|
|
return redirect(url_for('.corpus', corpus_id=corpus_id))
|
|
|
|
|
|
|
|
|
2021-11-30 15:22:16 +00:00
|
|
|
@bp.route('/<hashid:corpus_id>/delete')
|
2020-04-06 12:12:22 +00:00
|
|
|
@login_required
|
|
|
|
def delete_corpus(corpus_id):
|
|
|
|
corpus = Corpus.query.get_or_404(corpus_id)
|
2021-11-30 15:22:16 +00:00
|
|
|
if not (corpus.user == current_user or current_user.is_administrator()):
|
2020-04-06 12:12:22 +00:00
|
|
|
abort(403)
|
2022-02-08 11:26:20 +00:00
|
|
|
flash(f'Corpus "{corpus.title}" marked for deletion', 'corpus')
|
2020-04-21 16:34:21 +00:00
|
|
|
tasks.delete_corpus(corpus_id)
|
2020-04-06 12:12:22 +00:00
|
|
|
return redirect(url_for('main.dashboard'))
|
|
|
|
|
|
|
|
|
2022-04-19 09:48:44 +00:00
|
|
|
@bp.route('/<hashid:corpus_id>/export')
|
|
|
|
@login_required
|
|
|
|
def export_corpus(corpus_id):
|
|
|
|
abort(503)
|
|
|
|
corpus = Corpus.query.get_or_404(corpus_id)
|
|
|
|
if not (corpus.user == current_user or current_user.is_administrator()):
|
|
|
|
abort(403)
|
|
|
|
return send_from_directory(
|
|
|
|
as_attachment=True,
|
|
|
|
directory=os.path.join(corpus.user.path, 'corpora'),
|
|
|
|
filename=corpus.archive_file,
|
|
|
|
mimetype='zip'
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/<hashid:corpus_id>/files/<hashid:corpus_file_id>', methods=['GET', 'POST']) # noqa
|
|
|
|
@login_required
|
|
|
|
def corpus_file(corpus_id, corpus_file_id):
|
|
|
|
corpus_file = CorpusFile.query.filter(
|
|
|
|
CorpusFile.corpus_id == corpus_id,
|
|
|
|
CorpusFile.id == corpus_file_id
|
|
|
|
).first_or_404()
|
|
|
|
if not (
|
|
|
|
corpus_file.corpus.user == current_user
|
|
|
|
or current_user.is_administrator()
|
|
|
|
):
|
|
|
|
abort(403)
|
|
|
|
form = EditCorpusFileForm(prefix='edit-corpus-file-form')
|
|
|
|
if form.validate_on_submit():
|
|
|
|
corpus_file.address = form.address.data
|
|
|
|
corpus_file.author = form.author.data
|
|
|
|
corpus_file.booktitle = form.booktitle.data
|
|
|
|
corpus_file.chapter = form.chapter.data
|
|
|
|
corpus_file.editor = form.editor.data
|
|
|
|
corpus_file.institution = form.institution.data
|
|
|
|
corpus_file.journal = form.journal.data
|
|
|
|
corpus_file.pages = form.pages.data
|
|
|
|
corpus_file.publisher = form.publisher.data
|
|
|
|
corpus_file.publishing_year = form.publishing_year.data
|
|
|
|
corpus_file.school = form.school.data
|
|
|
|
corpus_file.title = form.title.data
|
|
|
|
corpus_file.corpus.status = CorpusStatus.UNPREPARED
|
|
|
|
db.session.commit()
|
|
|
|
flash(f'Corpus file "{corpus_file.filename}" edited', category='corpus') # noqa
|
|
|
|
return redirect(url_for('.corpus', corpus_id=corpus_id))
|
|
|
|
# If no form is submitted or valid, fill out fields with current values
|
|
|
|
form.address.data = corpus_file.address
|
|
|
|
form.author.data = corpus_file.author
|
|
|
|
form.booktitle.data = corpus_file.booktitle
|
|
|
|
form.chapter.data = corpus_file.chapter
|
|
|
|
form.editor.data = corpus_file.editor
|
|
|
|
form.institution.data = corpus_file.institution
|
|
|
|
form.journal.data = corpus_file.journal
|
|
|
|
form.pages.data = corpus_file.pages
|
|
|
|
form.publisher.data = corpus_file.publisher
|
|
|
|
form.publishing_year.data = corpus_file.publishing_year
|
|
|
|
form.school.data = corpus_file.school
|
|
|
|
form.title.data = corpus_file.title
|
|
|
|
return render_template(
|
|
|
|
'corpora/corpus_file.html.j2',
|
|
|
|
corpus=corpus_file.corpus,
|
|
|
|
corpus_file=corpus_file,
|
|
|
|
form=form,
|
|
|
|
title='Edit corpus file'
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-11-30 15:22:16 +00:00
|
|
|
@bp.route('/<hashid:corpus_id>/files/add', methods=['GET', 'POST'])
|
2020-04-06 12:12:22 +00:00
|
|
|
@login_required
|
|
|
|
def add_corpus_file(corpus_id):
|
|
|
|
corpus = Corpus.query.get_or_404(corpus_id)
|
2021-11-30 15:22:16 +00:00
|
|
|
if not (corpus.user == current_user or current_user.is_administrator()):
|
2020-04-06 12:12:22 +00:00
|
|
|
abort(403)
|
2022-04-12 14:11:24 +00:00
|
|
|
form = AddCorpusFileForm(prefix='add-corpus-file-form')
|
2020-11-13 09:01:51 +00:00
|
|
|
if form.is_submitted():
|
|
|
|
if not form.validate():
|
|
|
|
return make_response(form.errors, 400)
|
2020-04-06 12:12:22 +00:00
|
|
|
# Save the file
|
2021-11-16 14:23:57 +00:00
|
|
|
corpus_file = CorpusFile(
|
|
|
|
address=form.address.data,
|
|
|
|
author=form.author.data,
|
|
|
|
booktitle=form.booktitle.data,
|
|
|
|
chapter=form.chapter.data,
|
|
|
|
corpus=corpus,
|
|
|
|
editor=form.editor.data,
|
2022-04-13 07:08:11 +00:00
|
|
|
filename=form.vrt.data.filename,
|
2021-11-16 14:23:57 +00:00
|
|
|
institution=form.institution.data,
|
|
|
|
journal=form.journal.data,
|
2022-02-03 11:39:16 +00:00
|
|
|
mimetype='application/vrt+xml',
|
2021-11-16 14:23:57 +00:00
|
|
|
pages=form.pages.data,
|
|
|
|
publisher=form.publisher.data,
|
|
|
|
publishing_year=form.publishing_year.data,
|
|
|
|
school=form.school.data,
|
|
|
|
title=form.title.data
|
|
|
|
)
|
2020-04-06 12:12:22 +00:00
|
|
|
db.session.add(corpus_file)
|
2022-02-03 11:39:16 +00:00
|
|
|
db.session.flush(objects=[corpus_file])
|
|
|
|
db.session.refresh(corpus_file)
|
|
|
|
try:
|
2022-04-13 07:08:11 +00:00
|
|
|
form.vrt.data.save(corpus_file.path)
|
2022-02-03 11:39:16 +00:00
|
|
|
except OSError as e:
|
|
|
|
current_app.logger.error(e)
|
|
|
|
db.session.rollback()
|
2022-02-08 11:26:20 +00:00
|
|
|
flash('Internal Server Error', category='error')
|
2022-02-03 11:39:16 +00:00
|
|
|
return make_response({'redirect_url': url_for('.add_corpus_file', corpus_id=corpus.id)}, 500) # noqa
|
2022-02-08 11:26:20 +00:00
|
|
|
corpus.status = CorpusStatus.UNPREPARED
|
2020-04-06 12:12:22 +00:00
|
|
|
db.session.commit()
|
2022-02-08 11:26:20 +00:00
|
|
|
flash(f'Corpus file "{corpus_file.filename}" added', category='corpus')
|
2020-11-13 09:01:51 +00:00
|
|
|
return make_response({'redirect_url': url_for('.corpus', corpus_id=corpus.id)}, 201) # noqa
|
2022-02-03 11:39:16 +00:00
|
|
|
return render_template(
|
|
|
|
'corpora/add_corpus_file.html.j2',
|
|
|
|
corpus=corpus,
|
|
|
|
form=form,
|
|
|
|
title='Add corpus file'
|
|
|
|
)
|
2020-04-06 12:12:22 +00:00
|
|
|
|
|
|
|
|
2021-11-30 15:22:16 +00:00
|
|
|
@bp.route('/<hashid:corpus_id>/files/<hashid:corpus_file_id>/delete')
|
2020-04-06 12:12:22 +00:00
|
|
|
@login_required
|
|
|
|
def delete_corpus_file(corpus_id, corpus_file_id):
|
2022-02-08 11:26:20 +00:00
|
|
|
corpus_file = CorpusFile.query.filter(
|
|
|
|
CorpusFile.corpus_id == corpus_id,
|
|
|
|
CorpusFile.id == corpus_file_id
|
|
|
|
).first_or_404()
|
|
|
|
if not (
|
|
|
|
corpus_file.corpus.user == current_user
|
|
|
|
or current_user.is_administrator()
|
|
|
|
):
|
2020-04-06 12:12:22 +00:00
|
|
|
abort(403)
|
2021-12-08 13:45:05 +00:00
|
|
|
flash(
|
2022-02-08 11:26:20 +00:00
|
|
|
f'Corpus file "{corpus_file.filename}" marked for deletion',
|
|
|
|
category='corpus'
|
|
|
|
)
|
2020-04-21 16:34:21 +00:00
|
|
|
tasks.delete_corpus_file(corpus_file_id)
|
2020-11-13 09:01:51 +00:00
|
|
|
return redirect(url_for('.corpus', corpus_id=corpus_id))
|
2020-04-06 12:12:22 +00:00
|
|
|
|
|
|
|
|
2021-11-30 15:22:16 +00:00
|
|
|
@bp.route('/<hashid:corpus_id>/files/<hashid:corpus_file_id>/download')
|
2020-04-06 12:12:22 +00:00
|
|
|
@login_required
|
|
|
|
def download_corpus_file(corpus_id, corpus_file_id):
|
2022-02-08 11:26:20 +00:00
|
|
|
corpus_file = CorpusFile.query.filter(
|
|
|
|
CorpusFile.corpus_id == corpus_id,
|
|
|
|
CorpusFile.id == corpus_file_id
|
|
|
|
).first_or_404()
|
|
|
|
if not (
|
|
|
|
corpus_file.corpus.user == current_user
|
|
|
|
or current_user.is_administrator()
|
|
|
|
):
|
2020-04-06 12:12:22 +00:00
|
|
|
abort(403)
|
2022-02-08 11:26:20 +00:00
|
|
|
return send_from_directory(
|
|
|
|
as_attachment=True,
|
2022-04-12 14:11:24 +00:00
|
|
|
attachment_filename=corpus_file.filename,
|
2022-02-08 11:26:20 +00:00
|
|
|
directory=os.path.dirname(corpus_file.path),
|
2022-04-12 14:11:24 +00:00
|
|
|
filename=os.path.basename(corpus_file.path)
|
2022-07-08 09:46:47 +00:00
|
|
|
)
|