mirror of
https://github.com/fastapi-practices/fastapi_best_architecture.git
synced 2025-08-18 15:00:46 +08:00

* WIP: add rbac authorization * Perform pre-commit fixes * add rbac route whitelist * add init test data user role associations * Restore database table id naming to fix generic crud base * Add database section value uniqueness settings * Update the test directory to tests * Update route_name file name to health_check * Split user auth and user action interfaces * Fix conflict between merge and current branch * Add pymysql dependencies * Fix RBAC authentication method * Add the select serialisation tool * Fix missing return messages due to global exception handler slicing * Update the user interface with associated relationships * Add items to be completed * Perform pre-commit fixes * Add pre-made routers * Paging data return structure optimisation * Split user auth and user interface tests * Fix user register test data structure error * Fix duplicate named test classes
49 lines
976 B
Python
49 lines
976 B
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
from decimal import Decimal
|
|
|
|
from sqlalchemy.sql import Select
|
|
|
|
|
|
def select_to_dict(obj: Select) -> dict:
|
|
"""
|
|
Serialize SQLAlchemy Select to dict
|
|
|
|
:param obj:
|
|
:return:
|
|
"""
|
|
obj_dict = {}
|
|
for column in obj.__table__.columns.keys():
|
|
val = getattr(obj, column)
|
|
if isinstance(val, Decimal):
|
|
val = float(val)
|
|
obj_dict[column] = val
|
|
return obj_dict
|
|
|
|
|
|
def select_to_list(obj: list) -> list:
|
|
"""
|
|
Serialize SQLAlchemy Select to list
|
|
|
|
:param obj:
|
|
:return:
|
|
"""
|
|
ret_list = []
|
|
for _ in obj:
|
|
ret_dict = select_to_dict(_)
|
|
ret_list.append(ret_dict)
|
|
return ret_list
|
|
|
|
|
|
def select_to_json(obj: Select) -> dict:
|
|
"""
|
|
Serialize SQLAlchemy Select to json
|
|
|
|
:param obj:
|
|
:return:
|
|
"""
|
|
obj_dict = obj.__dict__
|
|
if '_sa_instance_state' in obj_dict:
|
|
del obj_dict['_sa_instance_state']
|
|
return obj_dict
|