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

@ -1,6 +1,6 @@
from app import db, hashids from app import db, hashids
from app.decorators import admin_required from app.decorators import admin_required
from app.models import JobStatusMailNotificationLevel, Role, User from app.models import Role, User, UserSettingJobStatusMailNotificationLevel
from app.settings import tasks as settings_tasks from app.settings import tasks as settings_tasks
from app.settings.forms import ( from app.settings.forms import (
EditGeneralSettingsForm, EditGeneralSettingsForm,
@ -102,7 +102,7 @@ def edit_user(user_id):
and edit_notification_settings_form.validate() and edit_notification_settings_form.validate()
): ):
user.setting_job_status_mail_notification_level = \ user.setting_job_status_mail_notification_level = \
JobStatusMailNotificationLevel[ UserSettingJobStatusMailNotificationLevel[
edit_notification_settings_form.job_status_mail_notification_level.data # noqa edit_notification_settings_form.job_status_mail_notification_level.data # noqa
] ]
db.session.commit() db.session.commit()

View File

@ -7,9 +7,10 @@ from app.models import (
JobInput, JobInput,
JobResult, JobResult,
JobStatus, JobStatus,
JobStatusMailNotificationLevel UserSettingJobStatusMailNotificationLevel
) )
from datetime import datetime 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') @db.event.listens_for(JobResult, 'after_insert')
def ressource_after_insert_handler(mapper, connection, ressource): def ressource_after_insert_handler(mapper, connection, ressource):
value = ressource.to_dict(backrefs=False, relationships=False) value = ressource.to_dict(backrefs=False, relationships=False)
for relationship in mapper.relationships: for attr in mapper.relationships:
value[relationship.key] = {} value[attr.key] = {}
jsonpatch = [ jsonpatch = [
{'op': 'add', 'path': ressource.jsonpatch_path, 'value': value} {'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): def ressource_after_update_handler(mapper, connection, ressource):
jsonpatch = [] jsonpatch = []
for attr in db.inspect(ressource).attrs: for attr in db.inspect(ressource).attrs:
# Don't handle changes in relationship fields
if attr.key in mapper.relationships: if attr.key in mapper.relationships:
continue continue
# Check if their are changes for the current field if not attr.load_history().has_changes():
history = attr.load_history()
if not history.has_changes():
continue continue
if isinstance(history.added[0], datetime): if isinstance(attr.value, datetime):
# In order to be JSON serializable, DateTime attributes must be value = attr.value.isoformat() + 'Z'
# converted to a string elif isinstance(attr.value, Enum):
attr_name = attr.key value = attr.value.name
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: else:
attr_name = attr.key value = attr.value
value = history.added[0]
jsonpatch.append( jsonpatch.append(
{ {
'op': 'replace', 'op': 'replace',
'path': f'{ressource.jsonpatch_path}/{attr_name}', 'path': f'{ressource.jsonpatch_path}/{attr.key}',
'value': value 'value': value
} }
) )
# Job status update notification if it changed and wanted by the user if isinstance(ressource, Job) and attr.key == 'status':
if isinstance(ressource, Job) and attr_name == 'status': _job_status_email_handler(ressource)
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 jsonpatch: if jsonpatch:
room = f'users.{ressource.user_hashid}' room = f'users.{ressource.user_hashid}'
socketio.emit('users.patch', jsonpatch, room=room) 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)

View File

@ -17,33 +17,23 @@ import xml.etree.ElementTree as ET
import yaml import yaml
class CorpusStatus(IntEnum): class IntEnumProxy(db.TypeDecorator):
UNPREPARED = 1 impl = db.Integer
SUBMITTED = 2
QUEUED = 3
BUILDING = 4
BUILT = 5
FAILED = 6
STARTING_ANALYSIS_SESSION = 7
RUNNING_ANALYSIS_SESSION = 8
CANCELING_ANALYSIS_SESSION = 9
def __init__(self, enumtype, *args, **kwargs):
super().__init__(*args, **kwargs)
self._enumtype = enumtype
class JobStatus(IntEnum): def process_bind_param(self, value, dialect):
INITIALIZING = 1 if isinstance(value, self._enumtype):
SUBMITTED = 2 return value.value
QUEUED = 3 elif isinstance(value, int):
RUNNING = 4 return value
CANCELING = 5 else:
CANCELED = 6 return TypeError()
COMPLETED = 7
FAILED = 8
def process_result_value(self, value, dialect):
class JobStatusMailNotificationLevel(IntEnum): return self._enumtype(value)
NONE = 1
END = 2
ALL = 3
class Permission(IntEnum): class Permission(IntEnum):
@ -143,6 +133,12 @@ class Role(HashidMixin, db.Model):
db.session.commit() db.session.commit()
class UserSettingJobStatusMailNotificationLevel(IntEnum):
NONE = 1
END = 2
ALL = 3
class User(HashidMixin, UserMixin, db.Model): class User(HashidMixin, UserMixin, db.Model):
__tablename__ = 'users' __tablename__ = 'users'
# Primary key # Primary key
@ -159,10 +155,9 @@ class User(HashidMixin, UserMixin, db.Model):
token_expiration = db.Column(db.DateTime) token_expiration = db.Column(db.DateTime)
username = db.Column(db.String(64), unique=True, index=True) username = db.Column(db.String(64), unique=True, index=True)
setting_dark_mode = db.Column(db.Boolean, default=False) setting_dark_mode = db.Column(db.Boolean, default=False)
setting_job_status_mail_notification_level_enum_value = db.Column( setting_job_status_mail_notification_level = db.Column(
'setting_job_status_mail_notification_level', IntEnumProxy(UserSettingJobStatusMailNotificationLevel),
db.Integer, default=UserSettingJobStatusMailNotificationLevel.END
default=2
) )
# Backrefs: role: Role # Backrefs: role: Role
# Relationships # Relationships
@ -214,19 +209,6 @@ class User(HashidMixin, UserMixin, db.Model):
return os.path.join( return os.path.join(
current_app.config.get('NOPAQUE_DATA_DIR'), 'users', str(self.id)) current_app.config.get('NOPAQUE_DATA_DIR'), 'users', str(self.id))
@property
def setting_job_status_mail_notification_level(self):
return JobStatusMailNotificationLevel(
self.setting_job_status_mail_notification_level_enum_value
)
@setting_job_status_mail_notification_level.setter
def setting_job_status_mail_notification_level(self, enum_member):
if not isinstance(enum_member, JobStatusMailNotificationLevel):
return TypeError()
self.setting_job_status_mail_notification_level_enum_value = \
enum_member.value
def can(self, permission): def can(self, permission):
return self.role.has_permission(permission) return self.role.has_permission(permission)
@ -553,6 +535,17 @@ class JobResult(FileMixin, HashidMixin, db.Model):
return self.job.user_id return self.job.user_id
class JobStatus(IntEnum):
INITIALIZING = 1
SUBMITTED = 2
QUEUED = 3
RUNNING = 4
CANCELING = 5
CANCELED = 6
COMPLETED = 7
FAILED = 8
class Job(HashidMixin, db.Model): class Job(HashidMixin, db.Model):
''' '''
Class to define Jobs. Class to define Jobs.
@ -573,7 +566,10 @@ class Job(HashidMixin, db.Model):
''' '''
service_args = db.Column(db.String(255)) service_args = db.Column(db.String(255))
service_version = db.Column(db.String(16)) service_version = db.Column(db.String(16))
status_enum_value = db.Column('status', db.Integer, default=1) status = db.Column(
IntEnumProxy(JobStatus),
default=JobStatus.INITIALIZING
)
title = db.Column(db.String(32)) title = db.Column(db.String(32))
# Backrefs: user: User # Backrefs: user: User
# Relationships # Relationships
@ -601,16 +597,6 @@ class Job(HashidMixin, db.Model):
def path(self): def path(self):
return os.path.join(self.user.path, 'jobs', str(self.id)) return os.path.join(self.user.path, 'jobs', str(self.id))
@property
def status(self):
return JobStatus(self.status_enum_value)
@status.setter
def status(self, enum_member):
if not isinstance(enum_member, JobStatus):
return TypeError()
self.status_enum_value = enum_member.value
@property @property
def url(self): def url(self):
return url_for('jobs.job', job_id=self.id) return url_for('jobs.job', job_id=self.id)
@ -780,6 +766,18 @@ class CorpusFile(FileMixin, HashidMixin, db.Model):
return dict_corpus_file return dict_corpus_file
class CorpusStatus(IntEnum):
UNPREPARED = 1
SUBMITTED = 2
QUEUED = 3
BUILDING = 4
BUILT = 5
FAILED = 6
STARTING_ANALYSIS_SESSION = 7
RUNNING_ANALYSIS_SESSION = 8
CANCELING_ANALYSIS_SESSION = 9
class Corpus(HashidMixin, db.Model): class Corpus(HashidMixin, db.Model):
''' '''
Class to define a corpus. Class to define a corpus.
@ -793,7 +791,10 @@ class Corpus(HashidMixin, db.Model):
creation_date = db.Column(db.DateTime(), default=datetime.utcnow) creation_date = db.Column(db.DateTime(), default=datetime.utcnow)
description = db.Column(db.String(255)) description = db.Column(db.String(255))
last_edited_date = db.Column(db.DateTime(), default=datetime.utcnow) last_edited_date = db.Column(db.DateTime(), default=datetime.utcnow)
status_enum_value = db.Column('status', db.Integer, default=1) status = db.Column(
IntEnumProxy(CorpusStatus),
default=CorpusStatus.UNPREPARED
)
title = db.Column(db.String(32)) title = db.Column(db.String(32))
num_analysis_sessions = db.Column(db.Integer, default=0) num_analysis_sessions = db.Column(db.Integer, default=0)
num_tokens = db.Column(db.Integer, default=0) num_tokens = db.Column(db.Integer, default=0)
@ -824,16 +825,6 @@ class Corpus(HashidMixin, db.Model):
def path(self): def path(self):
return os.path.join(self.user.path, 'corpora', str(self.id)) return os.path.join(self.user.path, 'corpora', str(self.id))
@property
def status(self):
return CorpusStatus(self.status_enum_value)
@status.setter
def status(self, enum_member):
if not isinstance(enum_member, CorpusStatus):
return TypeError()
self.status_enum_value = enum_member.value
@property @property
def url(self): def url(self):
return url_for('corpora.corpus', corpus_id=self.id) return url_for('corpora.corpus', corpus_id=self.id)

View File

@ -1,5 +1,5 @@
from app.auth import USERNAME_REGEX from app.auth import USERNAME_REGEX
from app.models import JobStatusMailNotificationLevel, User from app.models import User, UserSettingJobStatusMailNotificationLevel
from flask_wtf import FlaskForm from flask_wtf import FlaskForm
from wtforms import ( from wtforms import (
BooleanField, BooleanField,
@ -96,5 +96,5 @@ class EditNotificationSettingsForm(FlaskForm):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.job_status_mail_notification_level.choices += [ self.job_status_mail_notification_level.choices += [
(enum_member.name, enum_member.name.capitalize()) (enum_member.name, enum_member.name.capitalize())
for enum_member in JobStatusMailNotificationLevel for enum_member in UserSettingJobStatusMailNotificationLevel
] ]

View File

@ -8,7 +8,7 @@ from .forms import (
EditNotificationSettingsForm EditNotificationSettingsForm
) )
from .. import db from .. import db
from ..models import JobStatusMailNotificationLevel from ..models import UserSettingJobStatusMailNotificationLevel
@bp.route('', methods=['GET', 'POST']) @bp.route('', methods=['GET', 'POST'])
@ -57,7 +57,7 @@ def index():
and edit_notification_settings_form.validate() and edit_notification_settings_form.validate()
): ):
current_user.setting_job_status_mail_notification_level = \ current_user.setting_job_status_mail_notification_level = \
JobStatusMailNotificationLevel[ UserSettingJobStatusMailNotificationLevel[
edit_notification_settings_form.job_status_mail_notification_level.data # noqa edit_notification_settings_form.job_status_mail_notification_level.data # noqa
] ]
db.session.commit() db.session.commit()