mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-21 04:46:26 +00:00
本次提交包含多项优化和修复: 1. 修复CRUD初始化参数传递、搜索参数处理逻辑 2. 更新环境配置中的大模型相关参数 3. 重构部分服务方法命名,统一代码风格 4. 新增多个枚举类型,补充模型关联关系和加载选项 5. 优化查询参数类实现,完善字段校验逻辑 6. 调整Pydantic模型字段注释和类型定义 7. 简化并移除冗余的CRUD方法实现 8. 新增超级管理员权限装饰器 9. 修复邮件日志模型的租户关联和字段定义
83 lines
2.9 KiB
Python
83 lines
2.9 KiB
Python
import re
|
||
from dataclasses import dataclass
|
||
|
||
from fastapi import Query
|
||
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
||
|
||
from app.common.enums import QueueEnum
|
||
from app.core.base_params import BaseQueryParam, TenantByQueryParam, UserByQueryParam
|
||
from app.core.base_schema import BaseSchema, TenantBySchema, UserBySchema
|
||
|
||
|
||
class ParamsCreateSchema(BaseModel):
|
||
"""
|
||
参数创建模型
|
||
"""
|
||
|
||
config_name: str = Field(..., min_length=1, max_length=64, description="参数名称")
|
||
config_key: str = Field(..., min_length=1, max_length=500, description="参数键名(小写字母开头,仅允许字母数字_.-)")
|
||
config_value: str | None = Field(default=None, max_length=500, description="参数键值")
|
||
config_type: bool = Field(default=False, description="是否系统内置(True:是 False:否)")
|
||
status: int = Field(default=0, ge=0, le=1, description="状态(0:正常 1:停用)")
|
||
description: str | None = Field(default=None, max_length=500, description="参数描述")
|
||
|
||
@field_validator("config_key")
|
||
@classmethod
|
||
def _validate_config_key(cls, v: str) -> str:
|
||
"""校验参数键名:小写字母开头,仅含字母/数字/_ . -"""
|
||
v = v.strip().lower()
|
||
if not re.match(r"^[a-z][a-z0-9_.-]*$", v):
|
||
raise ValueError("参数键名必须以小写字母开头,仅允许小写字母、数字、_ . -")
|
||
return v
|
||
|
||
@field_validator("status")
|
||
@classmethod
|
||
def _validate_status(cls, v: int) -> int:
|
||
"""校验状态:仅支持 0(正常) 或 1(停用)"""
|
||
if v not in {0, 1}:
|
||
raise ValueError("状态仅支持 0(正常) 或 1(停用)")
|
||
return v
|
||
|
||
|
||
class ParamsUpdateSchema(ParamsCreateSchema):
|
||
"""
|
||
参数更新模型
|
||
"""
|
||
|
||
|
||
class ParamsOutSchema(ParamsCreateSchema, BaseSchema, UserBySchema, TenantBySchema):
|
||
"""
|
||
参数响应模型
|
||
"""
|
||
|
||
model_config = ConfigDict(from_attributes=True)
|
||
|
||
|
||
@dataclass
|
||
class ParamsQueryParam(BaseQueryParam, UserByQueryParam, TenantByQueryParam):
|
||
"""
|
||
参数管理查询参数
|
||
|
||
支持:
|
||
- 时间范围(BaseQueryParam)
|
||
- 创建人/更新人筛选(UserByQueryParam)
|
||
- 租户筛选(TenantByQueryParam)
|
||
- 业务字段:参数名称、参数键名、是否系统内置
|
||
"""
|
||
|
||
def __init__(
|
||
self,
|
||
config_name: str | None = Query(None, description="参数名称"),
|
||
config_key: str | None = Query(None, description="参数键名"),
|
||
config_type: bool | None = Query(None, description="是否系统内置(True:是 False:否)"),
|
||
*args,
|
||
**kwargs,
|
||
) -> None:
|
||
super().__init__(*args, **kwargs)
|
||
if config_name:
|
||
self.config_name = (QueueEnum.like.value, config_name)
|
||
if config_key:
|
||
self.config_key = (QueueEnum.like.value, config_key)
|
||
if config_type is not None:
|
||
self.config_type = (QueueEnum.eq.value, config_type)
|