2021-08-20 12:41:40 +00:00
|
|
|
from datetime import datetime
|
2021-11-30 15:22:16 +00:00
|
|
|
from flask import current_app
|
2021-09-10 14:25:32 +00:00
|
|
|
from .. import db, mail, socketio
|
|
|
|
from ..email import create_message
|
|
|
|
from ..models import Corpus, CorpusFile, Job, JobInput, JobResult, QueryResult
|
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')
|
2021-09-10 14:25:32 +00:00
|
|
|
@db.event.listens_for(QueryResult, 'after_delete')
|
2021-08-20 12:41:40 +00:00
|
|
|
def ressource_after_delete(mapper, connection, ressource):
|
2021-09-10 14:25:32 +00:00
|
|
|
jsonpatch = [{'op': 'remove', 'path': ressource.jsonpatch_path}]
|
2021-11-30 15:22:16 +00:00
|
|
|
room = f'users.{ressource.user_hashid}'
|
|
|
|
socketio.emit('users.patch', jsonpatch, room=room)
|
2021-08-18 13:11:11 +00:00
|
|
|
|
2021-09-10 14:25:32 +00:00
|
|
|
|
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')
|
2021-09-10 14:25:32 +00:00
|
|
|
@db.event.listens_for(QueryResult, 'after_insert')
|
2021-08-20 12:41:40 +00:00
|
|
|
def ressource_after_insert_handler(mapper, connection, ressource):
|
2021-11-30 15:22:16 +00:00
|
|
|
value = ressource.to_dict(backrefs=False, relationships=False)
|
2021-08-18 13:11:11 +00:00
|
|
|
jsonpatch = [
|
2021-11-30 15:22:16 +00:00
|
|
|
{'op': 'add', 'path': ressource.jsonpatch_path, 'value': value}
|
2021-08-18 13:11:11 +00:00
|
|
|
]
|
2021-11-30 15:22:16 +00:00
|
|
|
room = f'users.{ressource.user_hashid}'
|
|
|
|
socketio.emit('users.patch', jsonpatch, room=room)
|
2021-08-18 13:11:11 +00:00
|
|
|
|
2021-09-10 14:25:32 +00:00
|
|
|
|
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-09-10 14:25:32 +00:00
|
|
|
@db.event.listens_for(QueryResult, 'after_update')
|
2021-08-20 12:41:40 +00:00
|
|
|
def ressource_after_update_handler(mapper, connection, ressource):
|
2021-08-18 13:11:11 +00:00
|
|
|
jsonpatch = []
|
2021-08-20 12:41:40 +00:00
|
|
|
for attr in db.inspect(ressource).attrs:
|
2021-09-10 14:25:32 +00:00
|
|
|
# We don't want to handle changes in relationship fields
|
|
|
|
# TODO: Find a way to handle this without a hardcoded list
|
2021-08-20 12:41:40 +00:00
|
|
|
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-09-10 14:25:32 +00:00
|
|
|
# In order to be JSON serializable, DateTime attributes must be
|
|
|
|
# converted to a string
|
2021-08-20 12:41:40 +00:00
|
|
|
if isinstance(new_value, datetime):
|
2021-09-14 10:51:49 +00:00
|
|
|
new_value = new_value.isoformat() + 'Z'
|
2021-08-18 13:11:11 +00:00
|
|
|
jsonpatch.append(
|
|
|
|
{
|
|
|
|
'op': 'replace',
|
2021-11-30 15:22:16 +00:00
|
|
|
'path': f'{ressource.jsonpatch_path}/{attr.key}',
|
2021-08-18 13:11:11 +00:00
|
|
|
'value': new_value
|
|
|
|
}
|
|
|
|
)
|
2021-09-10 14:25:32 +00:00
|
|
|
# Job status update notification if it changed and wanted by the user
|
|
|
|
if isinstance(ressource, Job) and attr.key == 'status':
|
2021-11-30 15:22:16 +00:00
|
|
|
if ressource.user.setting_job_status_mail_notifications == 'none': # noqa
|
2021-09-10 14:25:32 +00:00
|
|
|
pass
|
2021-11-30 15:22:16 +00:00
|
|
|
elif (ressource.user.setting_job_status_mail_notifications == 'end' # noqa
|
2021-09-10 14:25:32 +00:00
|
|
|
and ressource.status not in ['complete', 'failed']):
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
msg = create_message(
|
2021-11-30 15:22:16 +00:00
|
|
|
ressource.user.email,
|
|
|
|
f'Status update for your Job "{ressource.title}"',
|
2021-09-10 14:25:32 +00:00
|
|
|
'tasks/email/notification',
|
|
|
|
job=ressource
|
|
|
|
)
|
|
|
|
mail.send(msg)
|
2021-08-18 13:11:11 +00:00
|
|
|
if jsonpatch:
|
2021-11-30 15:22:16 +00:00
|
|
|
room = f'users.{ressource.user_hashid}'
|
|
|
|
socketio.emit('users.patch', jsonpatch, room=room)
|