mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-21 12:52:26 +00:00
- 修改接口定义,增加路径参数并完善请求描述,增强参数校验和依赖注入 - 优化CRUD层数据库操作,统一异步会话使用,删除多余db参数 - 增加业务表与字段模型关系级联删除配置,优化模型关联关系声明 - 精简pydantic模型,去除冗余校验装饰器,完善字段描述和必填约束 - 服务层增加类型检查和异常抛出,规范业务逻辑流程和错误提示 - 优化代码结构,调整模块导入顺序和注释,提升代码可读性和一致性
55 lines
2.1 KiB
Python
55 lines
2.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
from typing import Dict, List, Sequence, Optional
|
|
|
|
from app.core.base_crud import CRUDBase
|
|
from .model import RoleModel
|
|
from .schema import RoleCreateSchema, RoleUpdateSchema
|
|
from ..auth.schema import AuthSchema
|
|
from ..menu.crud import MenuCRUD
|
|
from ..dept.crud import DeptCRUD
|
|
|
|
|
|
class RoleCRUD(CRUDBase[RoleModel, RoleCreateSchema, RoleUpdateSchema]):
|
|
"""角色模块数据层"""
|
|
|
|
def __init__(self, auth: AuthSchema) -> None:
|
|
self.auth = auth
|
|
super().__init__(model=RoleModel, auth=auth)
|
|
|
|
async def get_by_id_crud(self, id: int) -> Optional[RoleModel]:
|
|
"""根据id获取角色信息"""
|
|
return await self.get(id=id)
|
|
|
|
async def get_list_crud(self, search: Optional[Dict] = None, order_by: Optional[List[Dict[str, str]]] = None) -> Sequence[RoleModel]:
|
|
"""获取角色列表"""
|
|
return await self.list(search=search, order_by=order_by)
|
|
|
|
async def set_role_menus_crud(self, role_ids: List[int], menu_ids: List[int]) -> None:
|
|
"""设置角色的菜单权限"""
|
|
roles = await self.list(search={"id": ("in", role_ids)})
|
|
menus = await MenuCRUD(self.auth).get_list_crud(search={"id": ("in", menu_ids)})
|
|
await self.update_relationships(
|
|
objs_to_update=roles,
|
|
relationship_field="menus",
|
|
related_objs=menus
|
|
)
|
|
|
|
async def set_role_data_scope_crud(self, role_ids: List[int], data_scope: int) -> None:
|
|
"""设置角色的数据范围"""
|
|
await self.set(ids=role_ids, data_scope=data_scope)
|
|
|
|
async def set_role_depts_crud(self, role_ids: List[int], dept_ids: List[int]) -> None:
|
|
"""设置角色的部门权限"""
|
|
roles = await self.list(search={"id": ("in", role_ids)})
|
|
depts = await DeptCRUD(self.auth).get_list_crud(search={"id": ("in", dept_ids)})
|
|
await self.update_relationships(
|
|
objs_to_update=roles,
|
|
relationship_field="depts",
|
|
related_objs=depts
|
|
)
|
|
|
|
async def set_available_crud(self, ids: List[int], status: bool) -> None:
|
|
"""设置角色的可用状态"""
|
|
await self.set(ids=ids, status=status)
|