mirror of
				https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
				synced 2025-11-04 04:12:45 +00:00 
			
		
		
		
	Restructure code
This commit is contained in:
		@@ -43,8 +43,8 @@ def create_app(config: Config = Config) -> Flask:
 | 
			
		||||
        message_queue=app.config['NOPAQUE_SOCKETIO_MESSAGE_QUEUE_URI']
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    from .events import socketio as socketio_events
 | 
			
		||||
    from .events import sqlalchemy as sqlalchemy_events
 | 
			
		||||
    from app import socketio_event_listeners
 | 
			
		||||
    from app import sqlalchemy_event_listeners
 | 
			
		||||
 | 
			
		||||
    from .admin import bp as admin_blueprint
 | 
			
		||||
    app.register_blueprint(admin_blueprint, url_prefix='/admin')
 | 
			
		||||
 
 | 
			
		||||
@@ -16,8 +16,8 @@ def create_message(
 | 
			
		||||
        f'{subject_prefix} {subject}',
 | 
			
		||||
        recipients=[recipient]
 | 
			
		||||
    )
 | 
			
		||||
    msg.body: Text = render_template(f'{template}.txt.j2', **kwargs)
 | 
			
		||||
    msg.html: Text = render_template(f'{template}.html.j2', **kwargs)
 | 
			
		||||
    msg.body = render_template(f'{template}.txt.j2', **kwargs)
 | 
			
		||||
    msg.html = render_template(f'{template}.html.j2', **kwargs)
 | 
			
		||||
    return msg
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,6 @@
 | 
			
		||||
from app import db, login
 | 
			
		||||
from app.converters.vrt import normalize_vrt_file
 | 
			
		||||
from app.sqlalchemy_custom_types import ContainerColumn, IntEnumColumn
 | 
			
		||||
from datetime import datetime, timedelta
 | 
			
		||||
from enum import IntEnum
 | 
			
		||||
from flask import current_app, url_for
 | 
			
		||||
@@ -8,7 +10,6 @@ from itsdangerous import BadSignature, TimedJSONWebSignatureSerializer
 | 
			
		||||
from time import sleep
 | 
			
		||||
from tqdm import tqdm
 | 
			
		||||
from werkzeug.security import generate_password_hash, check_password_hash
 | 
			
		||||
from . import db, login
 | 
			
		||||
import base64
 | 
			
		||||
import json
 | 
			
		||||
import os
 | 
			
		||||
@@ -22,45 +23,10 @@ TRANSKRIBUS_HTR_MODELS = \
 | 
			
		||||
    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:
 | 
			
		||||
    '''
 | 
			
		||||
    Mixin for db.Model classes. All file related models should use this.
 | 
			
		||||
    '''
 | 
			
		||||
    creation_date = db.Column(db.DateTime, default=datetime.utcnow)
 | 
			
		||||
    filename = db.Column(db.String(255))
 | 
			
		||||
    last_edited_date = db.Column(db.DateTime, default=datetime.utcnow)
 | 
			
		||||
@@ -127,7 +93,7 @@ class Role(HashidMixin, db.Model):
 | 
			
		||||
        }
 | 
			
		||||
        if relationships:
 | 
			
		||||
            dict_role['users'] = {
 | 
			
		||||
                x.to_dict(backrefs=False, relationships=True)
 | 
			
		||||
                x.hashid: x.to_dict(backrefs=False, relationships=True)
 | 
			
		||||
                for x in self.users
 | 
			
		||||
            }
 | 
			
		||||
        return dict_role
 | 
			
		||||
 
 | 
			
		||||
@@ -5,9 +5,6 @@ from flask_login import current_user
 | 
			
		||||
from flask_socketio import join_room
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
###############################################################################
 | 
			
		||||
# Socket.IO event handlers                                                    #
 | 
			
		||||
###############################################################################
 | 
			
		||||
@socketio.on('users.user.get')
 | 
			
		||||
@socketio_login_required
 | 
			
		||||
def users_user_get(user_hashid):
 | 
			
		||||
							
								
								
									
										43
									
								
								app/sqlalchemy_custom_types.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								app/sqlalchemy_custom_types.py
									
									
									
									
									
										Normal 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)
 | 
			
		||||
@@ -13,9 +13,6 @@ from datetime import datetime
 | 
			
		||||
from enum import Enum
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
###############################################################################
 | 
			
		||||
# SQLAlchemy event handlers                                                   #
 | 
			
		||||
###############################################################################
 | 
			
		||||
@db.event.listens_for(Corpus, 'after_delete')
 | 
			
		||||
@db.event.listens_for(CorpusFile, 'after_delete')
 | 
			
		||||
@db.event.listens_for(Job, 'after_delete')
 | 
			
		||||
		Reference in New Issue
	
	Block a user