mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2025-06-23 06:20:34 +00:00
Move event handlers to a dedicated event directory
This commit is contained in:
75
app/events/socketio.py
Normal file
75
app/events/socketio.py
Normal file
@ -0,0 +1,75 @@
|
||||
from flask import request
|
||||
from flask_login import current_user
|
||||
from flask_socketio import join_room, leave_room
|
||||
from .. import socketio
|
||||
from ..decorators import socketio_login_required
|
||||
from ..models import User
|
||||
|
||||
|
||||
'''
|
||||
' A list containing session ids of connected Socket.IO sessions, to keep track
|
||||
' of all connected sessions, which can be used to determine the runtimes of
|
||||
' associated background tasks.
|
||||
'''
|
||||
sessions = []
|
||||
|
||||
|
||||
###############################################################################
|
||||
# Socket.IO event handlers #
|
||||
###############################################################################
|
||||
@socketio.on('connect')
|
||||
@socketio_login_required
|
||||
def socketio_connect():
|
||||
'''
|
||||
' The Socket.IO module creates a session id (sid) for each request.
|
||||
' On connect the sid is saved in the connected sessions list.
|
||||
'''
|
||||
sessions.append(request.sid)
|
||||
|
||||
|
||||
@socketio.on('disconnect')
|
||||
def socketio_disconnect():
|
||||
'''
|
||||
' On disconnect the session id gets removed from the connected sessions
|
||||
' list.
|
||||
'''
|
||||
try:
|
||||
sessions.remove(request.sid)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
|
||||
@socketio.on('start_user_session')
|
||||
@socketio_login_required
|
||||
def socketio_start_user_session(user_id):
|
||||
user = User.query.get(user_id)
|
||||
if user is None:
|
||||
response = {'code': 404, 'msg': 'Not found'}
|
||||
socketio.emit('start_user_session', response, room=request.sid)
|
||||
elif not (user == current_user or current_user.is_administrator):
|
||||
response = {'code': 403, 'msg': 'Forbidden'}
|
||||
socketio.emit('start_user_session', response, room=request.sid)
|
||||
else:
|
||||
response = {'code': 200, 'msg': 'OK'}
|
||||
socketio.emit('start_user_session', response, room=request.sid)
|
||||
socketio.emit('user_{}_init'.format(user.id), user.to_dict(),
|
||||
room=request.sid)
|
||||
room = 'user_{}'.format(user.id)
|
||||
join_room(room)
|
||||
|
||||
|
||||
@socketio.on('stop_user_session')
|
||||
@socketio_login_required
|
||||
def socketio_stop_user_session(user_id):
|
||||
user = User.query.get(user_id)
|
||||
if user is None:
|
||||
response = {'code': 404, 'msg': 'Not found'}
|
||||
socketio.emit('stop_user_session', response, room=request.sid)
|
||||
elif not (user == current_user or current_user.is_administrator):
|
||||
response = {'code': 403, 'msg': 'Forbidden'}
|
||||
socketio.emit('stop_user_session', response, room=request.sid)
|
||||
else:
|
||||
response = {'code': 200, 'msg': 'OK'}
|
||||
socketio.emit('stop_user_session', response, room=request.sid)
|
||||
room = 'user_{}'.format(user.id)
|
||||
leave_room(room)
|
112
app/events/sqlalchemy.py
Normal file
112
app/events/sqlalchemy.py
Normal file
@ -0,0 +1,112 @@
|
||||
from datetime import datetime
|
||||
from .. import db, socketio
|
||||
from ..models import Corpus, CorpusFile, Job, JobInput, JobResult
|
||||
|
||||
|
||||
###############################################################################
|
||||
# SQLAlchemy event handlers #
|
||||
###############################################################################
|
||||
@db.event.listens_for(Corpus, 'after_delete')
|
||||
@db.event.listens_for(CorpusFile, 'after_delete')
|
||||
@db.event.listens_for(Job, 'after_delete')
|
||||
@db.event.listens_for(JobInput, 'after_delete')
|
||||
@db.event.listens_for(JobResult, 'after_delete')
|
||||
def ressource_after_delete(mapper, connection, ressource):
|
||||
if isinstance(ressource, Corpus):
|
||||
user_id = ressource.creator.id
|
||||
path = '/corpora/{}'.format(ressource.id)
|
||||
elif isinstance(ressource, CorpusFile):
|
||||
user_id = ressource.corpus.creator.id
|
||||
path = '/corpora/{}/files/{}'.format(ressource.corpus.id, ressource.id)
|
||||
elif isinstance(ressource, Job):
|
||||
user_id = ressource.creator.id
|
||||
path = '/jobs/{}'.format(ressource.id)
|
||||
elif isinstance(ressource, JobInput):
|
||||
user_id = ressource.job.creator.id
|
||||
path = '/jobs/{}/inputs/{}'.format(ressource.job.id, ressource.id)
|
||||
elif isinstance(ressource, JobResult):
|
||||
user_id = ressource.job.creator.id
|
||||
path = '/jobs/{}/results/{}'.format(ressource.job.id, ressource.id)
|
||||
event = 'user_{}_patch'.format(user_id)
|
||||
jsonpatch = [{'op': 'remove', 'path': path}]
|
||||
room = 'user_{}'.format(user_id)
|
||||
socketio.emit(event, jsonpatch, room=room)
|
||||
|
||||
@db.event.listens_for(Corpus, 'after_insert')
|
||||
@db.event.listens_for(CorpusFile, 'after_insert')
|
||||
@db.event.listens_for(Job, 'after_insert')
|
||||
@db.event.listens_for(JobInput, 'after_insert')
|
||||
@db.event.listens_for(JobResult, 'after_insert')
|
||||
def ressource_after_insert_handler(mapper, connection, ressource):
|
||||
if isinstance(ressource, Corpus):
|
||||
user_id = ressource.creator.id
|
||||
path = '/corpora/{}'.format(ressource.id)
|
||||
elif isinstance(ressource, CorpusFile):
|
||||
user_id = ressource.corpus.creator.id
|
||||
path = '/corpora/{}/files/{}'.format(ressource.corpus.id, ressource.id)
|
||||
elif isinstance(ressource, Job):
|
||||
user_id = ressource.creator.id
|
||||
path = '/jobs/{}'.format(ressource.id)
|
||||
elif isinstance(ressource, JobInput):
|
||||
user_id = ressource.job.creator.id
|
||||
path = '/jobs/{}/inputs/{}'.format(ressource.job.id, ressource.id)
|
||||
elif isinstance(ressource, JobResult):
|
||||
user_id = ressource.job.creator.id
|
||||
path = '/jobs/{}/results/{}'.format(ressource.job.id, ressource.id)
|
||||
event = 'user_{}_patch'.format(user_id)
|
||||
jsonpatch = [
|
||||
{
|
||||
'op': 'add',
|
||||
'path': path,
|
||||
'value': ressource.to_dict(include_relationships=False)
|
||||
}
|
||||
]
|
||||
room = 'user_{}'.format(user_id)
|
||||
socketio.emit(event, jsonpatch, room=room)
|
||||
|
||||
@db.event.listens_for(Corpus, 'after_update')
|
||||
@db.event.listens_for(CorpusFile, 'after_update')
|
||||
@db.event.listens_for(Job, 'after_update')
|
||||
@db.event.listens_for(JobInput, 'after_update')
|
||||
@db.event.listens_for(JobResult, 'after_update')
|
||||
def ressource_after_update_handler(mapper, connection, ressource):
|
||||
if isinstance(ressource, Corpus):
|
||||
user_id = ressource.creator.id
|
||||
base_path = '/corpora/{}'.format(ressource.id)
|
||||
elif isinstance(ressource, CorpusFile):
|
||||
user_id = ressource.corpus.creator.id
|
||||
base_path = '/corpora/{}/files/{}'.format(ressource.corpus.id,
|
||||
ressource.id)
|
||||
elif isinstance(ressource, Job):
|
||||
user_id = ressource.creator.id
|
||||
base_path = '/jobs/{}'.format(ressource.id)
|
||||
elif isinstance(ressource, JobInput):
|
||||
user_id = ressource.job.creator.id
|
||||
base_path = '/jobs/{}/inputs/{}'.format(ressource.job.id, ressource.id)
|
||||
elif isinstance(ressource, JobResult):
|
||||
user_id = ressource.job.creator.id
|
||||
base_path = '/jobs/{}/results/{}'.format(ressource.job.id,
|
||||
ressource.id)
|
||||
jsonpatch = []
|
||||
for attr in db.inspect(ressource).attrs:
|
||||
# We don't want to emit changes about relationship fields
|
||||
if attr.key in ['files', 'inputs', 'results']:
|
||||
continue
|
||||
history = attr.load_history()
|
||||
if not history.has_changes():
|
||||
continue
|
||||
new_value = history.added[0]
|
||||
# DateTime attributes must be converted to a timestamp
|
||||
if isinstance(new_value, datetime):
|
||||
new_value = new_value.timestamp()
|
||||
jsonpatch.append(
|
||||
{
|
||||
'op': 'replace',
|
||||
'path': '{}/{}'.format(base_path, attr.key),
|
||||
'value': new_value
|
||||
}
|
||||
)
|
||||
if jsonpatch:
|
||||
event = 'user_{}_patch'.format(user_id)
|
||||
room = 'user_{}'.format(user_id)
|
||||
socketio.emit(event, jsonpatch, room=room)
|
Reference in New Issue
Block a user