nopaque/app/corpora/routes.py

107 lines
3.1 KiB
Python
Raw Normal View History

2023-03-13 15:22:42 +00:00
from flask import abort, flash, redirect, render_template, url_for
2020-04-06 12:12:22 +00:00
from flask_login import current_user, login_required
2023-03-10 09:33:11 +00:00
from .decorators import corpus_follower_permission_required
from app import db
from app.models import (
Corpus,
CorpusFollowerAssociation,
2023-02-23 12:05:04 +00:00
CorpusFollowerRole,
User
)
2022-09-02 11:07:30 +00:00
from . import bp
2023-03-10 09:33:11 +00:00
from .forms import CreateCorpusForm
2023-03-13 15:22:42 +00:00
@bp.route('')
@login_required
def corpora():
return redirect(url_for('main.dashboard', _anchor='corpora'))
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()
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()
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)
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'
)
2023-02-21 15:18:04 +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)
corpus_follower_roles = CorpusFollowerRole.query.all()
users = [u.to_json_serializeable() for u in User.query.filter(User.is_public == True, User.id != current_user.id).all()]
2023-03-10 09:33:11 +00:00
# TODO: Add URL query option to toggle view
if corpus.user == current_user or current_user.is_administrator():
2023-02-09 10:05:27 +00:00
return render_template(
'corpora/corpus.html.j2',
corpus=corpus,
corpus_follower_roles=corpus_follower_roles,
users = users,
2023-02-09 10:05:27 +00:00
title='Corpus'
)
if current_user.is_following_corpus(corpus) or corpus.is_public:
2023-03-02 08:57:43 +00:00
cfa = CorpusFollowerAssociation.query.filter_by(corpus_id=corpus_id, follower_id=current_user.id).first_or_404()
2023-02-09 11:02:00 +00:00
corpus_files = [x.to_json_serializeable() for x in corpus.files]
owner = corpus.user.to_json_serializeable()
2023-02-09 10:05:27 +00:00
return render_template(
'corpora/public_corpus.html.j2',
2023-02-09 10:05:27 +00:00
corpus=corpus,
2023-02-09 11:02:00 +00:00
corpus_files=corpus_files,
2023-03-02 08:57:43 +00:00
cfa=cfa,
owner=owner,
title='Corpus',
2023-02-09 10:05:27 +00:00
)
abort(403)
2022-11-29 14:28:10 +00:00
2023-03-09 11:07:16 +00:00
@bp.route('/<hashid:corpus_id>/analyse')
@login_required
@corpus_follower_permission_required('VIEW')
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}'
)
@bp.route('/<hashid:corpus_id>/follow/<token>')
@login_required
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'])
@login_required
def import_corpus():
abort(503)
@bp.route('/<hashid:corpus_id>/export')
@login_required
def export_corpus(corpus_id):
abort(503)