Merge branch 'public-corpus' of gitlab.ub.uni-bielefeld.de:sfb1288inf/nopaque into public-corpus

This commit is contained in:
Inga Kirschnick 2023-02-10 09:43:05 +01:00
commit dabf0d1344
5 changed files with 132 additions and 27 deletions

View File

@ -59,7 +59,6 @@ def create_corpus():
@bp.route('/<hashid:corpus_id>', methods=['GET', 'POST']) @bp.route('/<hashid:corpus_id>', methods=['GET', 'POST'])
@login_required @login_required
def corpus(corpus_id): def corpus(corpus_id):
print(corpus_id)
corpus = Corpus.query.get_or_404(corpus_id) corpus = Corpus.query.get_or_404(corpus_id)
if not (corpus.user == current_user if not (corpus.user == current_user
or current_user.is_administrator() or current_user.is_administrator()
@ -89,7 +88,6 @@ def corpus(corpus_id):
if corpus.user == current_user: if corpus.user == current_user:
return render_template( return render_template(
'corpora/corpus.html.j2', 'corpora/corpus.html.j2',
corpus_settings_form=corpus_settings_form,
corpus=corpus, corpus=corpus,
token=token, token=token,
title='Corpus' title='Corpus'
@ -119,18 +117,26 @@ def share_corpus(token):
corpus_id = hashids.decode(corpus_hashid) corpus_id = hashids.decode(corpus_hashid)
return redirect(url_for('.corpus', corpus_id=corpus_id)) return redirect(url_for('.corpus', corpus_id=corpus_id))
@bp.route('/<hashid:corpus_id>/enable_is_public', methods=['POST'])
# @bp.route('/<hashid:corpus_id>/update') @login_required
# @login_required def enable_corpus_is_public(corpus_id):
# def update_corpus(corpus_id): corpus = Corpus.query.get_or_404(corpus_id)
# corpus = Corpus.query.get_or_404(corpus_id) if not (corpus.user == current_user or current_user.is_administrator()):
# if not (corpus.user == current_user or current_user.is_administrator()): abort(403)
# abort(403) corpus.is_public = True
# return render_template( db.session.commit()
# 'corpora/update_corpus.html.j2', return '', 204
# corpus=corpus,
# title='Corpus'
# ) @bp.route('/<hashid:corpus_id>/disable_is_public', methods=['POST'])
@login_required
def disable_corpus_is_public(corpus_id):
corpus = Corpus.query.get_or_404(corpus_id)
if not (corpus.user == current_user or current_user.is_administrator()):
abort(403)
corpus.is_public = False
db.session.commit()
return '', 204
@bp.route('/<hashid:corpus_id>', methods=['DELETE']) @bp.route('/<hashid:corpus_id>', methods=['DELETE'])

View File

@ -12,12 +12,23 @@ class CorpusDisplay extends RessourceDisplay {
.addEventListener('click', (event) => { .addEventListener('click', (event) => {
Utils.deleteCorpusRequest(this.userId, this.corpusId); Utils.deleteCorpusRequest(this.userId, this.corpusId);
}); });
this.displayElement
.querySelector('.action-switch[data-action="toggle-is-public"]')
.addEventListener('click', (event) => {
if (event.target.tagName !== 'INPUT') {return;}
if (event.target.checked) {
Utils.enableCorpusIsPublicRequest(this.userId, this.corpusId);
} else {
Utils.disableCorpusIsPublicRequest(this.userId, this.corpusId);
}
});
} }
init(user) { init(user) {
let corpus = user.corpora[this.corpusId]; let corpus = user.corpora[this.corpusId];
this.setCreationDate(corpus.creation_date); this.setCreationDate(corpus.creation_date);
this.setDescription(corpus.description); this.setDescription(corpus.description);
this.setIsPublic(corpus.is_public);
this.setStatus(corpus.status); this.setStatus(corpus.status);
this.setTitle(corpus.title); this.setTitle(corpus.title);
this.setNumTokens(corpus.num_tokens); this.setNumTokens(corpus.num_tokens);

View File

@ -69,6 +69,88 @@ class Utils {
return Utils.mergeObjectsDeep(mergedObject, ...objects.slice(2)); return Utils.mergeObjectsDeep(mergedObject, ...objects.slice(2));
} }
static enableCorpusIsPublicRequest(userId, corpusId) {
return new Promise((resolve, reject) => {
let corpus;
try {
corpus = app.data.users[userId].corpora[corpusId];
} catch (error) {
corpus = {};
}
let modalElement = Utils.HTMLToElement(
`
<div class="modal">
<div class="modal-content">
<h4>Hier könnte eine Warnung stehen</h4>
<p></p>
</div>
<div class="modal-footer">
<a class="action-button btn modal-close waves-effect waves-light" data-action="cancel">Cancel</a>
<a class="action-button btn modal-close red waves-effect waves-light" data-action="confirm">Confirm</a>
</div>
</div>
`
);
document.querySelector('#modals').appendChild(modalElement);
let modal = M.Modal.init(
modalElement,
{
dismissible: false,
onCloseEnd: () => {
modal.destroy();
modalElement.remove();
}
}
);
let confirmElement = modalElement.querySelector('.action-button[data-action="confirm"]');
confirmElement.addEventListener('click', (event) => {
let corpusTitle = corpus?.title;
fetch(`/corpora/${corpusId}/enable_is_public`, {method: 'POST', headers: {Accept: 'application/json'}})
.then(
(response) => {
if (response.status === 403) {app.flash('Forbidden', 'error'); reject(response);}
if (response.status === 404) {app.flash('Not Found', 'error'); reject(response);}
app.flash(`Corpus "${corpusTitle}" is public now`, 'corpus');
resolve(response);
},
(response) => {
app.flash('Something went wrong', 'error');
reject(response);
}
);
});
modal.open();
});
}
static disableCorpusIsPublicRequest(userId, corpusId) {
return new Promise((resolve, reject) => {
let corpus;
try {
corpus = app.data.users[userId].corpora[corpusId];
} catch (error) {
corpus = {};
}
let corpusTitle = corpus?.title;
fetch(`/corpora/${corpusId}/disable_is_public`, {method: 'POST', headers: {Accept: 'application/json'}})
.then(
(response) => {
if (response.status === 403) {app.flash('Forbidden', 'error'); reject(response);}
if (response.status === 404) {app.flash('Not Found', 'error'); reject(response);}
app.flash(`Corpus "${corpusTitle}" is private now`, 'corpus');
resolve(response);
},
(response) => {
app.flash('Something went wrong', 'error');
reject(response);
}
);
});
}
static buildCorpusRequest(userId, corpusId) { static buildCorpusRequest(userId, corpusId) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let corpus; let corpus;

View File

@ -11,13 +11,6 @@
<div class="row"> <div class="row">
<div class="col s8 m9 l10"> <div class="col s8 m9 l10">
<h1 id="title"><span class="corpus-title"></span></h1> <h1 id="title"><span class="corpus-title"></span></h1>
{% if not corpus.user == current_user %}
{% if current_user.is_following_corpus(corpus) %}
<a class="btn waves-effect waves-light" id="follow-corpus-request"><i class="material-icons left">add</i>Unfollow Corpus</a>
{% elif not current_user.is_following_corpus(corpus) %}
<a class="btn waves-effect waves-light" id="follow-corpus-request"><i class="material-icons left">add</i>Follow Corpus</a>
{% endif %}
{% endif %}
</div> </div>
<div class="col s4 m3 l2 right-align"> <div class="col s4 m3 l2 right-align">
<p>&nbsp;</p> <p>&nbsp;</p>
@ -64,11 +57,23 @@
</div> </div>
</div> </div>
</div> </div>
<div class="card-action right-align"> <div class="card-action">
<a class="btn corpus-analyse-trigger disabled waves-effect waves-light" href="{{ url_for('corpora.analyse_corpus', corpus_id=corpus.id) }}"><i class="material-icons left">search</i>Analyze</a> <div class="left-align">
<a class="action-button btn disabled waves-effect waves-light" data-action="build-request"><i class="nopaque-icons left">K</i>Build</a> <div class="action-switch switch center-align" data-action="toggle-is-public">
<a class="btn disabled export-corpus-trigger waves-effect waves-light" href="{{ url_for('corpora.export_corpus', corpus_id=corpus.id) }}"><i class="material-icons left">import_export</i>Export</a> <span class="share"></span>
<a class="action-button btn red waves-effect waves-light" data-action="delete-request"><i class="material-icons left">delete</i>Delete</a> <label>
<input class="corpus-is-public" {% if corpus.is_public %}checked{% endif %} type="checkbox">
<span class="lever"></span>
public
</label>
</div>
</div>
<div class="right-align">
<a class="btn corpus-analyse-trigger disabled waves-effect waves-light" href="{{ url_for('corpora.analyse_corpus', corpus_id=corpus.id) }}"><i class="material-icons left">search</i>Analyze</a>
<a class="action-button btn disabled waves-effect waves-light" data-action="build-request"><i class="nopaque-icons left">K</i>Build</a>
<a class="btn disabled export-corpus-trigger waves-effect waves-light" href="{{ url_for('corpora.export_corpus', corpus_id=corpus.id) }}"><i class="material-icons left">import_export</i>Export</a>
<a class="action-button btn red waves-effect waves-light" data-action="delete-request"><i class="material-icons left">delete</i>Delete</a>
</div>
</div> </div>
</div> </div>
</div> </div>
@ -95,6 +100,7 @@
</div> </div>
</div> </div>
{% endif %} {% endif %}
<div class="col s12"> <div class="col s12">
<div class="card"> <div class="card">
<div class="card-content"> <div class="card-content">

View File

@ -24,6 +24,6 @@ pyScss
python-dotenv python-dotenv
pyyaml pyyaml
redis redis
SQLAlchemy==1.4.46 SQLAlchemy==1.4.45
tqdm tqdm
wtforms[email] wtforms[email]