sipro blueprint

This commit is contained in:
2023-11-25 01:44:52 +03:00
parent 560a0248a5
commit 98a9a2cd61
4 changed files with 66 additions and 0 deletions

1
app.py
View File

@@ -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:

11
auxiliary.py Normal file
View File

@@ -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

View File

@@ -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

53
routes/sipro.py Normal file
View File

@@ -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