2022-02-08 11:26:20 +00:00
|
|
|
from app import db, mail, socketio
|
|
|
|
from app.email import create_message
|
|
|
|
from app.models import (
|
|
|
|
Corpus,
|
|
|
|
CorpusFile,
|
|
|
|
Job,
|
|
|
|
JobInput,
|
|
|
|
JobResult,
|
|
|
|
JobStatus,
|
2022-02-09 15:02:37 +00:00
|
|
|
UserSettingJobStatusMailNotificationLevel
|
2022-02-08 11:26:20 +00:00
|
|
|
)
|
2021-08-20 12:41:40 +00:00
|
|
|
from datetime import datetime
|
2022-02-09 15:02:37 +00:00
|
|
|
from enum import Enum
|
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):
|
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')
|
|
|
|
def ressource_after_insert_handler(mapper, connection, ressource):
|
2021-11-30 15:22:16 +00:00
|
|
|
value = ressource.to_dict(backrefs=False, relationships=False)
|
2022-02-09 15:02:37 +00:00
|
|
|
for attr in mapper.relationships:
|
|
|
|
value[attr.key] = {}
|
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-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:
|
2022-02-08 11:26:20 +00:00
|
|
|
if attr.key in mapper.relationships:
|
2021-08-20 12:41:40 +00:00
|
|
|
continue
|
2022-02-09 15:02:37 +00:00
|
|
|
if not attr.load_history().has_changes():
|
2021-08-18 13:11:11 +00:00
|
|
|
continue
|
2022-02-09 15:02:37 +00:00
|
|
|
if isinstance(attr.value, datetime):
|
|
|
|
value = attr.value.isoformat() + 'Z'
|
|
|
|
elif isinstance(attr.value, Enum):
|
|
|
|
value = attr.value.name
|
2022-02-08 11:26:20 +00:00
|
|
|
else:
|
2022-02-09 15:02:37 +00:00
|
|
|
value = attr.value
|
2021-08-18 13:11:11 +00:00
|
|
|
jsonpatch.append(
|
|
|
|
{
|
|
|
|
'op': 'replace',
|
2022-02-09 15:02:37 +00:00
|
|
|
'path': f'{ressource.jsonpatch_path}/{attr.key}',
|
2022-02-08 11:26:20 +00:00
|
|
|
'value': value
|
2021-08-18 13:11:11 +00:00
|
|
|
}
|
|
|
|
)
|
2022-02-09 15:02:37 +00:00
|
|
|
if isinstance(ressource, Job) and attr.key == 'status':
|
|
|
|
_job_status_email_handler(ressource)
|
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)
|
2022-02-09 15:02:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _job_status_email_handler(job):
|
|
|
|
if job.user.setting_job_status_mail_notification_level == UserSettingJobStatusMailNotificationLevel.NONE: # noqa
|
|
|
|
return
|
|
|
|
if job.user.setting_job_status_mail_notification_level == UserSettingJobStatusMailNotificationLevel.END: # noqa
|
|
|
|
if job.status not in [JobStatus.COMPLETED, JobStatus.FAILED]:
|
|
|
|
return
|
|
|
|
msg = create_message(
|
|
|
|
job.user.email,
|
|
|
|
f'Status update for your Job "{job.title}"',
|
|
|
|
'tasks/email/notification',
|
|
|
|
job=job
|
|
|
|
)
|
|
|
|
mail.send(msg)
|