From 98a9a2cd61913a6c7030ba9c326003975b3eb6b4 Mon Sep 17 00:00:00 2001 From: fakz9 Date: Sat, 25 Nov 2023 01:44:52 +0300 Subject: [PATCH] sipro blueprint --- app.py | 1 + auxiliary.py | 11 ++++++++++ routes/__init__.py | 1 + routes/sipro.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+) create mode 100644 auxiliary.py create mode 100644 routes/sipro.py diff --git a/app.py b/app.py index e9ece98..f6e5154 100644 --- a/app.py +++ b/app.py @@ -30,6 +30,7 @@ blueprints = [ (routes.assembly_blueprint, '/assembly'), (routes.general_blueprint, '/general'), (routes.application_blueprint, '/application'), + (routes.sipro_blueprint, '/sipro'), ] for blueprint, url_prefix in blueprints: diff --git a/auxiliary.py b/auxiliary.py new file mode 100644 index 0000000..81a00b4 --- /dev/null +++ b/auxiliary.py @@ -0,0 +1,11 @@ +def to_nested_dict(row): + result = {} + for key, value in row._mapping.items(): + keys = key.split('.') + current_level = result + for part in keys[:-1]: + if part not in current_level: + current_level[part] = {} + current_level = current_level[part] + current_level[keys[-1]] = value + return result diff --git a/routes/__init__.py b/routes/__init__.py index 8771ebc..cdcdd1a 100644 --- a/routes/__init__.py +++ b/routes/__init__.py @@ -5,3 +5,4 @@ from routes.printing import printing_blueprint from routes.assembly import assembly_blueprint from routes.general import general_blueprint from routes.application import application_blueprint +from routes.sipro import sipro_blueprint diff --git a/routes/sipro.py b/routes/sipro.py new file mode 100644 index 0000000..83a6e78 --- /dev/null +++ b/routes/sipro.py @@ -0,0 +1,53 @@ +from enum import StrEnum + +from flask import Blueprint, request +from sqlalchemy import func + +import auxiliary +import database + +sipro_blueprint = Blueprint('sipro', __name__) + + +class ExpandParam(StrEnum): + USER = 'user' + + +@sipro_blueprint.before_request +def auth(): + API_KEY = '5D809ED08080B5F204443B31374BD6A5' + if request.headers.get('Authorization') != API_KEY: + return {'error': 'Unauthorized'}, 401 + + +@sipro_blueprint.post('/assemblyInfo') +def assembly_info(): + params: dict = request.json + expand_param = params.get('expand') + order_ids = params.get('orderIds') + expand_list = expand_param if expand_param else [] + datetime_format = 'YYYY-MM-DD HH24:MI:SS' + entity_list = [ + database.Assembly.id.label('id'), + database.Assembly.order_id.label('order_id'), + func.to_char(database.Assembly.created_at, datetime_format).label('created_at'), + func.to_char(database.Assembly.ended_at, datetime_format).label('ended_at'), + database.Assembly.is_active.label('is_active'), + database.Assembly.state.label('state') + ] + query = database.Assembly.query + for expand in expand_list: + match expand: + case ExpandParam.USER: + query = query.join(database.User) + entity_list.extend([ + database.User.id.label('user.id'), + database.User.login.label('user.login'), + ]) + + if order_ids: + query = query.filter(database.Assembly.order_id.in_(order_ids)) + query = query.with_entities(*entity_list) + result = query.all() + json_result = [auxiliary.to_nested_dict(row) for row in result] + return json_result