From f1ab50b2830678998b08b966d010d10e608884d1 Mon Sep 17 00:00:00 2001 From: Patrick Jentsch Date: Tue, 28 Jan 2020 10:08:09 +0100 Subject: [PATCH] Change event names --- app/events.py | 33 ++++++++++++++------------------ app/static/js/nopaque.js | 18 ++++++++--------- app/templates/admin/user.html.j2 | 2 +- 3 files changed, 24 insertions(+), 29 deletions(-) diff --git a/app/events.py b/app/events.py index c05ea980..c292ce7a 100644 --- a/app/events.py +++ b/app/events.py @@ -35,47 +35,42 @@ def disconnect(): connected_sessions.remove(request.sid) -@socketio.on('subscribe_user_ressources') +@socketio.on('user_ressources_init') @login_required def subscribe_user_ressources(): - socketio.start_background_task(user_ressource_subscription_handler, + socketio.start_background_task(user_ressource_session_handler, current_app._get_current_object(), current_user.id, request.sid) -@socketio.on('subscribe_foreign_user_ressources') +@socketio.on('foreign_user_ressources_init') @login_required @admin_required def subscribe_foreign_user_ressources(user_id): - socketio.start_background_task(user_ressource_subscription_handler, + socketio.start_background_task(user_ressource_session_handler, current_app._get_current_object(), user_id, request.sid, True) -def user_ressource_subscription_handler(app, user_id, session_id, - foreign=False): +def user_ressource_session_handler(app, user_id, session_id, foreign=False): ''' ' Sends initial corpus and job lists to the client. Afterwards it checks ' every 3 seconds if changes to the initial values appeared. If changes are ' detected, a RFC 6902 compliant JSON patch gets send. ' - ' NOTE: The initial values are send as a init-* events. - ' The JSON patches are send as update-* events. - ' > where '*' is either 'corpora' or 'jobs' + ' NOTE: The initial values are send as a init events. + ' The JSON patches are send as update events. ''' - init_events = {'corpora': 'init-foreign-corpora' if foreign - else 'init-corpora', - 'jobs': 'init-foreign-jobs' if foreign else 'init-jobs'} - update_events = {'corpora': 'update-foreign-corpora' if foreign - else 'update-corpora', - 'jobs': 'update-foreign-jobs' if foreign - else 'update-jobs'} + init_events = {'corpora': 'foreign_corpora_init' if foreign + else 'corpora_init', + 'jobs': 'foreign_jobs_init' if foreign else 'jobs_init'} + update_events = {'corpora': 'foreign_corpora_update' if foreign + else 'corpora_update', + 'jobs': 'foreign_jobs_update' if foreign + else 'jobs_update'} with app.app_context(): # Gather current values from database. user = User.query.get(user_id) - if user is None: - ''' TODO: Handle this ''' - return corpora = {corpus.id: corpus.to_dict() for corpus in user.corpora} jobs = {job.id: job.to_dict() for job in user.jobs} # Send initial values to the user. diff --git a/app/static/js/nopaque.js b/app/static/js/nopaque.js index 0dbbe6db..ed5fb865 100644 --- a/app/static/js/nopaque.js +++ b/app/static/js/nopaque.js @@ -108,19 +108,19 @@ nopaque["toast"] = function(message, color="") { // socket event handlers -nopaque.socket.on('init-corpora', function(msg) { +nopaque.socket.on('corpora_init', function(msg) { nopaque.corpora = JSON.parse(msg); for (let subscriber of nopaque.corporaSubscribers) {subscriber._init(nopaque.corpora);} }); -nopaque.socket.on('init-jobs', function(msg) { +nopaque.socket.on('jobs_init', function(msg) { nopaque.jobs = JSON.parse(msg); for (let subscriber of nopaque.jobsSubscribers) {subscriber._init(nopaque.jobs);} }); -nopaque.socket.on('update-corpora', function(msg) { +nopaque.socket.on('corpora_update', function(msg) { var patch; patch = JSON.parse(msg); @@ -129,7 +129,7 @@ nopaque.socket.on('update-corpora', function(msg) { }); -nopaque.socket.on('update-jobs', function(msg) { +nopaque.socket.on('jobs_update', function(msg) { var patch; patch = JSON.parse(msg); @@ -138,19 +138,19 @@ nopaque.socket.on('update-jobs', function(msg) { }); -nopaque.socket.on('init-foreign-corpora', function(msg) { +nopaque.socket.on('foreign_corpora_init', function(msg) { nopaque.foreignCorpora = JSON.parse(msg); for (let subscriber of nopaque.foreignCorporaSubscribers) {subscriber._init(nopaque.foreignCorpora);} }); -nopaque.socket.on('init-foreign-jobs', function(msg) { +nopaque.socket.on('foreign_jobs_init', function(msg) { nopaque.foreignJobs = JSON.parse(msg); for (let subscriber of nopaque.foreignJobsSubscribers) {subscriber._init(nopaque.foreignJobs);} }); -nopaque.socket.on('update-foreign-corpora', function(msg) { +nopaque.socket.on('foreign_corpora_update', function(msg) { var patch; patch = JSON.parse(msg); @@ -159,7 +159,7 @@ nopaque.socket.on('update-foreign-corpora', function(msg) { }); -nopaque.socket.on('update-foreign-jobs', function(msg) { +nopaque.socket.on('foreign_jobs_update', function(msg) { var patch; patch = JSON.parse(msg); @@ -175,5 +175,5 @@ document.addEventListener("DOMContentLoaded", function() { {"alignment": "right", "constrainWidth": false, "coverTrigger": false}); nopaque.forms.init(); nopaque.navigation.init(); - nopaque.socket.emit("subscribe_user_ressources"); + nopaque.socket.emit("user_ressources_init"); }); diff --git a/app/templates/admin/user.html.j2 b/app/templates/admin/user.html.j2 index 8a3fd10c..7786dbf2 100644 --- a/app/templates/admin/user.html.j2 +++ b/app/templates/admin/user.html.j2 @@ -102,6 +102,6 @@ {% endblock %}