Restructure code

This commit is contained in:
Patrick Jentsch 2022-04-25 11:32:10 +02:00
parent fc1389e8b1
commit 10b5254e82
6 changed files with 53 additions and 50 deletions

View File

@ -43,8 +43,8 @@ def create_app(config: Config = Config) -> Flask:
message_queue=app.config['NOPAQUE_SOCKETIO_MESSAGE_QUEUE_URI'] message_queue=app.config['NOPAQUE_SOCKETIO_MESSAGE_QUEUE_URI']
) )
from .events import socketio as socketio_events from app import socketio_event_listeners
from .events import sqlalchemy as sqlalchemy_events from app import sqlalchemy_event_listeners
from .admin import bp as admin_blueprint from .admin import bp as admin_blueprint
app.register_blueprint(admin_blueprint, url_prefix='/admin') app.register_blueprint(admin_blueprint, url_prefix='/admin')

View File

@ -16,8 +16,8 @@ def create_message(
f'{subject_prefix} {subject}', f'{subject_prefix} {subject}',
recipients=[recipient] recipients=[recipient]
) )
msg.body: Text = render_template(f'{template}.txt.j2', **kwargs) msg.body = render_template(f'{template}.txt.j2', **kwargs)
msg.html: Text = render_template(f'{template}.html.j2', **kwargs) msg.html = render_template(f'{template}.html.j2', **kwargs)
return msg return msg

View File

@ -1,4 +1,6 @@
from app import db, login
from app.converters.vrt import normalize_vrt_file from app.converters.vrt import normalize_vrt_file
from app.sqlalchemy_custom_types import ContainerColumn, IntEnumColumn
from datetime import datetime, timedelta from datetime import datetime, timedelta
from enum import IntEnum from enum import IntEnum
from flask import current_app, url_for from flask import current_app, url_for
@ -8,7 +10,6 @@ from itsdangerous import BadSignature, TimedJSONWebSignatureSerializer
from time import sleep from time import sleep
from tqdm import tqdm from tqdm import tqdm
from werkzeug.security import generate_password_hash, check_password_hash from werkzeug.security import generate_password_hash, check_password_hash
from . import db, login
import base64 import base64
import json import json
import os import os
@ -22,45 +23,10 @@ TRANSKRIBUS_HTR_MODELS = \
json.loads(requests.get('https://transkribus.eu/TrpServer/rest/models/text').content)['trpModelMetadata'] # noqa json.loads(requests.get('https://transkribus.eu/TrpServer/rest/models/text').content)['trpModelMetadata'] # noqa
class IntEnumColumn(db.TypeDecorator):
impl = db.Integer
def __init__(self, enum_type, *args, **kwargs):
super().__init__(*args, **kwargs)
self.enum_type = enum_type
def process_bind_param(self, value, dialect):
if isinstance(value, self.enum_type) and isinstance(value.value, int):
return value.value
elif isinstance(value, int):
return self.enum_type(value).value
else:
return TypeError()
def process_result_value(self, value, dialect):
return self.enum_type(value)
class ContainerColumn(db.TypeDecorator):
impl = db.String
def __init__(self, container_type, *args, **kwargs):
super().__init__(*args, **kwargs)
self.container_type = container_type
def process_bind_param(self, value, dialect):
if isinstance(value, self.container_type):
return json.dumps(value)
elif isinstance(value, str) and isinstance(json.loads(value), self.container_type): # noqa
return value
else:
return TypeError()
def process_result_value(self, value, dialect):
return json.loads(value)
class FileMixin: class FileMixin:
'''
Mixin for db.Model classes. All file related models should use this.
'''
creation_date = db.Column(db.DateTime, default=datetime.utcnow) creation_date = db.Column(db.DateTime, default=datetime.utcnow)
filename = db.Column(db.String(255)) filename = 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)
@ -127,7 +93,7 @@ class Role(HashidMixin, db.Model):
} }
if relationships: if relationships:
dict_role['users'] = { dict_role['users'] = {
x.to_dict(backrefs=False, relationships=True) x.hashid: x.to_dict(backrefs=False, relationships=True)
for x in self.users for x in self.users
} }
return dict_role return dict_role

View File

@ -5,9 +5,6 @@ from flask_login import current_user
from flask_socketio import join_room from flask_socketio import join_room
###############################################################################
# Socket.IO event handlers #
###############################################################################
@socketio.on('users.user.get') @socketio.on('users.user.get')
@socketio_login_required @socketio_login_required
def users_user_get(user_hashid): def users_user_get(user_hashid):

View File

@ -0,0 +1,43 @@
from app import db
import json
class IntEnumColumn(db.TypeDecorator):
impl = db.Integer
def __init__(self, enum_type, *args, **kwargs):
super().__init__(*args, **kwargs)
self.enum_type = enum_type
def process_bind_param(self, value, dialect):
if isinstance(value, self.enum_type) and isinstance(value.value, int):
return value.value
elif isinstance(value, int):
return self.enum_type(value).value
else:
return TypeError()
def process_result_value(self, value, dialect):
return self.enum_type(value)
class ContainerColumn(db.TypeDecorator):
impl = db.String
def __init__(self, container_type, *args, **kwargs):
super().__init__(*args, **kwargs)
self.container_type = container_type
def process_bind_param(self, value, dialect):
if isinstance(value, self.container_type):
return json.dumps(value)
elif (
isinstance(value, str)
and isinstance(json.loads(value), self.container_type)
):
return value
else:
return TypeError()
def process_result_value(self, value, dialect):
return json.loads(value)

View File

@ -13,9 +13,6 @@ from datetime import datetime
from enum import Enum from enum import Enum
###############################################################################
# SQLAlchemy event handlers #
###############################################################################
@db.event.listens_for(Corpus, 'after_delete') @db.event.listens_for(Corpus, 'after_delete')
@db.event.listens_for(CorpusFile, 'after_delete') @db.event.listens_for(CorpusFile, 'after_delete')
@db.event.listens_for(Job, 'after_delete') @db.event.listens_for(Job, 'after_delete')