More generic implementation of fake enum db types

This commit is contained in:
Patrick Jentsch
2022-02-09 16:02:37 +01:00
parent 247ac801b0
commit 86d14f748f
5 changed files with 88 additions and 105 deletions

View File

@ -7,9 +7,10 @@ from app.models import (
JobInput,
JobResult,
JobStatus,
JobStatusMailNotificationLevel
UserSettingJobStatusMailNotificationLevel
)
from datetime import datetime
from enum import Enum
###############################################################################
@ -33,8 +34,8 @@ def ressource_after_delete(mapper, connection, ressource):
@db.event.listens_for(JobResult, 'after_insert')
def ressource_after_insert_handler(mapper, connection, ressource):
value = ressource.to_dict(backrefs=False, relationships=False)
for relationship in mapper.relationships:
value[relationship.key] = {}
for attr in mapper.relationships:
value[attr.key] = {}
jsonpatch = [
{'op': 'add', 'path': ressource.jsonpatch_path, 'value': value}
]
@ -50,49 +51,40 @@ def ressource_after_insert_handler(mapper, connection, ressource):
def ressource_after_update_handler(mapper, connection, ressource):
jsonpatch = []
for attr in db.inspect(ressource).attrs:
# Don't handle changes in relationship fields
if attr.key in mapper.relationships:
continue
# Check if their are changes for the current field
history = attr.load_history()
if not history.has_changes():
if not attr.load_history().has_changes():
continue
if isinstance(history.added[0], datetime):
# In order to be JSON serializable, DateTime attributes must be
# converted to a string
attr_name = attr.key
value = history.added[0].isoformat() + 'Z'
elif attr.key.endswith('_enum_value'):
# Handling fake enum attributes
attr_name = attr.key[:-11]
value = getattr(ressource, attr_name).name
if isinstance(attr.value, datetime):
value = attr.value.isoformat() + 'Z'
elif isinstance(attr.value, Enum):
value = attr.value.name
else:
attr_name = attr.key
value = history.added[0]
value = attr.value
jsonpatch.append(
{
'op': 'replace',
'path': f'{ressource.jsonpatch_path}/{attr_name}',
'path': f'{ressource.jsonpatch_path}/{attr.key}',
'value': value
}
)
# Job status update notification if it changed and wanted by the user
if isinstance(ressource, Job) and attr_name == 'status':
if ressource.user.setting_job_status_mail_notification_level == JobStatusMailNotificationLevel.NONE: # noqa
pass
elif (
ressource.user.setting_job_status_mail_notification_level == JobStatusMailNotificationLevel.END # noqa
and ressource.status not in [JobStatus.COMPLETED, JobStatus.FAILED] # noqa
):
pass
else:
msg = create_message(
ressource.user.email,
f'Status update for your Job "{ressource.title}"',
'tasks/email/notification',
job=ressource
)
mail.send(msg)
if isinstance(ressource, Job) and attr.key == 'status':
_job_status_email_handler(ressource)
if jsonpatch:
room = f'users.{ressource.user_hashid}'
socketio.emit('users.patch', jsonpatch, room=room)
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)