Rework notification system

This commit is contained in:
Patrick Jentsch 2020-04-27 15:22:20 +02:00
parent b30382e605
commit c67c033aec
6 changed files with 32 additions and 28 deletions

View File

@ -26,13 +26,12 @@ def add_corpus():
try: try:
os.makedirs(dir) os.makedirs(dir)
except OSError: except OSError:
flash('[ERROR]: Could not add corpus!') flash('[ERROR]: Could not add corpus!', 'corpus')
corpus.delete() corpus.delete()
else: else:
corpus_url = url_for('corpora.corpus', corpus_id=corpus.id) url = url_for('corpora.corpus', corpus_id=corpus.id)
flash('<i class="left material-icons">book</i>' flash('[<a href="{}">{}</a>] added'.format(url, corpus.title),
'[<a href="{}">{}</a>] added'.format(corpus_url, 'corpus')
corpus.title))
return redirect(url_for('corpora.corpus', corpus_id=corpus.id)) return redirect(url_for('corpora.corpus', corpus_id=corpus.id))
return render_template('corpora/add_corpus.html.j2', return render_template('corpora/add_corpus.html.j2',
add_corpus_form=add_corpus_form, add_corpus_form=add_corpus_form,
@ -82,7 +81,7 @@ def delete_corpus(corpus_id):
if not (corpus.creator == current_user or current_user.is_administrator()): if not (corpus.creator == current_user or current_user.is_administrator()):
abort(403) abort(403)
tasks.delete_corpus(corpus_id) tasks.delete_corpus(corpus_id)
flash('Corpus deleted!') flash('Corpus deleted!', 'corpus')
return redirect(url_for('main.dashboard')) return redirect(url_for('main.dashboard'))
@ -121,7 +120,7 @@ def add_corpus_file(corpus_id):
db.session.add(corpus_file) db.session.add(corpus_file)
corpus.status = 'unprepared' corpus.status = 'unprepared'
db.session.commit() db.session.commit()
flash('Corpus file added!') flash('Corpus file added!', 'corpus')
return make_response( return make_response(
{'redirect_url': url_for('corpora.corpus', corpus_id=corpus.id)}, {'redirect_url': url_for('corpora.corpus', corpus_id=corpus.id)},
201) 201)
@ -141,7 +140,7 @@ def delete_corpus_file(corpus_id, corpus_file_id):
or current_user.is_administrator()): or current_user.is_administrator()):
abort(403) abort(403)
tasks.delete_corpus_file(corpus_file_id) tasks.delete_corpus_file(corpus_file_id)
flash('Corpus file deleted!') flash('Corpus file deleted!', 'corpus')
return redirect(url_for('corpora.corpus', corpus_id=corpus_id)) return redirect(url_for('corpora.corpus', corpus_id=corpus_id))
@ -188,7 +187,7 @@ def edit_corpus_file(corpus_id, corpus_file_id):
corpus_file.title = edit_corpus_file_form.title.data corpus_file.title = edit_corpus_file_form.title.data
corpus.status = 'unprepared' corpus.status = 'unprepared'
db.session.commit() db.session.commit()
flash('Corpus file edited!') flash('Corpus file edited!', 'corpus')
return redirect(url_for('corpora.corpus', corpus_id=corpus_id)) return redirect(url_for('corpora.corpus', corpus_id=corpus_id))
# If no form is submitted or valid, fill out fields with current values # If no form is submitted or valid, fill out fields with current values
edit_corpus_file_form.address.data = corpus_file.address edit_corpus_file_form.address.data = corpus_file.address
@ -217,7 +216,7 @@ def prepare_corpus(corpus_id):
abort(403) abort(403)
if corpus.files.all(): if corpus.files.all():
tasks.build_corpus(corpus_id) tasks.build_corpus(corpus_id)
flash('Corpus gets build now.') flash('Corpus gets build now.', 'corpus')
else: else:
flash('Can not build corpus, please add corpus file(s).') flash('Can not build corpus, please add corpus file(s).', 'corpus')
return redirect(url_for('corpora.corpus', corpus_id=corpus_id)) return redirect(url_for('corpora.corpus', corpus_id=corpus_id))

View File

@ -23,7 +23,7 @@ def delete_job(job_id):
if not (job.creator == current_user or current_user.is_administrator()): if not (job.creator == current_user or current_user.is_administrator()):
abort(403) abort(403)
tasks.delete_job(job_id) tasks.delete_job(job_id)
flash('Job has been deleted!') flash('Job has been deleted!', 'job')
return redirect(url_for('main.dashboard')) return redirect(url_for('main.dashboard'))

View File

@ -61,7 +61,7 @@ def service(service):
os.makedirs(absolut_dir) os.makedirs(absolut_dir)
except OSError: except OSError:
job.delete() job.delete()
flash('Internal Server Error') flash('Internal Server Error', 'job')
return make_response({'redirect_url': url_for('services.service', return make_response({'redirect_url': url_for('services.service',
service=service)}, service=service)},
500) 500)
@ -74,9 +74,8 @@ def service(service):
db.session.add(job_input) db.session.add(job_input)
job.status = 'submitted' job.status = 'submitted'
db.session.commit() db.session.commit()
job_url = url_for('jobs.job', job_id=job.id) url = url_for('jobs.job', job_id=job.id)
flash('<i class="left material-icons">work</i>' flash('[<a href="{}">{}</a>] added'.format(url, job.title), 'job')
'[<a href="{}">{}</a>] added'.format(job_url, job.title))
return make_response( return make_response(
{'redirect_url': url_for('jobs.job', job_id=job.id)}, 201) {'redirect_url': url_for('jobs.job', job_id=job.id)}, 201)
return render_template('services/{}.html.j2'.format(service), return render_template('services/{}.html.j2'.format(service),

View File

@ -44,7 +44,7 @@ class CorpusAnalysisClient {
} }
} else { } else {
errorText = `Error ${response.payload.code} - ${response.payload.msg}`; errorText = `Error ${response.payload.code} - ${response.payload.msg}`;
nopaque.flash("error", errorText); nopaque.flash(errorText, "error");
if (this.displays.query.errorContainer != undefined) { if (this.displays.query.errorContainer != undefined) {
this.displays.query.errorContainer.innerHTML = `<p class="red-text">`+ this.displays.query.errorContainer.innerHTML = `<p class="red-text">`+
`<i class="material-icons tiny">error</i> ${errorText}</p>`; `<i class="material-icons tiny">error</i> ${errorText}</p>`;

View File

@ -58,7 +58,7 @@ nopaque.socket.init = function() {
pathArray = operation.path.split("/").slice(1); pathArray = operation.path.split("/").slice(1);
if (operation.op === "replace" && pathArray[1] === "status") { if (operation.op === "replace" && pathArray[1] === "status") {
if (nopaque.user.settings.jobStatusSiteNotifications === "end" && !["complete", "failed"].includes(operation.value)) {continue;} if (nopaque.user.settings.jobStatusSiteNotifications === "end" && !["complete", "failed"].includes(operation.value)) {continue;}
nopaque.flash(`<i class="left material-icons">work</i>[<a href="/jobs/${pathArray[0]}">${nopaque.jobs[pathArray[0]].title}</a>] New status: ${operation.value}`); nopaque.flash(`[<a href="/jobs/${pathArray[0]}">${nopaque.jobs[pathArray[0]].title}</a>] New status: ${operation.value}`, "job");
} }
} }
} }
@ -184,23 +184,28 @@ nopaque.flash = function() {
message = arguments[0]; message = arguments[0];
break; break;
case 2: case 2:
category = arguments[0]; message = arguments[0];
message = arguments[1]; category = arguments[1];
break; break;
default: default:
console.error("Usage: nopaque.flash(message) or nopaque.flash(category, message)") console.error("Usage: nopaque.flash(message) or nopaque.flash(message, category)")
} }
switch (category) { switch (category) {
case "corpus":
message = `<i class="left material-icons">book</i>${message}`;
break;
case "error": case "error":
classes = "red"; message = `<i class="left material-icons red-text">error</i>${message}`;
break;
case "job":
message = `<i class="left material-icons">work</i>${message}`;
break; break;
default: default:
classes = ""; message = `<i class="left material-icons">notifications</i>${message}`;
} }
toast = M.toast({classes: classes, toast = M.toast({html: `<span>${message}</span>
html: `<span>${message}</span>
<button data-action="close" class="btn-flat toast-action white-text"> <button data-action="close" class="btn-flat toast-action white-text">
<i class="material-icons">close</i> <i class="material-icons">close</i>
</button>`}); </button>`});
@ -225,7 +230,8 @@ document.addEventListener("DOMContentLoaded", function() {
nopaque.Forms.init(); nopaque.Forms.init();
nopaque.Navigation.init(); nopaque.Navigation.init();
while (nopaque.flashedMessages.length) { while (nopaque.flashedMessages.length) {
nopaque.flash(...nopaque.flashedMessages.shift()); flashedMessage = nopaque.flashedMessages.shift();
nopaque.flash(flashedMessage[1], flashedMessage[0]);
} }
if (nopaque.user.isAuthenticated) { if (nopaque.user.isAuthenticated) {
if (nopaque.user.settings.darkMode) { if (nopaque.user.settings.darkMode) {

View File

@ -378,7 +378,7 @@ class ResultsList extends List {
if (expertModeSwitchElement.checked) { if (expertModeSwitchElement.checked) {
this.expertModeOn("query-display"); // page holds new result rows, so add new tooltips this.expertModeOn("query-display"); // page holds new result rows, so add new tooltips
} }
nopaque.flash("Updated matches per page.") nopaque.flash("Updated matches per page.", "corpus")
} catch (e) { } catch (e) {
// console.log(e); // console.log(e);
// console.log("resultsList has no results right now."); // console.log("resultsList has no results right now.");
@ -394,7 +394,7 @@ class ResultsList extends List {
let rc; let rc;
try { try {
if (event.type === "change") { if (event.type === "change") {
nopaque.flash("Updated context per match!"); nopaque.flash("Updated context per match!", "corpus");
} }
} catch (e) { } catch (e) {
} finally { } finally {