mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-23 05:10:57 +00:00
refactor: 优化代码注释和文档字符串格式
style: 统一代码风格和格式 docs: 完善函数和方法的文档字符串 refactor(base_model): 移除冗余的表名和表参数生成方法 refactor(constant): 更新返回码注释格式 refactor(router_class): 添加路由处理器的详细文档 refactor(database): 完善数据库连接函数的文档 refactor(security): 添加认证类和方法的详细文档 refactor(validator): 更新验证器函数的文档格式 refactor(serialize): 优化序列化工具类的文档 refactor(response): 完善响应类的文档字符串 refactor(dependencies): 添加依赖函数的详细文档 refactor(initialize): 完善初始化脚本的文档 refactor(plugin): 添加生命周期和中间件注册的文档 refactor(service): 完善服务层方法的文档 refactor(controller): 添加控制器方法的详细文档 refactor(crud): 完善CRUD操作的文档字符串 refactor(schema): 简化模型类并移除冗余字段 refactor(param): 更新查询参数类的注释格式 refactor(template): 优化代码生成模板的格式 refactor(console): 添加控制台输出功能的实现 refactor(util): 完善工具函数的文档字符串
This commit is contained in:
@@ -21,9 +21,18 @@ MenuRouter = APIRouter(route_class=OperationLogRoute, prefix="/menu", tags=["菜
|
||||
|
||||
@MenuRouter.get("/tree", summary="查询菜单树", description="查询菜单树")
|
||||
async def get_menu_tree_controller(
|
||||
search: MenuQueryParam = Depends(),
|
||||
auth: AuthSchema = Depends(AuthPermission(["system:menu:query"]))
|
||||
search: MenuQueryParam = Depends(),
|
||||
auth: AuthSchema = Depends(AuthPermission(["system:menu:query"]))
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
查询菜单树。
|
||||
|
||||
参数:
|
||||
- search (MenuQueryParam): 查询参数模型。
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含菜单树的 JSON 响应。
|
||||
"""
|
||||
order_by = [{"order": "asc"}]
|
||||
result_dict_list = await MenuService.get_menu_tree_service(search=search, auth=auth, order_by=order_by)
|
||||
logger.info(f"查询菜单树成功")
|
||||
@@ -32,9 +41,18 @@ async def get_menu_tree_controller(
|
||||
|
||||
@MenuRouter.get("/detail/{id}", summary="查询菜单详情", description="查询菜单详情")
|
||||
async def get_obj_detail_controller(
|
||||
id: int = Path(..., description="菜单ID"),
|
||||
auth: AuthSchema = Depends(AuthPermission(["system:menu:query"]))
|
||||
id: int = Path(..., description="菜单ID"),
|
||||
auth: AuthSchema = Depends(AuthPermission(["system:menu:query"]))
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
查询菜单详情。
|
||||
|
||||
参数:
|
||||
- id (int): 菜单ID。
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含菜单详情的 JSON 响应。
|
||||
"""
|
||||
result_dict = await MenuService.get_menu_detail_service(id=id, auth=auth)
|
||||
logger.info(f"查询菜单情成功 {id}")
|
||||
return SuccessResponse(data=result_dict, msg="获取菜单成功")
|
||||
@@ -45,6 +63,15 @@ async def create_obj_controller(
|
||||
data: MenuCreateSchema,
|
||||
auth: AuthSchema = Depends(AuthPermission(["system:menu:create"]))
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
创建菜单。
|
||||
|
||||
参数:
|
||||
- data (MenuCreateSchema): 菜单创建模型。
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含创建菜单的 JSON 响应。
|
||||
"""
|
||||
result_dict = await MenuService.create_menu_service(data=data, auth=auth)
|
||||
logger.info(f"创建菜单成功: {result_dict}")
|
||||
return SuccessResponse(data=result_dict, msg="创建菜单成功")
|
||||
@@ -56,6 +83,16 @@ async def update_obj_controller(
|
||||
id: int = Path(..., description="菜单ID"),
|
||||
auth: AuthSchema = Depends(AuthPermission(["system:menu:update"]))
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
修改菜单。
|
||||
|
||||
参数:
|
||||
- id (int): 菜单ID。
|
||||
- data (MenuUpdateSchema): 菜单更新模型。
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含修改菜单的 JSON 响应。
|
||||
"""
|
||||
result_dict = await MenuService.update_menu_service(id=id, data=data, auth=auth)
|
||||
logger.info(f"修改菜单成功: {result_dict}")
|
||||
return SuccessResponse(data=result_dict, msg="修改菜单成功")
|
||||
@@ -66,6 +103,15 @@ async def delete_obj_controller(
|
||||
ids: list[int] = Body(..., description="ID列表"),
|
||||
auth: AuthSchema = Depends(AuthPermission(["system:menu:delete"]))
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
删除菜单。
|
||||
|
||||
参数:
|
||||
- ids (list[int]): 菜单ID列表。
|
||||
|
||||
返回:
|
||||
- JSONResponse: 包含删除菜单的 JSON 响应。
|
||||
"""
|
||||
await MenuService.delete_menu_service(ids=ids, auth=auth)
|
||||
logger.info(f"删除菜单成功: {ids}")
|
||||
return SuccessResponse(msg="删除菜单成功")
|
||||
@@ -76,6 +122,15 @@ async def batch_set_available_obj_controller(
|
||||
data: BatchSetAvailable,
|
||||
auth: AuthSchema = Depends(AuthPermission(["system:menu:patch"]))
|
||||
) -> JSONResponse:
|
||||
"""
|
||||
批量修改菜单状态。
|
||||
|
||||
参数:
|
||||
- data (BatchSetAvailable): 批量修改菜单状态模型。
|
||||
|
||||
返回:
|
||||
- JSONResponse: 批量修改菜单状态的 JSON 响应。
|
||||
"""
|
||||
await MenuService.set_menu_available_service(data=data, auth=auth)
|
||||
logger.info(f"批量修改菜单状态成功: {data.ids}")
|
||||
return SuccessResponse(msg="批量修改菜单状态成功")
|
||||
@@ -18,10 +18,13 @@ class MenuCRUD(CRUDBase[MenuModel, MenuCreateSchema, MenuUpdateSchema]):
|
||||
|
||||
async def get_by_id_crud(self, id: int) -> Optional[MenuModel]:
|
||||
"""
|
||||
根据id获取菜单信息
|
||||
根据 id 获取菜单信息。
|
||||
|
||||
:param id: 菜单ID
|
||||
:return: 菜单信息
|
||||
参数:
|
||||
- id (int): 菜单 ID。
|
||||
|
||||
返回:
|
||||
- MenuModel | None: 菜单信息,未找到返回 None。
|
||||
"""
|
||||
obj = await self.get(id=id)
|
||||
if not obj:
|
||||
@@ -30,29 +33,39 @@ class MenuCRUD(CRUDBase[MenuModel, MenuCreateSchema, MenuUpdateSchema]):
|
||||
|
||||
async def get_list_crud(self, search: Optional[Dict] = None, order_by: Optional[List[Dict[str, str]]] = None) -> Sequence[MenuModel]:
|
||||
"""
|
||||
获取菜单列表
|
||||
获取菜单列表。
|
||||
|
||||
:param search: 搜索条件
|
||||
:param order_by: 排序字段
|
||||
:return: 菜单列表
|
||||
参数:
|
||||
- search (Dict | None): 搜索条件。
|
||||
- order_by (List[Dict[str, str]] | None): 排序字段列表。
|
||||
|
||||
返回:
|
||||
- Sequence[MenuModel]: 菜单列表。
|
||||
"""
|
||||
return await self.list(search=search, order_by=order_by)
|
||||
|
||||
async def get_tree_list_crud(self, search: Optional[Dict] = None, order_by: Optional[List[Dict[str, str]]] = None) -> Sequence[MenuModel]:
|
||||
"""
|
||||
获取菜单树形列表
|
||||
获取菜单树形列表。
|
||||
|
||||
:param search: 搜索条件
|
||||
:param order_by: 排序字段
|
||||
:return: 菜单树形列表
|
||||
参数:
|
||||
- search (Dict | None): 搜索条件。
|
||||
- order_by (List[Dict[str, str]] | None): 排序字段列表。
|
||||
|
||||
返回:
|
||||
- Sequence[MenuModel]: 菜单树形列表。
|
||||
"""
|
||||
return await self.tree_list(search=search, order_by=order_by, children_attr='children')
|
||||
|
||||
async def set_available_crud(self, ids: List[int], status: bool) -> None:
|
||||
"""
|
||||
批量设置菜单可用状态
|
||||
批量设置菜单可用状态。
|
||||
|
||||
:param ids: 菜单ID列表
|
||||
:param status: 可用状态
|
||||
参数:
|
||||
- ids (List[int]): 菜单 ID 列表。
|
||||
- status (bool): 可用状态。
|
||||
|
||||
返回:
|
||||
- None
|
||||
"""
|
||||
await self.set(ids=ids, status=status)
|
||||
|
||||
@@ -28,6 +28,16 @@ class MenuService:
|
||||
|
||||
@classmethod
|
||||
async def get_menu_detail_service(cls, auth: AuthSchema, id: int) -> Dict:
|
||||
"""
|
||||
获取菜单详情。
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证对象。
|
||||
- id (int): 菜单ID。
|
||||
|
||||
返回:
|
||||
- Dict: 菜单详情对象。
|
||||
"""
|
||||
menu = await MenuCRUD(auth).get_by_id_crud(id=id)
|
||||
if menu and menu.parent_id:
|
||||
parent = await MenuCRUD(auth).get_by_id_crud(id=menu.parent_id)
|
||||
@@ -40,12 +50,15 @@ class MenuService:
|
||||
@classmethod
|
||||
async def get_menu_tree_service(cls, auth: AuthSchema, search: Optional[MenuQueryParam] = None, order_by: Optional[List[Dict]] = None) -> List[Dict]:
|
||||
"""
|
||||
获取菜单树形列表service
|
||||
获取菜单树形列表。
|
||||
|
||||
:param auth: 认证对象
|
||||
:param search: 查询参数对象
|
||||
:param order_by: 排序参数
|
||||
:return: 菜单树形列表对象
|
||||
参数:
|
||||
- auth (AuthSchema): 认证对象。
|
||||
- search (MenuQueryParam | None): 查询参数对象。
|
||||
- order_by (List[Dict] | None): 排序参数列表。
|
||||
|
||||
返回:
|
||||
- List[Dict]: 菜单树形列表对象。
|
||||
"""
|
||||
# 使用树形结构查询,预加载children关系
|
||||
menu_list = await MenuCRUD(auth).get_tree_list_crud(search=search.__dict__, order_by=order_by)
|
||||
@@ -56,6 +69,16 @@ class MenuService:
|
||||
|
||||
@classmethod
|
||||
async def create_menu_service(cls, auth: AuthSchema, data: MenuCreateSchema) -> Dict:
|
||||
"""
|
||||
创建菜单。
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证对象。
|
||||
- data (MenuCreateSchema): 创建参数对象。
|
||||
|
||||
返回:
|
||||
- Dict: 创建的菜单对象。
|
||||
"""
|
||||
menu = await MenuCRUD(auth).get(name=data.name)
|
||||
if menu:
|
||||
raise CustomException(msg='创建失败,该菜单已存在')
|
||||
@@ -66,6 +89,17 @@ class MenuService:
|
||||
|
||||
@classmethod
|
||||
async def update_menu_service(cls, auth: AuthSchema,id:int, data: MenuUpdateSchema) -> Dict:
|
||||
"""
|
||||
更新菜单。
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证对象。
|
||||
- id (int): 菜单ID。
|
||||
- data (MenuUpdateSchema): 更新参数对象。
|
||||
|
||||
返回:
|
||||
- Dict: 更新的菜单对象。
|
||||
"""
|
||||
menu = await MenuCRUD(auth).get_by_id_crud(id=id)
|
||||
if not menu:
|
||||
raise CustomException(msg='更新失败,该菜单不存在')
|
||||
@@ -87,6 +121,16 @@ class MenuService:
|
||||
|
||||
@classmethod
|
||||
async def delete_menu_service(cls, auth: AuthSchema, ids: list[int]) -> None:
|
||||
"""
|
||||
删除菜单。
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证对象。
|
||||
- ids (list[int]): 菜单ID列表。
|
||||
|
||||
返回:
|
||||
- None
|
||||
"""
|
||||
if len(ids) < 1:
|
||||
raise CustomException(msg='删除失败,删除对象不能为空')
|
||||
for id in ids:
|
||||
@@ -98,7 +142,14 @@ class MenuService:
|
||||
@classmethod
|
||||
async def set_menu_available_service(cls, auth: AuthSchema, data: BatchSetAvailable) -> None:
|
||||
"""
|
||||
递归获取所有父、子级菜单,然后批量修改菜单可用状态
|
||||
递归获取所有父、子级菜单,然后批量修改菜单可用状态。
|
||||
|
||||
参数:
|
||||
- auth (AuthSchema): 认证对象。
|
||||
- data (BatchSetAvailable): 批量设置可用参数对象。
|
||||
|
||||
返回:
|
||||
- None
|
||||
"""
|
||||
menu_list = await MenuCRUD(auth).get_list_crud()
|
||||
total_ids = []
|
||||
|
||||
Reference in New Issue
Block a user