Skip to content
Snippets Groups Projects
routes.py 4.01 KiB
from flask import abort, flash, redirect, render_template, url_for
from flask_breadcrumbs import register_breadcrumb
from flask_login import current_user
from app import db
from app.models import (
    Corpus,
    CorpusFollowerAssociation,
    CorpusFollowerRole,
    User
)
from . import bp
from .decorators import corpus_follower_permission_required
from .forms import CreateCorpusForm
from .utils import (
    corpus_endpoint_arguments_constructor as corpus_eac,
    corpus_dynamic_list_constructor as corpus_dlc
)


@bp.route('')
@register_breadcrumb(bp, '.', '<i class="nopaque-icons left">I</i>My Corpora')
def corpora():
    return redirect(url_for('main.dashboard', _anchor='corpora'))


@bp.route('/create', methods=['GET', 'POST'])
@register_breadcrumb(bp, '.create', 'Create')
def create_corpus():
    form = CreateCorpusForm()
    if form.validate_on_submit():
        try:
            corpus = Corpus.create(
                title=form.title.data,
                description=form.description.data,
                user=current_user
            )
        except OSError:
            abort(500)
        db.session.commit()
        flash(f'Corpus "{corpus.title}" created', 'corpus')
        return redirect(corpus.url)
    return render_template(
        'corpora/create.html.j2',
        title='Create corpus',
        form=form
    )


@bp.route('/<hashid:corpus_id>')
@register_breadcrumb(bp, '.entity', '', dynamic_list_constructor=corpus_dlc)
def corpus(corpus_id):
    corpus = Corpus.query.get_or_404(corpus_id)
    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()
    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
    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,
            users = users
        )
    if (current_user.is_following_corpus(corpus) or corpus.is_public):
        abort(404)
        # 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)



@bp.route('/<hashid:corpus_id>/analysis')
@corpus_follower_permission_required('VIEW')
@register_breadcrumb(bp, '.entity.analysis', 'Analysis', endpoint_arguments_constructor=corpus_eac)
def analysis(corpus_id):
    corpus = Corpus.query.get_or_404(corpus_id)
    return render_template(
        'corpora/analysis.html.j2',
        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()
#         flash(f'You are following "{corpus.title}" now', category='corpus')
#         return redirect(url_for('corpora.corpus', corpus_id=corpus_id))
#     abort(403)


@bp.route('/import', methods=['GET', 'POST'])
@register_breadcrumb(bp, '.import', 'Import')
def import_corpus():
    abort(503)


@bp.route('/<hashid:corpus_id>/export')
@corpus_follower_permission_required('VIEW')
@register_breadcrumb(bp, '.entity.export', 'Export', endpoint_arguments_constructor=corpus_eac)
def export_corpus(corpus_id):
    abort(503)