2023-03-13 15:22:42 +00:00
|
|
|
from flask import abort, flash, redirect, render_template, url_for
|
2023-03-14 10:13:35 +00:00
|
|
|
from flask_breadcrumbs import register_breadcrumb
|
2023-04-11 09:46:33 +00:00
|
|
|
from flask_login import current_user
|
2023-03-10 09:33:11 +00:00
|
|
|
from app import db
|
2023-02-15 15:17:25 +00:00
|
|
|
from app.models import (
|
|
|
|
Corpus,
|
|
|
|
CorpusFollowerAssociation,
|
2023-02-23 12:05:04 +00:00
|
|
|
CorpusFollowerRole,
|
2023-03-13 12:29:01 +00:00
|
|
|
User
|
2023-02-15 15:17:25 +00:00
|
|
|
)
|
2022-09-02 11:07:30 +00:00
|
|
|
from . import bp
|
2023-04-12 10:45:41 +00:00
|
|
|
from .decorators import corpus_follower_permission_required
|
2023-03-10 09:33:11 +00:00
|
|
|
from .forms import CreateCorpusForm
|
2023-03-14 10:13:35 +00:00
|
|
|
from .utils import (
|
|
|
|
corpus_endpoint_arguments_constructor as corpus_eac,
|
|
|
|
corpus_dynamic_list_constructor as corpus_dlc
|
|
|
|
)
|
2023-02-15 09:56:54 +00:00
|
|
|
|
2023-02-21 15:23:10 +00:00
|
|
|
|
2023-03-13 15:22:42 +00:00
|
|
|
@bp.route('')
|
2023-03-14 10:13:35 +00:00
|
|
|
@register_breadcrumb(bp, '.', '<i class="nopaque-icons left">I</i>My Corpora')
|
2023-03-13 15:22:42 +00:00
|
|
|
def corpora():
|
|
|
|
return redirect(url_for('main.dashboard', _anchor='corpora'))
|
|
|
|
|
|
|
|
|
2022-09-02 11:07:30 +00:00
|
|
|
@bp.route('/create', methods=['GET', 'POST'])
|
2023-03-14 10:13:35 +00:00
|
|
|
@register_breadcrumb(bp, '.create', 'Create')
|
2022-09-02 11:07:30 +00:00
|
|
|
def create_corpus():
|
2022-11-30 13:58:45 +00:00
|
|
|
form = CreateCorpusForm()
|
2020-11-13 09:01:51 +00:00
|
|
|
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:
|
2020-11-13 09:01:51 +00:00
|
|
|
abort(500)
|
2022-02-03 11:39:16 +00:00
|
|
|
db.session.commit()
|
2023-03-10 09:33:11 +00:00
|
|
|
flash(f'Corpus "{corpus.title}" created', 'corpus')
|
2022-09-02 11:07:30 +00:00
|
|
|
return redirect(corpus.url)
|
2022-02-08 11:26:20 +00:00
|
|
|
return render_template(
|
2023-04-18 09:32:04 +00:00
|
|
|
'corpora/create.html.j2',
|
2023-03-28 12:19:37 +00:00
|
|
|
title='Create corpus',
|
|
|
|
form=form
|
2022-02-08 11:26:20 +00:00
|
|
|
)
|
2020-10-29 14:20:30 +00:00
|
|
|
|
|
|
|
|
2023-02-21 15:18:04 +00:00
|
|
|
@bp.route('/<hashid:corpus_id>')
|
2023-03-14 10:13:35 +00:00
|
|
|
@register_breadcrumb(bp, '.entity', '', dynamic_list_constructor=corpus_dlc)
|
2020-04-06 12:12:22 +00:00
|
|
|
def corpus(corpus_id):
|
|
|
|
corpus = Corpus.query.get_or_404(corpus_id)
|
2023-04-27 13:11:18 +00:00
|
|
|
cfrs = CorpusFollowerRole.query.all()
|
2023-05-05 06:41:14 +00:00
|
|
|
# TODO: Better solution for filtering admin
|
|
|
|
users = User.query.filter(User.is_public == True, User.id != current_user.id, User.id != corpus.user.id, User.role_id < 4).all()
|
2023-04-27 13:11:18 +00:00
|
|
|
cfa = CorpusFollowerAssociation.query.filter_by(corpus_id=corpus_id, follower_id=current_user.id).first()
|
|
|
|
if cfa is None:
|
|
|
|
if corpus.user == current_user or current_user.is_administrator():
|
|
|
|
cfr = CorpusFollowerRole.query.filter_by(name='Administrator').first()
|
|
|
|
else:
|
|
|
|
cfr = CorpusFollowerRole.query.filter_by(name='Anonymous').first()
|
|
|
|
else:
|
|
|
|
cfr = cfa.role
|
|
|
|
return render_template(
|
|
|
|
'corpora/corpus.html.j2',
|
|
|
|
title=corpus.title,
|
|
|
|
corpus=corpus,
|
|
|
|
cfrs=cfrs,
|
|
|
|
cfr=cfr,
|
|
|
|
users = users
|
|
|
|
)
|
2022-11-29 14:28:10 +00:00
|
|
|
|
2023-03-01 15:31:41 +00:00
|
|
|
|
2023-04-18 09:32:04 +00:00
|
|
|
@bp.route('/<hashid:corpus_id>/analysis')
|
2023-03-09 11:07:16 +00:00
|
|
|
@corpus_follower_permission_required('VIEW')
|
2023-04-18 09:32:04 +00:00
|
|
|
@register_breadcrumb(bp, '.entity.analysis', 'Analysis', endpoint_arguments_constructor=corpus_eac)
|
|
|
|
def analysis(corpus_id):
|
2023-03-09 11:07:16 +00:00
|
|
|
corpus = Corpus.query.get_or_404(corpus_id)
|
|
|
|
return render_template(
|
2023-04-18 09:32:04 +00:00
|
|
|
'corpora/analysis.html.j2',
|
2023-03-09 11:07:16 +00:00
|
|
|
corpus=corpus,
|
|
|
|
title=f'Analyse Corpus {corpus.title}'
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/<hashid:corpus_id>/follow/<token>')
|
|
|
|
def follow_corpus(corpus_id, token):
|
|
|
|
corpus = Corpus.query.get_or_404(corpus_id)
|
|
|
|
if current_user.follow_corpus_by_token(token):
|
|
|
|
db.session.commit()
|
2023-03-10 09:33:11 +00:00
|
|
|
flash(f'You are following "{corpus.title}" now', category='corpus')
|
2023-03-09 11:07:16 +00:00
|
|
|
return redirect(url_for('corpora.corpus', corpus_id=corpus_id))
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/import', methods=['GET', 'POST'])
|
2023-03-14 10:13:35 +00:00
|
|
|
@register_breadcrumb(bp, '.import', 'Import')
|
2023-03-09 11:07:16 +00:00
|
|
|
def import_corpus():
|
|
|
|
abort(503)
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/<hashid:corpus_id>/export')
|
2023-04-12 10:45:41 +00:00
|
|
|
@corpus_follower_permission_required('VIEW')
|
2023-03-14 10:13:35 +00:00
|
|
|
@register_breadcrumb(bp, '.entity.export', 'Export', endpoint_arguments_constructor=corpus_eac)
|
2023-03-09 11:07:16 +00:00
|
|
|
def export_corpus(corpus_id):
|
|
|
|
abort(503)
|