mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-21 04:46:26 +00:00
style: 移除Python文件中的编码声明并优化代码格式
refactor: 重构前端组件和样式,添加AI助手功能 docs: 更新README文档,添加ruff代码检查说明 feat: 新增AI助手相关API和前端组件 chore: 更新.gitignore文件,添加ruff缓存配置 fix: 修复前端布局和设置相关的问题 perf: 优化代码结构和性能,移除冗余代码 test: 更新测试文件,移除编码声明 build: 更新依赖版本,调整requirements.txt
This commit is contained in:
@@ -1,22 +1,20 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import json
|
||||
from redis.asyncio.client import Redis
|
||||
|
||||
from fastapi import UploadFile
|
||||
from redis.asyncio.client import Redis
|
||||
|
||||
from app.api.v1.module_system.auth.schema import AuthSchema
|
||||
from app.common.enums import RedisInitKeyConfig
|
||||
from app.core.base_schema import UploadResponseSchema
|
||||
from app.core.database import async_db_session
|
||||
from app.core.exceptions import CustomException
|
||||
from app.core.logger import log
|
||||
from app.core.redis_crud import RedisCURD
|
||||
from app.utils.excel_util import ExcelUtil
|
||||
from app.utils.upload_util import UploadUtil
|
||||
from app.core.base_schema import UploadResponseSchema
|
||||
from app.core.exceptions import CustomException
|
||||
from app.core.logger import log
|
||||
|
||||
from ..auth.schema import AuthSchema
|
||||
from .schema import ParamsOutSchema, ParamsUpdateSchema, ParamsCreateSchema, ParamsQueryParam
|
||||
from .crud import ParamsCRUD
|
||||
from .schema import ParamsCreateSchema, ParamsOutSchema, ParamsQueryParam, ParamsUpdateSchema
|
||||
|
||||
|
||||
class ParamsService:
|
||||
@@ -27,26 +25,26 @@ class ParamsService:
|
||||
async def get_obj_detail_service(cls, auth: AuthSchema, id: int) -> dict:
|
||||
"""
|
||||
获取配置详情
|
||||
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
- id (int): 配置管理型ID
|
||||
|
||||
|
||||
返回:
|
||||
- dict: 配置管理型模型实例字典表示
|
||||
"""
|
||||
obj = await ParamsCRUD(auth).get_obj_by_id_crud(id=id)
|
||||
return ParamsOutSchema.model_validate(obj).model_dump()
|
||||
|
||||
|
||||
@classmethod
|
||||
async def get_obj_by_key_service(cls, auth: AuthSchema, config_key: str) -> dict:
|
||||
"""
|
||||
根据配置键获取配置详情
|
||||
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
- config_key (str): 配置管理型key
|
||||
|
||||
|
||||
返回:
|
||||
- Dict: 配置管理型模型实例字典表示
|
||||
"""
|
||||
@@ -54,16 +52,16 @@ class ParamsService:
|
||||
if not obj:
|
||||
raise CustomException(msg=f'配置键 {config_key} 不存在')
|
||||
return ParamsOutSchema.model_validate(obj).model_dump()
|
||||
|
||||
|
||||
@classmethod
|
||||
async def get_config_value_by_key_service(cls, auth: AuthSchema, config_key: str) -> str | None:
|
||||
"""
|
||||
根据配置键获取配置值
|
||||
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
- config_key (str): 配置管理型key
|
||||
|
||||
|
||||
返回:
|
||||
- str | None: 配置值字符串或None
|
||||
"""
|
||||
@@ -76,12 +74,12 @@ class ParamsService:
|
||||
async def get_obj_list_service(cls, auth: AuthSchema, search: ParamsQueryParam | None = None, order_by: list[dict] | None = None) -> list[dict]:
|
||||
"""
|
||||
获取配置管理型列表
|
||||
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
- search (ParamsQueryParam | None): 查询参数对象
|
||||
- order_by (list[dict] | None): 排序参数列表
|
||||
|
||||
|
||||
返回:
|
||||
- list[dict]: 配置管理型模型实例字典列表表示
|
||||
"""
|
||||
@@ -91,17 +89,17 @@ class ParamsService:
|
||||
else:
|
||||
obj_list = await ParamsCRUD(auth).get_obj_list_crud()
|
||||
return [ParamsOutSchema.model_validate(obj).model_dump() for obj in obj_list]
|
||||
|
||||
|
||||
@classmethod
|
||||
async def create_obj_service(cls, auth: AuthSchema, redis: Redis, data: ParamsCreateSchema) -> dict:
|
||||
"""
|
||||
创建配置管理型
|
||||
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
- redis (Redis): Redis 客户端实例
|
||||
- data (ParamsCreateSchema): 配置管理型创建模型
|
||||
|
||||
|
||||
返回:
|
||||
- dict: 新创建的配置管理型模型实例字典表示
|
||||
"""
|
||||
@@ -125,20 +123,20 @@ class ParamsService:
|
||||
except Exception as e:
|
||||
log.error(f"创建字典类型失败: {e}")
|
||||
raise CustomException(msg=f"创建字典类型失败 {e}")
|
||||
|
||||
|
||||
return new_obj_dict
|
||||
|
||||
|
||||
@classmethod
|
||||
async def update_obj_service(cls, auth: AuthSchema, redis: Redis, id:int, data: ParamsUpdateSchema) -> dict:
|
||||
async def update_obj_service(cls, auth: AuthSchema, redis: Redis, id: int, data: ParamsUpdateSchema) -> dict:
|
||||
"""
|
||||
更新配置管理型
|
||||
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
- redis (Redis): Redis 客户端实例
|
||||
- id (int): 配置管理型ID
|
||||
- data (ParamsUpdateSchema): 配置管理型更新模型
|
||||
|
||||
|
||||
返回:
|
||||
- Dict: 更新后的配置管理型模型实例字典表示
|
||||
"""
|
||||
@@ -147,7 +145,7 @@ class ParamsService:
|
||||
raise CustomException(msg='更新失败,该数系统配置不存在')
|
||||
if exist_obj.config_key != data.config_key:
|
||||
raise CustomException(msg='更新失败,系统配置key不允许修改')
|
||||
|
||||
|
||||
new_obj = await ParamsCRUD(auth).update_obj_crud(id=id, data=data)
|
||||
if not new_obj:
|
||||
raise CustomException(msg='更新失败,系统配置不存在')
|
||||
@@ -174,12 +172,12 @@ class ParamsService:
|
||||
async def delete_obj_service(cls, auth: AuthSchema, redis: Redis, ids: list[int]) -> None:
|
||||
"""
|
||||
删除配置管理型
|
||||
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证信息模型
|
||||
- redis (Redis): Redis 客户端实例
|
||||
- ids (list[int]): 配置管理型ID列表
|
||||
|
||||
|
||||
返回:
|
||||
- None
|
||||
"""
|
||||
@@ -193,9 +191,9 @@ class ParamsService:
|
||||
if exist_obj.config_type:
|
||||
# 如果有字典数据,不能删除
|
||||
raise CustomException(msg=f'{exist_obj.config_name} 删除失败,系统初始化配置不可以删除')
|
||||
|
||||
|
||||
await ParamsCRUD(auth).delete_obj_crud(ids=ids)
|
||||
|
||||
|
||||
# 同步删除Redis缓存
|
||||
for id in ids:
|
||||
exist_obj = await ParamsCRUD(auth).get_obj_by_id_crud(id=id)
|
||||
@@ -208,21 +206,21 @@ class ParamsService:
|
||||
except Exception as e:
|
||||
log.error(f"删除系统配置失败: {e}")
|
||||
raise CustomException(msg="删除字典类型失败")
|
||||
|
||||
|
||||
@classmethod
|
||||
async def export_obj_service(cls, data_list: list[dict]) -> bytes:
|
||||
"""
|
||||
导出系统配置列表
|
||||
|
||||
|
||||
参数:
|
||||
- data_list (list[dict]): 系统配置模型实例字典列表表示
|
||||
|
||||
|
||||
返回:
|
||||
- bytes: Excel文件二进制数据
|
||||
"""
|
||||
mapping_dict = {
|
||||
'id': '编号',
|
||||
'config_name': '参数名称',
|
||||
'config_name': '参数名称',
|
||||
'config_key': '参数键名',
|
||||
'config_value': '参数键值',
|
||||
'config_type': '系统内置((True:是 False:否))',
|
||||
@@ -241,21 +239,21 @@ class ParamsService:
|
||||
item['creator'] = item.get('creator', {}).get('name', '未知') if isinstance(item.get('creator'), dict) else '未知'
|
||||
|
||||
return ExcelUtil.export_list2excel(list_data=data, mapping_dict=mapping_dict)
|
||||
|
||||
|
||||
@classmethod
|
||||
async def upload_service(cls, base_url: str, file: UploadFile) -> dict:
|
||||
"""
|
||||
上传文件
|
||||
|
||||
|
||||
参数:
|
||||
- base_url (str): 基础URL
|
||||
- file (UploadFile): 上传的文件对象
|
||||
|
||||
|
||||
返回:
|
||||
- dict: 上传文件的响应模型实例字典表示
|
||||
"""
|
||||
filename, filepath, file_url = await UploadUtil.upload_file(file=file, base_url=base_url)
|
||||
|
||||
|
||||
return UploadResponseSchema(
|
||||
file_path=f'{filepath}',
|
||||
file_name=filename,
|
||||
@@ -267,10 +265,10 @@ class ParamsService:
|
||||
async def init_config_service(cls, redis: Redis) -> None:
|
||||
"""
|
||||
初始化系统配置
|
||||
|
||||
|
||||
参数:
|
||||
- redis (Redis): Redis 客户端实例
|
||||
|
||||
|
||||
返回:
|
||||
- None
|
||||
"""
|
||||
@@ -302,10 +300,10 @@ class ParamsService:
|
||||
async def get_init_config_service(cls, redis: Redis) -> list[dict]:
|
||||
"""
|
||||
获取系统配置
|
||||
|
||||
|
||||
参数:
|
||||
- redis (Redis): Redis 客户端实例
|
||||
|
||||
|
||||
返回:
|
||||
- list[dict]: 系统配置模型实例字典列表表示
|
||||
"""
|
||||
@@ -316,22 +314,22 @@ class ParamsService:
|
||||
if not config:
|
||||
continue
|
||||
try:
|
||||
new_config = json.loads(config)
|
||||
new_config = json.loads(config)
|
||||
configs.append(new_config)
|
||||
except Exception as e:
|
||||
log.error(f"解析系统配置数据失败: {e}")
|
||||
continue
|
||||
|
||||
|
||||
return configs
|
||||
|
||||
|
||||
@classmethod
|
||||
async def get_system_config_for_middleware(cls, redis: Redis) -> dict:
|
||||
"""
|
||||
获取中间件所需的系统配置
|
||||
|
||||
|
||||
参数:
|
||||
- redis (Redis): Redis 客户端实例
|
||||
|
||||
|
||||
返回:
|
||||
- dict: 包含演示模式、IP白名单、API白名单和IP黑名单的配置字典
|
||||
"""
|
||||
@@ -342,10 +340,10 @@ class ParamsService:
|
||||
f"{RedisInitKeyConfig.SYSTEM_CONFIG.key}:white_api_list_path",
|
||||
f"{RedisInitKeyConfig.SYSTEM_CONFIG.key}:ip_black_list"
|
||||
]
|
||||
|
||||
|
||||
# 批量获取配置
|
||||
config_values = await RedisCURD(redis).mget(config_keys)
|
||||
|
||||
|
||||
# 初始化默认配置
|
||||
config_result = {
|
||||
"demo_enable": False,
|
||||
@@ -353,24 +351,24 @@ class ParamsService:
|
||||
"white_api_list_path": [],
|
||||
"ip_black_list": []
|
||||
}
|
||||
|
||||
|
||||
# 解析演示模式配置
|
||||
if config_values[0]:
|
||||
try:
|
||||
demo_config = json.loads(config_values[0])
|
||||
config_result["demo_enable"] = demo_config.get("config_value", False) if isinstance(demo_config, dict) else False
|
||||
except json.JSONDecodeError:
|
||||
log.error(f"解析演示模式配置失败")
|
||||
|
||||
log.error("解析演示模式配置失败")
|
||||
|
||||
# 解析IP白名单配置
|
||||
if config_values[1]:
|
||||
|
||||
|
||||
try:
|
||||
ip_white_config = json.loads(config_values[1])
|
||||
# 确保是列表类型
|
||||
config_result["ip_white_list"] = json.loads(ip_white_config.get("config_value", []))
|
||||
config_result["ip_white_list"] = json.loads(ip_white_config.get("config_value", []))
|
||||
except json.JSONDecodeError:
|
||||
log.error(f"解析IP白名单配置失败")
|
||||
log.error("解析IP白名单配置失败")
|
||||
# 解析IP黑名单
|
||||
# 解析API路径白名单
|
||||
if config_values[2]:
|
||||
@@ -379,8 +377,8 @@ class ParamsService:
|
||||
# 确保是列表类型
|
||||
config_result["white_api_list_path"] = json.loads(white_api_config.get("config_value", []))
|
||||
except json.JSONDecodeError:
|
||||
log.error(f"解析API白名单配置失败")
|
||||
|
||||
log.error("解析API白名单配置失败")
|
||||
|
||||
# 解析IP黑名单
|
||||
if config_values[3]:
|
||||
try:
|
||||
@@ -388,5 +386,5 @@ class ParamsService:
|
||||
# 确保是列表类型
|
||||
config_result["ip_black_list"] = json.loads(black_ip_config.get("config_value", []))
|
||||
except json.JSONDecodeError:
|
||||
log.error(f"解析IP黑名单配置失败")
|
||||
return config_result
|
||||
log.error("解析IP黑名单配置失败")
|
||||
return config_result
|
||||
|
||||
Reference in New Issue
Block a user