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

* simplify crud method naming * update get_user_list to get_select * add sign in logs * Perform pre-commit fix * Encapsulated request ip address resolution * Delete login log records for uncertain exceptions * Add login log deletion interface * Add login logging to background tasks * update the user agent parse
54 lines
1.0 KiB
Python
54 lines
1.0 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
from decimal import Decimal
|
|
from typing import Any, TypeVar
|
|
|
|
from sqlalchemy import Row, RowMapping
|
|
|
|
RowData = Row | RowMapping | Any
|
|
|
|
R = TypeVar('R', bound=RowData)
|
|
|
|
|
|
def select_to_dict(obj: R) -> 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[R]) -> 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: R) -> 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
|