110 lines
3.3 KiB
Python
Raw Normal View History

2023-03-13 16:22:42 +01:00
from flask import abort, flash, redirect, render_template, url_for
2023-04-11 11:46:33 +02:00
from flask_login import current_user
2023-03-10 10:33:11 +01:00
from app import db
from app.models import (
Corpus,
CorpusFollowerAssociation,
2023-02-23 13:05:04 +01:00
CorpusFollowerRole,
User
)
2022-09-02 13:07:30 +02:00
from . import bp
2023-04-12 12:45:41 +02:00
from .decorators import corpus_follower_permission_required
2023-03-10 10:33:11 +01:00
from .forms import CreateCorpusForm
2023-03-13 16:22:42 +01:00
@bp.route('')
def corpora():
2024-09-25 12:08:20 +02:00
return redirect(url_for('main.dashboard', _anchor='corpora'))
2023-03-13 16:22:42 +01:00
2022-09-02 13:07:30 +02:00
@bp.route('/create', methods=['GET', 'POST'])
def create_corpus():
form = CreateCorpusForm()
if form.validate_on_submit():
2020-04-06 14:12:22 +02:00
try:
2022-09-02 13:07:30 +02: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 10:33:11 +01:00
flash(f'Corpus "{corpus.title}" created', 'corpus')
2022-09-02 13:07:30 +02:00
return redirect(corpus.url)
return render_template(
'corpora/create.html.j2',
2023-03-28 14:19:37 +02:00
title='Create corpus',
form=form
)
2023-02-21 16:18:04 +01:00
@bp.route('/<hashid:corpus_id>')
2020-04-06 14:12:22 +02:00
def corpus(corpus_id):
corpus = Corpus.query.get_or_404(corpus_id)
2023-04-27 15:11:18 +02:00
cfrs = CorpusFollowerRole.query.all()
# 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 15:11:18 +02: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:
2023-04-27 15:11:18 +02:00
cfr = CorpusFollowerRole.query.filter_by(name='Administrator').first()
else:
cfr = CorpusFollowerRole.query.filter_by(name='Anonymous').first()
else:
cfr = cfa.role
if corpus.user == current_user or current_user.is_administrator:
return render_template(
'corpora/corpus.html.j2',
title=corpus.title,
corpus=corpus,
cfr=cfr,
cfrs=cfrs,
2023-10-25 16:21:30 +02:00
users=users
)
if (current_user.is_following_corpus(corpus) or corpus.is_public):
2023-10-25 16:21:30 +02:00
cfas = CorpusFollowerAssociation.query.filter(Corpus.id == corpus_id, CorpusFollowerAssociation.follower_id != corpus.user.id).all()
return render_template(
'corpora/public_corpus.html.j2',
title=corpus.title,
corpus=corpus,
cfrs=cfrs,
cfr=cfr,
cfas=cfas,
users=users
)
abort(403)
2022-11-29 15:28:10 +01:00
@bp.route('/<hashid:corpus_id>/analysis')
2023-03-09 12:07:16 +01:00
@corpus_follower_permission_required('VIEW')
def analysis(corpus_id):
2023-03-09 12:07:16 +01:00
corpus = Corpus.query.get_or_404(corpus_id)
return render_template(
'corpora/analysis.html.j2',
2023-03-09 12:07:16 +01:00
corpus=corpus,
title=f'Analyse Corpus {corpus.title}'
)
2023-12-21 12:48:50 +01:00
@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()
flash(f'You are following "{corpus.title}" now', category='corpus')
return redirect(url_for('corpora.corpus', corpus_id=corpus_id))
abort(403)
2023-03-09 12:07:16 +01:00
@bp.route('/import', methods=['GET', 'POST'])
def import_corpus():
abort(503)
@bp.route('/<hashid:corpus_id>/export')
2023-04-12 12:45:41 +02:00
@corpus_follower_permission_required('VIEW')
2023-03-09 12:07:16 +01:00
def export_corpus(corpus_id):
abort(503)