2024-12-16 09:09:54 +00:00
|
|
|
|
from datetime import datetime
|
|
|
|
|
from flask import (
|
|
|
|
|
abort,
|
|
|
|
|
current_app,
|
|
|
|
|
flash,
|
|
|
|
|
Flask,
|
|
|
|
|
jsonify,
|
|
|
|
|
redirect,
|
|
|
|
|
render_template,
|
|
|
|
|
url_for
|
|
|
|
|
)
|
2023-04-11 09:46:33 +00:00
|
|
|
|
from flask_login import current_user
|
2024-12-16 09:09:54 +00:00
|
|
|
|
from string import punctuation
|
|
|
|
|
from threading import Thread
|
|
|
|
|
import nltk
|
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-02-15 09:56:54 +00:00
|
|
|
|
|
2023-02-21 15:23:10 +00:00
|
|
|
|
|
2024-12-16 09:09:54 +00:00
|
|
|
|
|
|
|
|
|
def _delete_corpus(app: Flask, corpus_id: int):
|
|
|
|
|
with app.app_context():
|
|
|
|
|
corpus: Corpus = Corpus.query.get(corpus_id)
|
|
|
|
|
corpus.delete()
|
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _build_corpus(app: Flask, corpus_id: int):
|
|
|
|
|
with app.app_context():
|
|
|
|
|
corpus = Corpus.query.get(corpus_id)
|
|
|
|
|
corpus.build()
|
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
|
|
|
2023-03-13 15:22:42 +00:00
|
|
|
|
@bp.route('')
|
|
|
|
|
def corpora():
|
2024-09-25 10:08:20 +00:00
|
|
|
|
return redirect(url_for('main.dashboard', _anchor='corpora'))
|
2023-03-13 15:22:42 +00:00
|
|
|
|
|
|
|
|
|
|
2022-09-02 11:07:30 +00:00
|
|
|
|
@bp.route('/create', methods=['GET', 'POST'])
|
|
|
|
|
def create_corpus():
|
2022-11-30 13:58:45 +00:00
|
|
|
|
form = CreateCorpusForm()
|
2024-12-16 09:09:54 +00:00
|
|
|
|
|
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()
|
2024-12-16 09:09:54 +00:00
|
|
|
|
|
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)
|
2024-12-16 09:09:54 +00:00
|
|
|
|
|
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>')
|
2024-12-16 09:09:54 +00:00
|
|
|
|
def corpus(corpus_id: int):
|
2020-04-06 12:12:22 +00:00
|
|
|
|
corpus = Corpus.query.get_or_404(corpus_id)
|
2024-12-16 09:09:54 +00:00
|
|
|
|
|
|
|
|
|
cfa = CorpusFollowerAssociation.query.filter_by(
|
|
|
|
|
corpus_id=corpus_id,
|
|
|
|
|
follower_id=current_user.id
|
|
|
|
|
).first()
|
|
|
|
|
|
2023-04-27 13:11:18 +00:00
|
|
|
|
if cfa is None:
|
2024-04-11 12:33:47 +00:00
|
|
|
|
if corpus.user == current_user or current_user.is_administrator:
|
2023-04-27 13:11:18 +00:00
|
|
|
|
cfr = CorpusFollowerRole.query.filter_by(name='Administrator').first()
|
|
|
|
|
else:
|
|
|
|
|
cfr = CorpusFollowerRole.query.filter_by(name='Anonymous').first()
|
|
|
|
|
else:
|
|
|
|
|
cfr = cfa.role
|
2024-12-16 09:09:54 +00: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()
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
corpus.user == current_user
|
|
|
|
|
or current_user.is_administrator
|
|
|
|
|
):
|
2023-05-12 11:43:38 +00:00
|
|
|
|
return render_template(
|
|
|
|
|
'corpora/corpus.html.j2',
|
|
|
|
|
title=corpus.title,
|
|
|
|
|
corpus=corpus,
|
|
|
|
|
cfr=cfr,
|
|
|
|
|
cfrs=cfrs,
|
2023-10-25 14:21:30 +00:00
|
|
|
|
users=users
|
2023-05-12 11:43:38 +00:00
|
|
|
|
)
|
2024-12-16 09:09:54 +00:00
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
current_user.is_following_corpus(corpus)
|
|
|
|
|
or corpus.is_public
|
|
|
|
|
):
|
|
|
|
|
cfas = CorpusFollowerAssociation.query.filter(
|
|
|
|
|
Corpus.id == corpus_id,
|
|
|
|
|
CorpusFollowerAssociation.follower_id != corpus.user.id
|
|
|
|
|
).all()
|
2023-10-25 14:21:30 +00:00
|
|
|
|
return render_template(
|
|
|
|
|
'corpora/public_corpus.html.j2',
|
|
|
|
|
title=corpus.title,
|
|
|
|
|
corpus=corpus,
|
|
|
|
|
cfrs=cfrs,
|
|
|
|
|
cfr=cfr,
|
|
|
|
|
cfas=cfas,
|
|
|
|
|
users=users
|
|
|
|
|
)
|
2024-12-16 09:09:54 +00:00
|
|
|
|
|
2023-05-12 11:43:38 +00:00
|
|
|
|
abort(403)
|
|
|
|
|
|
2022-11-29 14:28:10 +00:00
|
|
|
|
|
2024-12-16 09:09:54 +00:00
|
|
|
|
@bp.route('/<hashid:corpus_id>', methods=['DELETE'])
|
|
|
|
|
def delete_corpus(corpus_id: int):
|
|
|
|
|
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 jsonify(f'Corpus "{corpus.title}" marked for deletion.'), 202
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/<hashid:corpus_id>/build', methods=['PATCH'])
|
|
|
|
|
def build_corpus(corpus_id: int):
|
|
|
|
|
corpus = Corpus.query.get_or_404(corpus_id)
|
|
|
|
|
|
|
|
|
|
cfa = CorpusFollowerAssociation.query.filter_by(
|
|
|
|
|
corpus_id=corpus_id,
|
|
|
|
|
follower_id=current_user.id
|
|
|
|
|
).first()
|
|
|
|
|
|
|
|
|
|
if not (
|
|
|
|
|
cfa is not None and cfa.role.has_permission('MANAGE_FILES')
|
|
|
|
|
or corpus.user == current_user
|
|
|
|
|
or current_user.is_administrator
|
|
|
|
|
):
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
|
if len(corpus.files.all()) == 0:
|
|
|
|
|
abort(409)
|
|
|
|
|
|
|
|
|
|
thread = Thread(
|
|
|
|
|
target=_build_corpus,
|
|
|
|
|
args=(current_app._get_current_object(), corpus.id)
|
|
|
|
|
)
|
|
|
|
|
thread.start()
|
|
|
|
|
|
|
|
|
|
return jsonify(f'Corpus "{corpus.title}" marked for building.'), 202
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/<hashid:corpus_id>/generate-share-link', methods=['POST'])
|
|
|
|
|
def create_share_link(corpus_id: int, expiration_date: str, role_name: str):
|
|
|
|
|
if not isinstance(expiration_date, str):
|
|
|
|
|
abort(400)
|
|
|
|
|
|
|
|
|
|
if not isinstance(role_name, str):
|
|
|
|
|
abort(400)
|
|
|
|
|
|
|
|
|
|
corpus = Corpus.query.get_or_404(corpus_id)
|
|
|
|
|
|
|
|
|
|
cfa = CorpusFollowerAssociation.query.filter_by(
|
|
|
|
|
corpus_id=corpus_id,
|
|
|
|
|
follower_id=current_user.id
|
|
|
|
|
).first()
|
|
|
|
|
|
|
|
|
|
if not (
|
|
|
|
|
cfa is not None and cfa.role.has_permission('MANAGE_FOLLOWERS')
|
|
|
|
|
or corpus.user == current_user
|
|
|
|
|
or current_user.is_administrator
|
|
|
|
|
):
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
|
_expiration_date = datetime.strptime(expiration_date, '%b %d, %Y')
|
|
|
|
|
|
|
|
|
|
cfr = CorpusFollowerRole.query.filter_by(name=role_name).first()
|
|
|
|
|
if cfr is None:
|
|
|
|
|
abort(400)
|
|
|
|
|
|
|
|
|
|
token = current_user.generate_follow_corpus_token(
|
|
|
|
|
corpus.hashid,
|
|
|
|
|
role_name,
|
|
|
|
|
_expiration_date
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
corpus_share_link = url_for(
|
|
|
|
|
'corpora.follow_corpus',
|
|
|
|
|
corpus_id=corpus_id,
|
|
|
|
|
token=token,
|
|
|
|
|
_external=True
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return jsonify(corpus_share_link)
|
|
|
|
|
|
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')
|
2024-12-16 09:09:54 +00:00
|
|
|
|
def analysis(corpus_id: int):
|
2023-03-09 11:07:16 +00:00
|
|
|
|
corpus = Corpus.query.get_or_404(corpus_id)
|
2024-12-16 09:09:54 +00:00
|
|
|
|
|
2023-03-09 11:07:16 +00:00
|
|
|
|
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}'
|
|
|
|
|
)
|
|
|
|
|
|
2024-12-16 09:09:54 +00:00
|
|
|
|
@bp.route('/<hashid:corpus_id>/analysis/stopwords')
|
|
|
|
|
def get_stopwords():
|
|
|
|
|
languages = [
|
|
|
|
|
'german',
|
|
|
|
|
'english',
|
|
|
|
|
'catalan',
|
|
|
|
|
'greek',
|
|
|
|
|
'spanish',
|
|
|
|
|
'french',
|
|
|
|
|
'italian',
|
|
|
|
|
'russian',
|
|
|
|
|
'chinese'
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
nltk.download('stopwords', quiet=True)
|
|
|
|
|
stopwords = {
|
|
|
|
|
language: nltk.corpus.stopwords.words(language)
|
|
|
|
|
for language in languages
|
|
|
|
|
}
|
|
|
|
|
stopwords['punctuation'] = list(punctuation)
|
|
|
|
|
stopwords['punctuation'] += ['—', '|', '–', '“', '„', '--']
|
|
|
|
|
stopwords['user_stopwords'] = []
|
|
|
|
|
|
|
|
|
|
return jsonify(stopwords)
|
|
|
|
|
|
2023-03-09 11:07:16 +00:00
|
|
|
|
|
2023-12-21 11:48:50 +00:00
|
|
|
|
@bp.route('/<hashid:corpus_id>/follow/<token>')
|
2024-12-16 09:09:54 +00:00
|
|
|
|
def follow_corpus(corpus_id: int, token: str):
|
2023-12-21 11:48:50 +00:00
|
|
|
|
corpus = Corpus.query.get_or_404(corpus_id)
|
2023-03-09 11:07:16 +00:00
|
|
|
|
|
2024-12-16 09:09:54 +00:00
|
|
|
|
if not current_user.follow_corpus_by_token(token):
|
|
|
|
|
abort(403)
|
2023-03-09 11:07:16 +00:00
|
|
|
|
|
2024-12-16 09:09:54 +00:00
|
|
|
|
db.session.commit()
|
2023-03-09 11:07:16 +00:00
|
|
|
|
|
2024-12-16 09:09:54 +00:00
|
|
|
|
flash(f'You are following "{corpus.title}" now', category='corpus')
|
|
|
|
|
return redirect(corpus.url)
|