refactor: P2 core+common 建设(应用工厂/扩展/工具层迁入 src/)
【结构】 - src/core/:database/response/jwt/password/security/redis_client/mail/scheduler - src/common/:base_model/base_db/forms(base_form+fields)/constants/utils - src/models_registry.py:模型聚合器(Flask-Migrate 全量表识别) 【绞杀者 shim】extensions/、utils/、application/constants|forms|models 打兼容转发 【兼容修复】应用工厂抽取至 application/factory.py(P2 前置,P1 已完成) 【基线修复】application/models(25) + middleware(7) 原被嵌套 .gitignore(*) 误排除出版本库,已补入(base_model/base_db 现为 shim,原版在 src/common/) 【登记既有缺陷】 1. flask db CLI 因 .env FLASK_VERSION 与 --version 冲突报错 → Makefile 已加 FLASK_SKIP_DOTENV=1 2. 本机 Redis 未设密码但 REDIS_AUTH=True,/captcha 报 AUTH 错误(环境问题) 【验收】22 模型表全量识别;src/ flake8 零告警;pytest 5 passed;应用启动; /login 表单校验、/user/page 鉴权拦截冒烟正常 Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude
parent
92130ef070
commit
658ee6d596
+4
-86
@@ -1,90 +1,8 @@
|
||||
"""
|
||||
模型工具模块
|
||||
兼容 shim(绞杀者迁移期间保留)
|
||||
=============================================================
|
||||
|
||||
提供SQLAlchemy模型对象的序列化工具:
|
||||
- model_to_dict: 将单个模型对象转换为字典
|
||||
- models_to_list: 将模型对象列表转换为字典列表
|
||||
- format_datetime: 格式化datetime对象
|
||||
模型工具已迁移至 src.common.utils.model_utils。
|
||||
本文件仅作兼容转发,最终在 P7(切尾)阶段删除。
|
||||
"""
|
||||
|
||||
import logging
|
||||
from datetime import datetime
|
||||
|
||||
from application.constants.common import SERIALIZE_EXCLUDE_FIELDS
|
||||
from utils.case_converter import convert_camel_case
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def format_datetime(dt, format_str='%Y-%m-%d %H:%M:%S'):
|
||||
"""
|
||||
格式化日期时间
|
||||
|
||||
Args:
|
||||
dt: datetime对象
|
||||
format_str: 日期时间格式字符串,默认为 '%Y-%m-%d %H:%M:%S'
|
||||
|
||||
Returns:
|
||||
格式化后的日期字符串,如果输入为None则返回None
|
||||
"""
|
||||
return dt.strftime(format_str) if dt else None
|
||||
|
||||
|
||||
def model_to_dict(model, exclude=None, convert_case=True):
|
||||
"""
|
||||
将SQLAlchemy模型对象转换为字典
|
||||
|
||||
Args:
|
||||
model: SQLAlchemy模型对象
|
||||
exclude: 需要排除的字段列表,如 ['id', 'is_delete'](使用下划线命名)
|
||||
convert_case: 是否将字段名转换为驼峰命名,默认True
|
||||
|
||||
Returns:
|
||||
转换后的字典对象
|
||||
"""
|
||||
if not model:
|
||||
return None
|
||||
|
||||
result = {}
|
||||
for column in model.__table__.columns:
|
||||
field_name = column.name
|
||||
value = getattr(model, field_name)
|
||||
|
||||
if exclude and field_name in exclude:
|
||||
continue
|
||||
|
||||
if field_name in SERIALIZE_EXCLUDE_FIELDS:
|
||||
continue
|
||||
|
||||
# 所有 datetime 类型统一格式化
|
||||
if isinstance(value, datetime):
|
||||
value = value.strftime('%Y-%m-%d %H:%M:%S') if value else None
|
||||
|
||||
if convert_case:
|
||||
field_name = convert_camel_case(field_name)
|
||||
|
||||
result[field_name] = value
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def models_to_list(models, exclude=None, convert_case=True):
|
||||
"""
|
||||
将SQLAlchemy模型对象列表转换为字典列表
|
||||
|
||||
Args:
|
||||
models: SQLAlchemy模型对象列表
|
||||
exclude: 需要排除的字段列表
|
||||
convert_case: 是否将字段名转换为驼峰命名
|
||||
|
||||
Returns:
|
||||
字典列表
|
||||
"""
|
||||
if not models:
|
||||
return []
|
||||
|
||||
return [
|
||||
model_to_dict(model, exclude, convert_case)
|
||||
for model in models
|
||||
]
|
||||
from src.common.utils.model_utils import * # noqa: F401,F403
|
||||
|
||||
Reference in New Issue
Block a user