2021-08-20 12:41:40 +00:00
|
|
|
from datetime import datetime
|
2021-08-18 13:11:11 +00:00
|
|
|
from . import db, socketio
|
2021-08-20 12:41:40 +00:00
|
|
|
from .models import Corpus, CorpusFile, Job, JobInput, JobResult
|
2021-08-18 13:11:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# SQLAlchemy event handlers #
|
|
|
|
###############################################################################
|
2021-08-20 12:41:40 +00:00
|
|
|
@db.event.listens_for(Corpus, 'after_delete')
|
|
|
|
@db.event.listens_for(CorpusFile, 'after_delete')
|
2021-08-18 13:11:11 +00:00
|
|
|
@db.event.listens_for(Job, 'after_delete')
|
2021-08-20 12:41:40 +00:00
|
|
|
@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)
|
2021-08-18 13:11:11 +00:00
|
|
|
socketio.emit(event, jsonpatch, room=room)
|
|
|
|
|
2021-08-20 12:41:40 +00:00
|
|
|
@db.event.listens_for(Corpus, 'after_insert')
|
|
|
|
@db.event.listens_for(CorpusFile, 'after_insert')
|
|
|
|
@db.event.listens_for(Job, 'after_insert')
|
2021-08-18 13:11:11 +00:00
|
|
|
@db.event.listens_for(JobInput, 'after_insert')
|
2021-08-20 12:41:40 +00:00
|
|
|
@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)
|
2021-08-18 13:11:11 +00:00
|
|
|
jsonpatch = [
|
|
|
|
{
|
|
|
|
'op': 'add',
|
2021-08-20 12:41:40 +00:00
|
|
|
'path': path,
|
|
|
|
'value': ressource.to_dict(include_relationships=False)
|
2021-08-18 13:11:11 +00:00
|
|
|
}
|
|
|
|
]
|
2021-08-20 12:41:40 +00:00
|
|
|
room = 'user_{}'.format(user_id)
|
2021-08-18 13:11:11 +00:00
|
|
|
socketio.emit(event, jsonpatch, room=room)
|
|
|
|
|
2021-08-20 12:41:40 +00:00
|
|
|
@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')
|
2021-08-18 13:11:11 +00:00
|
|
|
@db.event.listens_for(JobResult, 'after_update')
|
2021-08-20 12:41:40 +00:00
|
|
|
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)
|
2021-08-18 13:11:11 +00:00
|
|
|
jsonpatch = []
|
2021-08-20 12:41:40 +00:00
|
|
|
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
|
2021-08-18 13:11:11 +00:00
|
|
|
history = attr.load_history()
|
|
|
|
if not history.has_changes():
|
|
|
|
continue
|
|
|
|
new_value = history.added[0]
|
2021-08-20 12:41:40 +00:00
|
|
|
# DateTime attributes must be converted to a timestamp
|
|
|
|
if isinstance(new_value, datetime):
|
|
|
|
new_value = new_value.timestamp()
|
2021-08-18 13:11:11 +00:00
|
|
|
jsonpatch.append(
|
|
|
|
{
|
|
|
|
'op': 'replace',
|
2021-08-20 12:41:40 +00:00
|
|
|
'path': '{}/{}'.format(base_path, attr.key),
|
2021-08-18 13:11:11 +00:00
|
|
|
'value': new_value
|
|
|
|
}
|
|
|
|
)
|
|
|
|
if jsonpatch:
|
2021-08-20 12:41:40 +00:00
|
|
|
event = 'user_{}_patch'.format(user_id)
|
|
|
|
room = 'user_{}'.format(user_id)
|
2021-08-18 13:11:11 +00:00
|
|
|
socketio.emit(event, jsonpatch, room=room)
|