Files
FastapiAdmin/backend/app/api/v1/module_system/menu/crud.py
T
zhangtao e159b31e62 feat: Add role code to system_role.json and update system_users.json creator_id to null
fix: Refactor initialize.py to handle nested children data during initialization

feat: Implement tree structure traversal functions in common_util.py

chore: Update requirements.txt to specify sqlalchemy-crud-plus version and add rich

refactor: Change API endpoints in dept.ts and menu.ts to return tree structure

feat: Add code field to role, dept, and menu interfaces in respective TypeScript files

fix: Update dept and role Vue components to display and handle code field

docs: Add comprehensive project documentation for FastAPI Vue3 Admin
2025-09-15 00:19:11 +08:00

72 lines
2.3 KiB
Python

# -*- coding: utf-8 -*-
from typing import Dict, List, Optional, Sequence
from app.core.base_crud import CRUDBase
from ..auth.schema import AuthSchema
from .model import MenuModel
from .schema import MenuCreateSchema, MenuUpdateSchema
class MenuCRUD(CRUDBase[MenuModel, MenuCreateSchema, MenuUpdateSchema]):
"""菜单模块数据层"""
def __init__(self, auth: AuthSchema) -> None:
"""初始化菜单CRUD"""
self.auth = auth
super().__init__(model=MenuModel, auth=auth)
async def get_by_id_crud(self, id: int) -> Optional[MenuModel]:
"""
根据id获取菜单信息
:param id: 菜单ID
:return: 菜单信息
"""
obj = await self.get(id=id)
if not obj:
return None
if obj.parent_id:
parent = await self.get(id=obj.parent_id)
if parent:
obj.parent_name = parent.name
return obj
async def get_list_crud(self, search: Dict = None, order_by: List[Dict[str, str]] = None) -> Sequence[MenuModel]:
"""
获取菜单列表
:param search: 搜索条件
:param order_by: 排序字段
:return: 菜单列表
"""
obj_list = await self.list(search=search, order_by=order_by)
parent_ids = [obj.parent_id for obj in obj_list if obj.parent_id]
if parent_ids:
parents = await self.list(search={"id": ("in", parent_ids)})
parent_map = {p.id: p.name for p in parents}
for obj in obj_list:
if obj.parent_id:
obj.parent_name = parent_map.get(obj.parent_id)
return obj_list
async def get_tree_list_crud(self, search: Dict = None, order_by: List[Dict[str, str]] = None) -> Sequence[MenuModel]:
"""
获取菜单树形列表
:param search: 搜索条件
:param order_by: 排序字段
:return: 菜单树形列表
"""
return await self.get_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: 可用状态
"""
await self.set(ids=ids, status=status)