Extend CorpusList for use in public corpora route

This commit is contained in:
Patrick Jentsch 2022-11-30 14:58:45 +01:00
parent 5491cb184a
commit c8d7de434f
3 changed files with 45 additions and 8 deletions

View File

@ -31,14 +31,17 @@ def user_can_delete_corpus(user, corpus):
@bp.route('') @bp.route('')
@login_required @login_required
def corpora(): def corpora():
corpora = Corpus.query.filter(Corpus.user_id == current_user.id | Corpus.is_public == True).all() query = Corpus.query.filter(
return render_template('corpora/corpora.html', corpora=corpora) (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']) @bp.route('/create', methods=['GET', 'POST'])
@login_required @login_required
def create_corpus(): def create_corpus():
form = CreateCorpusForm(prefix='create-corpus-form') form = CreateCorpusForm()
if form.validate_on_submit(): if form.validate_on_submit():
try: try:
corpus = Corpus.create( corpus = Corpus.create(
@ -150,7 +153,7 @@ def create_corpus_file(corpus_id):
corpus = Corpus.query.get_or_404(corpus_id) corpus = Corpus.query.get_or_404(corpus_id)
if not user_can_update_corpus(current_user, corpus): if not user_can_update_corpus(current_user, corpus):
abort(403) abort(403)
form = CreateCorpusFileForm(prefix='create-corpus-file-form') form = CreateCorpusFileForm()
if form.is_submitted(): if form.is_submitted():
if not form.validate(): if not form.validate():
response = {'errors': form.errors} response = {'errors': form.errors}
@ -197,10 +200,7 @@ 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() 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()): if not (corpus_file.corpus.user == current_user or current_user.is_administrator()):
abort(403) abort(403)
form = UpdateCorpusFileForm( form = UpdateCorpusFileForm(data=corpus_file.to_json_serializeable())
data=corpus_file.to_json_serializeable(),
prefix='edit-corpus-file-form'
)
if form.validate_on_submit(): if form.validate_on_submit():
form.populate_obj(corpus_file) form.populate_obj(corpus_file)
if db.session.is_modified(corpus_file): if db.session.is_modified(corpus_file):

View File

@ -1,4 +1,12 @@
class CorpusList extends RessourceList { class CorpusList extends RessourceList {
static instances = [];
static getInstance(elem) {
return CorpusList.instances.find((instance) => {
return instance.listjs.list === elem;
});
}
static autoInit() { static autoInit() {
for (let corpusListElement of document.querySelectorAll('.corpus-list:not(.no-autoinit)')) { for (let corpusListElement of document.querySelectorAll('.corpus-list:not(.no-autoinit)')) {
new CorpusList(corpusListElement); new CorpusList(corpusListElement);
@ -59,6 +67,7 @@ class CorpusList extends RessourceList {
constructor(listElement, options = {}) { constructor(listElement, options = {}) {
super(listElement, {...CorpusList.options, ...options}); super(listElement, {...CorpusList.options, ...options});
CorpusList.instances.push(this);
} }
init(user) { init(user) {

View File

@ -0,0 +1,28 @@
{% extends "base.html.j2" %}
{% block page_content %}
<div class="container">
<div class="row">
<div class="col s12">
<h1 id="title">{{ title }}</h1>
</div>
<div class="col s12" id="corpora">
<div class="card">
<div class="card-content">
<div class="corpus-list"></div>
</div>
</div>
</div>
</div>
</div>
{% endblock page_content %}
{% block scripts %}
{{ super() }}
<script>
let corpusList = CorpusList.getInstance(document.querySelector('#corpora .corpus-list .list'));
corpusList._init({{ corpora|tojson }});
</script>
{% endblock scripts %}