nopaque/app/corpora/routes.py

283 lines
9.2 KiB
Python
Raw Normal View History

from flask import (
abort,
current_app,
flash,
2022-09-02 11:07:30 +00:00
Markup,
redirect,
render_template,
send_from_directory
)
2020-04-06 12:12:22 +00:00
from flask_login import current_user, login_required
2022-09-02 11:07:30 +00:00
from threading import Thread
2020-04-06 12:12:22 +00:00
import os
2022-09-02 11:07:30 +00:00
from app import db
from app.models import Corpus, CorpusFile, CorpusStatus
from . import bp
from .forms import CreateCorpusFileForm, CreateCorpusForm, EditCorpusFileForm
2020-04-06 12:12:22 +00:00
2022-09-02 11:07:30 +00:00
@bp.route('/create', methods=['GET', 'POST'])
2020-04-06 12:12:22 +00:00
@login_required
2022-09-02 11:07:30 +00:00
def create_corpus():
form = CreateCorpusForm(prefix='create-corpus-form')
if form.validate_on_submit():
2020-04-06 12:12:22 +00:00
try:
2022-09-02 11:07:30 +00:00
corpus = Corpus.create(
title=form.title.data,
description=form.description.data,
user=current_user
)
except OSError:
abort(500)
db.session.commit()
2022-09-02 11:07:30 +00:00
message = Markup(
f'Corpus "<a href="{corpus.url}">{corpus.title}</a>" created'
2021-11-16 14:23:57 +00:00
)
2022-09-02 11:07:30 +00:00
flash(message, 'corpus')
return redirect(corpus.url)
return render_template(
2022-09-02 11:07:30 +00:00
'corpora/create_corpus.html.j2',
form=form,
2022-09-02 11:07:30 +00:00
title='Create corpus'
)
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)
# cool = False
# if corpus.is_public:
# cool = True
# elif current_user.is_authenticated:
# if corpus.user == current_user or current_user.is_administrator:
# cool = True
# else:
# abort(403)
# else:
# return current_app.login_manager.unauthorized()
return render_template(
'corpora/corpus.html.j2',
corpus=corpus,
title='Corpus'
)
2022-09-02 11:07:30 +00:00
@bp.route('/<hashid:corpus_id>', methods=['DELETE'])
@login_required
def delete_corpus(corpus_id):
def _delete_corpus(app, corpus_id):
with app.app_context():
corpus = Corpus.query.get(corpus_id)
corpus.delete()
db.session.commit()
corpus = Corpus.query.get_or_404(corpus_id)
if not (corpus.user == current_user or current_user.is_administrator()):
abort(403)
thread = Thread(
target=_delete_corpus,
args=(current_app._get_current_object(), corpus_id)
)
thread.start()
return {}, 202
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-09-02 11:07:30 +00:00
@bp.route('/<hashid:corpus_id>/build', methods=['POST'])
@login_required
def build_corpus(corpus_id):
2022-09-02 11:07:30 +00:00
def _build_corpus(app, corpus_id):
with app.app_context():
corpus = Corpus.query.get(corpus_id)
corpus.build()
db.session.commit()
2020-04-06 12:12:22 +00:00
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-09-02 11:07:30 +00:00
# Check if the corpus has corpus files
if not corpus.files.all():
response = {'errors': {'message': 'Corpus file(s) required'}}
return response, 409
thread = Thread(
target=_build_corpus,
args=(current_app._get_current_object(), corpus_id)
)
thread.start()
return {}, 202
2020-04-06 12:12:22 +00:00
2022-09-02 11:07:30 +00:00
@bp.route('/<hashid:corpus_id>/files/create', methods=['GET', 'POST'])
@login_required
2022-09-02 11:07:30 +00:00
def create_corpus_file(corpus_id):
corpus = Corpus.query.get_or_404(corpus_id)
if not (corpus.user == current_user or current_user.is_administrator()):
abort(403)
2022-09-02 11:07:30 +00:00
form = CreateCorpusFileForm(prefix='create-corpus-file-form')
if form.is_submitted():
if not form.validate():
response = {'errors': form.errors}
return response, 400
try:
corpus_file = CorpusFile.create(
form.vrt.data,
address=form.address.data,
author=form.author.data,
booktitle=form.booktitle.data,
chapter=form.chapter.data,
editor=form.editor.data,
institution=form.institution.data,
journal=form.journal.data,
pages=form.pages.data,
publisher=form.publisher.data,
publishing_year=form.publishing_year.data,
school=form.school.data,
title=form.title.data,
mimetype='application/vrt+xml',
corpus=corpus
)
except OSError:
abort(500)
corpus.status = CorpusStatus.UNPREPARED
db.session.commit()
message = Markup(
'Corpus file'
f'"<a href="{corpus_file.url}">{corpus_file.filename}</a>" added'
)
flash(message, category='corpus')
return {}, 201, {'Location': corpus.url}
return render_template(
'corpora/create_corpus_file.html.j2',
corpus=corpus,
form=form,
title='Add corpus file'
)
2022-09-02 11:07:30 +00:00
@bp.route('/<hashid:corpus_id>/files/<hashid:corpus_file_id>',
methods=['GET', 'POST'])
@login_required
def corpus_file(corpus_id, corpus_file_id):
2022-09-02 11:07:30 +00:00
corpus_file = CorpusFile.query.get_or_404(corpus_file_id)
if corpus_file.corpus.id != corpus_id:
abort(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():
2022-09-02 11:07:30 +00:00
has_changes = False
if corpus_file.address != form.address.data:
corpus_file.address = form.address.data
has_changes = True
if corpus_file.author != form.author.data:
corpus_file.author = form.author.data
has_changes = True
if corpus_file.booktitle != form.booktitle.data:
corpus_file.booktitle = form.booktitle.data
has_changes = True
if corpus_file.chapter != form.chapter.data:
corpus_file.chapter = form.chapter.data
has_changes = True
if corpus_file.editor != form.editor.data:
corpus_file.editor = form.editor.data
has_changes = True
if corpus_file.institution != form.institution.data:
corpus_file.institution = form.institution.data
has_changes = True
if corpus_file.journal != form.journal.data:
corpus_file.journal = form.journal.data
has_changes = True
if corpus_file.pages != form.pages.data:
corpus_file.pages = form.pages.data
has_changes = True
if corpus_file.publisher != form.publisher.data:
corpus_file.publisher = form.publisher.data
has_changes = True
if corpus_file.publishing_year != form.publishing_year.data:
corpus_file.publishing_year = form.publishing_year.data
has_changes = True
if corpus_file.school != form.school.data:
corpus_file.school = form.school.data
has_changes = True
if corpus_file.title != form.title.data:
corpus_file.title = form.title.data
has_changes = True
if has_changes:
corpus_file.corpus.status = CorpusStatus.UNPREPARED
db.session.commit()
2022-09-02 11:07:30 +00:00
message = Markup(f'Corpus file "<a href="{corpus_file.url}">{corpus_file.filename}</a>" updated')
flash(message, category='corpus')
return redirect(corpus_file.corpus.url)
form.prefill(corpus_file)
return render_template(
'corpora/corpus_file.html.j2',
corpus=corpus_file.corpus,
corpus_file=corpus_file,
form=form,
title='Edit corpus file'
)
2022-09-02 11:07:30 +00:00
@bp.route('/<hashid:corpus_id>/files/<hashid:corpus_file_id>', methods=['DELETE'])
2020-04-06 12:12:22 +00:00
@login_required
def delete_corpus_file(corpus_id, corpus_file_id):
2022-09-02 11:07:30 +00:00
def _delete_corpus_file(app, corpus_file_id):
with app.app_context():
corpus_file = CorpusFile.query.get(corpus_file_id)
corpus_file.delete()
db.session.commit()
corpus_file = CorpusFile.query.get_or_404(corpus_file_id)
if corpus_file.corpus.id != corpus_id:
abort(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-09-02 11:07:30 +00:00
thread = Thread(
target=_delete_corpus_file,
args=(current_app._get_current_object(), corpus_file_id)
)
2022-09-02 11:07:30 +00:00
thread.start()
return {}, 202
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-09-02 11:07:30 +00:00
corpus_file = CorpusFile.query.get_or_404(corpus_file_id)
if corpus_file.corpus.id != corpus_id:
abort(404)
if not (corpus_file.corpus.user == current_user or current_user.is_administrator()):
2020-04-06 12:12:22 +00:00
abort(403)
return send_from_directory(
2022-09-02 11:07:30 +00:00
os.path.dirname(corpus_file.path),
os.path.basename(corpus_file.path),
as_attachment=True,
2022-04-12 14:11:24 +00:00
attachment_filename=corpus_file.filename,
2022-09-02 11:07:30 +00:00
mimetype=corpus_file.mimetype
)
2022-09-02 11:07:30 +00:00
@bp.route('/import', methods=['GET', 'POST'])
@login_required
def import_corpus():
abort(503)
@bp.route('/<hashid:corpus_id>/export')
@login_required
def export_corpus(corpus_id):
abort(503)