Use enums where appropriate. This commit includes new migrations that are NOT compatible with older nopaque instances

This commit is contained in:
Patrick Jentsch
2022-02-08 12:26:20 +01:00
parent fe938c0ca2
commit df6ab3991c
110 changed files with 1389 additions and 2301 deletions

View File

@ -1,9 +1,8 @@
from app import hashids
from app import hashids, socketio
from app.decorators import socketio_login_required
from app.models import User
from flask_login import current_user
from flask_socketio import join_room
from .. import socketio
from ..decorators import socketio_login_required
from ..models import User
###############################################################################

View File

@ -1,8 +1,15 @@
from app import db, mail, socketio
from app.email import create_message
from app.models import (
Corpus,
CorpusFile,
Job,
JobInput,
JobResult,
JobStatus,
JobStatusMailNotificationLevel
)
from datetime import datetime
from flask import current_app
from .. import db, mail, socketio
from ..email import create_message
from ..models import Corpus, CorpusFile, Job, JobInput, JobResult, QueryResult
###############################################################################
@ -13,7 +20,6 @@ from ..models import Corpus, CorpusFile, Job, JobInput, JobResult, QueryResult
@db.event.listens_for(Job, 'after_delete')
@db.event.listens_for(JobInput, 'after_delete')
@db.event.listens_for(JobResult, 'after_delete')
@db.event.listens_for(QueryResult, 'after_delete')
def ressource_after_delete(mapper, connection, ressource):
jsonpatch = [{'op': 'remove', 'path': ressource.jsonpatch_path}]
room = f'users.{ressource.user_hashid}'
@ -25,14 +31,10 @@ def ressource_after_delete(mapper, connection, ressource):
@db.event.listens_for(Job, 'after_insert')
@db.event.listens_for(JobInput, 'after_insert')
@db.event.listens_for(JobResult, 'after_insert')
@db.event.listens_for(QueryResult, 'after_insert')
def ressource_after_insert_handler(mapper, connection, ressource):
value = ressource.to_dict(backrefs=False, relationships=False)
if isinstance(ressource, Job):
value['inputs'] = {}
value['results'] = {}
elif isinstance(ressource, Corpus):
value['files'] = {}
for relationship in mapper.relationships:
value[relationship.key] = {}
jsonpatch = [
{'op': 'add', 'path': ressource.jsonpatch_path, 'value': value}
]
@ -45,35 +47,43 @@ def ressource_after_insert_handler(mapper, connection, ressource):
@db.event.listens_for(Job, 'after_update')
@db.event.listens_for(JobInput, 'after_update')
@db.event.listens_for(JobResult, 'after_update')
@db.event.listens_for(QueryResult, 'after_update')
def ressource_after_update_handler(mapper, connection, ressource):
jsonpatch = []
for attr in db.inspect(ressource).attrs:
# We don't want to handle changes in relationship fields
# TODO: Find a way to handle this without a hardcoded list
if attr.key in ['files', 'inputs', 'results']:
# 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():
continue
new_value = history.added[0]
# In order to be JSON serializable, DateTime attributes must be
# converted to a string
if isinstance(new_value, datetime):
new_value = new_value.isoformat() + 'Z'
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
else:
attr_name = attr.key
value = history.added[0]
jsonpatch.append(
{
'op': 'replace',
'path': f'{ressource.jsonpatch_path}/{attr.key}',
'value': new_value
'path': f'{ressource.jsonpatch_path}/{attr_name}',
'value': value
}
)
# Job status update notification if it changed and wanted by the user
if isinstance(ressource, Job) and attr.key == 'status':
if ressource.user.setting_job_status_mail_notifications == 'none': # noqa
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_notifications == 'end' # noqa
and ressource.status not in ['complete', 'failed']):
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(