mirror of
				https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
				synced 2025-11-04 12:22:47 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			335 lines
		
	
	
		
			11 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			335 lines
		
	
	
		
			11 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
from flask import (
 | 
						|
    abort,
 | 
						|
    current_app,
 | 
						|
    flash,
 | 
						|
    Markup,
 | 
						|
    redirect,
 | 
						|
    render_template,
 | 
						|
    request,
 | 
						|
    send_from_directory,
 | 
						|
    url_for
 | 
						|
)
 | 
						|
from flask_login import current_user, login_required
 | 
						|
from threading import Thread
 | 
						|
import os
 | 
						|
from app import db, hashids
 | 
						|
from app.models import Corpus, CorpusFile, CorpusStatus, CorpusFollowerAssociation, User
 | 
						|
from . import bp
 | 
						|
from .forms import ChangeCorpusSettingsForm, CreateCorpusFileForm, CreateCorpusForm, UpdateCorpusFileForm
 | 
						|
 | 
						|
 | 
						|
@bp.route('')
 | 
						|
@login_required
 | 
						|
def corpora():
 | 
						|
    query = Corpus.query.filter(
 | 
						|
        (Corpus.user_id == current_user.id) | (Corpus.is_public == True)
 | 
						|
    )
 | 
						|
    corpora = [c.to_json_serializeable() for c in query.all()]
 | 
						|
    return render_template('corpora/corpora.html.j2', corpora=corpora, title='Corpora')
 | 
						|
 | 
						|
 | 
						|
@bp.route('/create', methods=['GET', 'POST'])
 | 
						|
@login_required
 | 
						|
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()
 | 
						|
        message = Markup(
 | 
						|
            f'Corpus "<a href="{corpus.url}">{corpus.title}</a>" created'
 | 
						|
        )
 | 
						|
        flash(message, 'corpus')
 | 
						|
        return redirect(corpus.url)
 | 
						|
    return render_template(
 | 
						|
        'corpora/create_corpus.html.j2',
 | 
						|
        form=form,
 | 
						|
        title='Create corpus'
 | 
						|
    )
 | 
						|
 | 
						|
 | 
						|
@bp.route('/<hashid:corpus_id>', methods=['GET', 'POST'])
 | 
						|
@login_required
 | 
						|
def corpus(corpus_id):
 | 
						|
    corpus = Corpus.query.get_or_404(corpus_id)
 | 
						|
    if not (corpus.user == current_user
 | 
						|
            or current_user.is_administrator()
 | 
						|
            or current_user.is_following_corpus(corpus)
 | 
						|
            or corpus.is_public):
 | 
						|
        abort(403)
 | 
						|
    corpus_settings_form = ChangeCorpusSettingsForm(
 | 
						|
        data=corpus.to_json_serializeable(),
 | 
						|
        prefix='corpus-settings-form'
 | 
						|
    )
 | 
						|
    if corpus_settings_form.validate_on_submit():
 | 
						|
        corpus.is_public = corpus_settings_form.is_public.data
 | 
						|
        db.session.commit()
 | 
						|
        flash('Your changes have been saved')
 | 
						|
        return redirect(url_for('.corpus', corpus_id=corpus.id))
 | 
						|
    if corpus.user == current_user or current_user.is_administrator():
 | 
						|
        return render_template(
 | 
						|
            'corpora/corpus.html.j2',
 | 
						|
            corpus_settings_form=corpus_settings_form,
 | 
						|
            corpus=corpus,
 | 
						|
            title='Corpus'
 | 
						|
        )
 | 
						|
    else:
 | 
						|
        corpus_files = [x.to_json_serializeable() for x in corpus.files]
 | 
						|
        return render_template(
 | 
						|
            'corpora/corpus_public.html.j2',
 | 
						|
            corpus=corpus,
 | 
						|
            corpus_files=corpus_files,
 | 
						|
            title='Corpus'
 | 
						|
        )
 | 
						|
 | 
						|
 | 
						|
 | 
						|
# @bp.route('/<hashid:corpus_id>/update')
 | 
						|
# @login_required
 | 
						|
# def update_corpus(corpus_id):
 | 
						|
#     corpus = Corpus.query.get_or_404(corpus_id)
 | 
						|
#     if not (corpus.user == current_user or current_user.is_administrator()):
 | 
						|
#         abort(403)
 | 
						|
#     return render_template(
 | 
						|
#         'corpora/update_corpus.html.j2',
 | 
						|
#         corpus=corpus,
 | 
						|
#         title='Corpus'
 | 
						|
#     )
 | 
						|
 | 
						|
 | 
						|
@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
 | 
						|
 | 
						|
 | 
						|
@bp.route('/<hashid:corpus_id>/analyse')
 | 
						|
@login_required
 | 
						|
def analyse_corpus(corpus_id):
 | 
						|
    corpus = Corpus.query.get_or_404(corpus_id)
 | 
						|
    if not (corpus.user == current_user
 | 
						|
            or current_user.is_administrator()
 | 
						|
            or current_user.is_following_corpus(corpus)
 | 
						|
            or corpus.is_public):
 | 
						|
        abort(403)
 | 
						|
    return render_template(
 | 
						|
        'corpora/analyse_corpus.html.j2',
 | 
						|
        corpus=corpus,
 | 
						|
        title=f'Analyse Corpus {corpus.title}'
 | 
						|
    )
 | 
						|
 | 
						|
 | 
						|
@bp.route('/<hashid:corpus_id>/build', methods=['POST'])
 | 
						|
@login_required
 | 
						|
def build_corpus(corpus_id):
 | 
						|
    def _build_corpus(app, corpus_id):
 | 
						|
        with app.app_context():
 | 
						|
            corpus = Corpus.query.get(corpus_id)
 | 
						|
            corpus.build()
 | 
						|
            db.session.commit()
 | 
						|
 | 
						|
    corpus = Corpus.query.get_or_404(corpus_id)
 | 
						|
    if not (corpus.user == current_user or current_user.is_administrator()):
 | 
						|
        abort(403)
 | 
						|
    # 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
 | 
						|
 | 
						|
 | 
						|
@bp.route('/<hashid:corpus_id>/files/create', methods=['GET', 'POST'])
 | 
						|
@login_required
 | 
						|
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)
 | 
						|
    form = CreateCorpusFileForm()
 | 
						|
    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 (AttributeError, 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'
 | 
						|
    )
 | 
						|
 | 
						|
 | 
						|
@bp.route('/<hashid:corpus_id>/files/<hashid:corpus_file_id>', methods=['GET', 'POST'])
 | 
						|
@login_required
 | 
						|
def corpus_file(corpus_id, corpus_file_id):
 | 
						|
    corpus_file = CorpusFile.query.filter_by(corpus_id = corpus_id, id=corpus_file_id).first_or_404()
 | 
						|
    if not (corpus_file.corpus.user == current_user or current_user.is_administrator()):
 | 
						|
        abort(403)
 | 
						|
    form = UpdateCorpusFileForm(data=corpus_file.to_json_serializeable())
 | 
						|
    if form.validate_on_submit():
 | 
						|
        form.populate_obj(corpus_file)
 | 
						|
        if db.session.is_modified(corpus_file):
 | 
						|
            corpus_file.corpus.status = CorpusStatus.UNPREPARED
 | 
						|
            db.session.commit()
 | 
						|
            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)
 | 
						|
    return render_template(
 | 
						|
        'corpora/corpus_file.html.j2',
 | 
						|
        corpus=corpus_file.corpus,
 | 
						|
        corpus_file=corpus_file,
 | 
						|
        form=form,
 | 
						|
        title='Edit corpus file'
 | 
						|
    )
 | 
						|
 | 
						|
 | 
						|
@bp.route('/<hashid:corpus_id>/files/<hashid:corpus_file_id>', methods=['DELETE'])
 | 
						|
@login_required
 | 
						|
def delete_corpus_file(corpus_id, corpus_file_id):
 | 
						|
    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.filter_by(corpus_id = corpus_id, id=corpus_file_id).first_or_404()
 | 
						|
    if not (corpus_file.corpus.user == current_user or current_user.is_administrator()):
 | 
						|
        abort(403)
 | 
						|
    thread = Thread(
 | 
						|
        target=_delete_corpus_file,
 | 
						|
        args=(current_app._get_current_object(), corpus_file_id)
 | 
						|
    )
 | 
						|
    thread.start()
 | 
						|
    return {}, 202
 | 
						|
 | 
						|
 | 
						|
@bp.route('/<hashid:corpus_id>/files/<hashid:corpus_file_id>/download')
 | 
						|
@login_required
 | 
						|
def download_corpus_file(corpus_id, corpus_file_id):
 | 
						|
    corpus_file = CorpusFile.query.filter_by(corpus_id = corpus_id, id=corpus_file_id).first_or_404()
 | 
						|
    if not (corpus_file.corpus.user == current_user or current_user.is_administrator()):
 | 
						|
        abort(403)
 | 
						|
    return send_from_directory(
 | 
						|
        os.path.dirname(corpus_file.path),
 | 
						|
        os.path.basename(corpus_file.path),
 | 
						|
        as_attachment=True,
 | 
						|
        attachment_filename=corpus_file.filename,
 | 
						|
        mimetype=corpus_file.mimetype
 | 
						|
    )
 | 
						|
 | 
						|
 | 
						|
@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)
 | 
						|
 | 
						|
@bp.route('/<hashid:corpus_id>/follow', methods=['GET', 'POST'])
 | 
						|
@login_required
 | 
						|
def follow_corpus(corpus_id):
 | 
						|
    corpus = Corpus.query.get_or_404(corpus_id)
 | 
						|
    user_hashid = request.args.get('user_id')
 | 
						|
    if user_hashid is None:
 | 
						|
        user = current_user
 | 
						|
    else:
 | 
						|
        if not current_user.is_administrator():
 | 
						|
            abort(403)
 | 
						|
        else:
 | 
						|
            user_id = hashids.decode(user_hashid)
 | 
						|
            user = User.query.get_or_404(user_id)
 | 
						|
    if not user.is_following_corpus(corpus):
 | 
						|
        user.follow_corpus(corpus)
 | 
						|
    db.session.commit()
 | 
						|
    # flash('Hallo Inga Kirschnick')
 | 
						|
    return {}, 202
 | 
						|
 | 
						|
@bp.route('/<hashid:corpus_id>/unfollow', methods=['GET', 'POST'])
 | 
						|
@login_required
 | 
						|
def unfollow_corpus(corpus_id):
 | 
						|
    corpus = Corpus.query.get_or_404(corpus_id)
 | 
						|
    user_hashid = request.args.get('user_id')
 | 
						|
    if user_hashid is None:
 | 
						|
        user = current_user
 | 
						|
    else:
 | 
						|
        if not current_user.is_administrator():
 | 
						|
            abort(403)
 | 
						|
        else:
 | 
						|
            user_id = hashids.decode(user_hashid)
 | 
						|
            user = User.query.get_or_404(user_id)
 | 
						|
    if user.is_following_corpus(corpus):
 | 
						|
        user.unfollow_corpus(corpus)
 | 
						|
    db.session.commit()
 | 
						|
    return {}, 202
 | 
						|
 | 
						|
@bp.route('/add_permission/<hashid:corpus_id>/<hashid:user_id>/<int:permission>')
 | 
						|
def add_permission(corpus_id, user_id, permission):
 | 
						|
    a = CorpusFollowerAssociation.query.filter_by(followed_corpus_id=corpus_id, following_user_id=user_id).first_or_404()
 | 
						|
    a.add_permission(permission)
 | 
						|
    db.session.commit()
 | 
						|
    return 'ok'
 | 
						|
 | 
						|
 | 
						|
@bp.route('/remove_permission/<hashid:corpus_id>/<hashid:user_id>/<int:permission>')
 | 
						|
def remove_permission(corpus_id, user_id, permission):
 | 
						|
    a = CorpusFollowerAssociation.query.filter_by(followed_corpus_id=corpus_id, following_user_id=user_id).first_or_404()
 | 
						|
    a.remove_permission(permission)
 | 
						|
    db.session.commit()
 | 
						|
    return 'ok'
 | 
						|
 |