mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-22 13:05:18 +00:00
本次提交包含多项优化和修复: 1. 修复CRUD初始化参数传递、搜索参数处理逻辑 2. 更新环境配置中的大模型相关参数 3. 重构部分服务方法命名,统一代码风格 4. 新增多个枚举类型,补充模型关联关系和加载选项 5. 优化查询参数类实现,完善字段校验逻辑 6. 调整Pydantic模型字段注释和类型定义 7. 简化并移除冗余的CRUD方法实现 8. 新增超级管理员权限装饰器 9. 修复邮件日志模型的租户关联和字段定义
101 lines
3.7 KiB
Python
101 lines
3.7 KiB
Python
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
|
||
from app.core.base_schema import BaseSchema
|
||
|
||
|
||
class PluginCreateSchema(BaseModel):
|
||
"""新增插件"""
|
||
|
||
name: str = Field(..., min_length=1, max_length=100, description="插件名称")
|
||
code: str = Field(..., min_length=1, max_length=50, description="插件编码(如 module_xxx)")
|
||
version: str = Field(default="1.0.0", max_length=20, description="版本号")
|
||
author: str | None = Field(default=None, max_length=100, description="作者")
|
||
icon: str | None = Field(default=None, max_length=500, description="图标URL")
|
||
category: str = Field(default="tool", max_length=20, description="分类(tool/ai/monitor/business)")
|
||
price: int = Field(default=0, ge=0, description="价格(分,0=免费)")
|
||
menu_path: str | None = Field(default=None, max_length=200, description="菜单路径")
|
||
permission_prefix: str | None = Field(default=None, max_length=100, description="权限前缀")
|
||
dependencies: str | None = Field(default=None, description="依赖插件编码(JSON数组)")
|
||
sort: int = Field(default=0, ge=0, description="排序")
|
||
status: int = Field(default=0, ge=0, le=1, description="状态(0:启动 1:停用)")
|
||
description: str | None = Field(default=None, max_length=255, description="插件描述")
|
||
|
||
@field_validator("category")
|
||
@classmethod
|
||
def _validate_category(cls, v: str) -> str:
|
||
allowed = {"tool", "ai", "monitor", "business"}
|
||
if v not in allowed:
|
||
raise ValueError(f"插件分类仅支持 tool、ai、monitor、business,当前值: {v}")
|
||
return v
|
||
|
||
@field_validator("version")
|
||
@classmethod
|
||
def _validate_version(cls, v: str) -> str:
|
||
import re
|
||
|
||
if not re.match(r"^\d+\.\d+\.\d+$", v):
|
||
raise ValueError("版本号格式需为 x.y.z(如 1.0.0)")
|
||
return v
|
||
|
||
@field_validator("code")
|
||
@classmethod
|
||
def _validate_code(cls, v: str) -> str:
|
||
v = v.strip()
|
||
if not v:
|
||
raise ValueError("插件编码不能为空")
|
||
return v
|
||
|
||
|
||
class PluginUpdateSchema(PluginCreateSchema):
|
||
"""更新插件 — 所有字段可选"""
|
||
|
||
name: str | None = Field(default=None, max_length=100, description="插件名称") # type: ignore[assignment]
|
||
code: str | None = Field(default=None, max_length=50, description="插件编码") # type: ignore[assignment]
|
||
status: int | None = Field(default=None, ge=0, le=1, description="状态(0:启动 1:停用)")
|
||
|
||
@field_validator("status")
|
||
@classmethod
|
||
def _validate_status(cls, v: int | None) -> int | None:
|
||
if v is None:
|
||
return v
|
||
if v not in {0, 1}:
|
||
raise ValueError("状态仅支持 0(正常) 或 1(禁用)")
|
||
return v
|
||
|
||
|
||
class PluginOutSchema(PluginCreateSchema, BaseSchema):
|
||
"""插件响应"""
|
||
|
||
model_config = ConfigDict(from_attributes=True)
|
||
|
||
installed: bool = False # 当前租户是否已安装
|
||
|
||
|
||
class PluginInstallSchema(BaseModel):
|
||
"""安装/卸载插件"""
|
||
|
||
plugin_id: int = Field(..., description="插件ID")
|
||
|
||
|
||
@dataclass
|
||
class PluginQueryParam(BaseQueryParam):
|
||
"""插件查询参数"""
|
||
|
||
def __init__(
|
||
self,
|
||
name: str | None = Query(None, description="插件名称"),
|
||
category: str | None = Query(None, description="插件分类(tool/ai/monitor/business)"),
|
||
*args,
|
||
**kwargs,
|
||
) -> None:
|
||
super().__init__(*args, **kwargs)
|
||
if name:
|
||
self.name = (QueueEnum.like.value, name)
|
||
if category:
|
||
self.category = (QueueEnum.eq.value, category)
|