sysmodule.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. # #!/usr/bin/env python
  2. # # -*- coding: utf-8 -*-
  3. #
  4. # from typing import Optional
  5. #
  6. # from fastapi import APIRouter, Path, Depends
  7. # from sqlalchemy import delete
  8. # from sqlalchemy.ext.asyncio import AsyncSession
  9. #
  10. # from admin.schemas.sysdata.sysmodule import (SysApiItemList, SysApiItemDetail,
  11. # CreateSysApi, UpdateSysApi,
  12. # DeleteSysApi, SysModuleList,
  13. # SysModuleDetail, CreateSysModule)
  14. # from core.security import enforcer
  15. # from crud.sysdata.role import crud_role_api
  16. # from crud.sysdata.sysmodule import crud_sys_api, crud_sys_module
  17. # from models.sysdata.role import RoleApi
  18. # from models.sysdata.sysmodule import SysModule
  19. # from models.user import SysUser
  20. # from utils.depends import get_async_db, get_current_user
  21. #
  22. # router = APIRouter()
  23. #
  24. #
  25. # @router.get("/modules", response_model=SysModuleList, summary="模块列表")
  26. # async def get_modules(page: Optional[int] = 1,
  27. # size: Optional[int] = 10,
  28. # id: Optional[int] = 0,
  29. # name: Optional[str] = "",
  30. # level: Optional[int] = 0,
  31. # db: AsyncSession = Depends(get_async_db)):
  32. #
  33. # filter = {}
  34. # if name: # 按name查询
  35. # filter["name"] = name
  36. # elif id and (not level): # 按ID查询
  37. # filter["id"] = id
  38. # elif (not id) and level: # 按 level 查询
  39. # filter["level"] = level
  40. # elif id and level: # 按 pid 和 level 查询
  41. # filter.update({"pid": id, "level": level})
  42. # else:
  43. # filter = [SysModule.level.in_([0, 1])]
  44. # offset = (page - 1) * size
  45. #
  46. # total, items = await crud_sys_module.findall(db, filter, offset, size)
  47. # return {"total": total, "data": items}
  48. #
  49. #
  50. # @router.post("/modules", response_model=SysModuleDetail, summary="新建模块")
  51. # async def create_module(module: CreateSysModule,
  52. # current_user: SysUser = Depends(get_current_user),
  53. # db: AsyncSession = Depends(get_async_db)):
  54. # # 参数校验
  55. # obj_in = module.dict(exclude_none=True)
  56. # if not obj_in:
  57. # return {"errcode": 400, "mess": "请求参数为空!"}
  58. #
  59. # # 判断是否存在同名模块
  60. # db_obj = await crud_sys_module.find_one(db, {
  61. # "name": obj_in["name"],
  62. # "pid": obj_in["pid"],
  63. # "level": obj_in["level"]
  64. # })
  65. # if db_obj:
  66. # return {"errcode": 400, "mess": "模块已存在!"}
  67. #
  68. # # 新增
  69. # obj_in["creator"] = current_user.id
  70. # item = await crud_sys_module.create(db, obj_in)
  71. # return {"data": item}
  72. #
  73. #
  74. # @router.get("/apis", response_model=SysApiItemList, summary="接口列表")
  75. # async def get_apis(page: Optional[int] = 1,
  76. # size: Optional[int] = 10,
  77. # module: Optional[int] = 0,
  78. # name: Optional[str] = "",
  79. # path: Optional[str] = "",
  80. # db: AsyncSession = Depends(get_async_db)):
  81. #
  82. # filter = {}
  83. # if name:
  84. # filter["name"] = name
  85. # if path:
  86. # filter["path"] = path
  87. # if module:
  88. # filter["module_id"] = module
  89. # offset = (page - 1) * size
  90. #
  91. # total, items = await crud_sys_api.findall(db, filter, offset, size)
  92. # return {"total": total, "data": items}
  93. #
  94. #
  95. # @router.post("/apis", response_model=SysApiItemDetail, summary="新增接口")
  96. # async def create_api(api: CreateSysApi,
  97. # db: AsyncSession = Depends(get_async_db),
  98. # current_user: SysUser = Depends(get_current_user)):
  99. # # 参数校验
  100. # obj_in = api.dict(exclude_none=True)
  101. # if not obj_in:
  102. # return {"errcode": 400, "mess": "请求参数为空!"}
  103. #
  104. # # 判断path是否重复
  105. # db_api = await crud_sys_api.find_one(db, {"path": obj_in["path"]})
  106. # if db_api:
  107. # return {"errcode": 400, "mess": "接口已存在!"}
  108. #
  109. # # 判断 module 是否存在
  110. # db_module = await crud_sys_module.find_one(db, {"id": api.module_id})
  111. # if db_module:
  112. # return {"errcode": 404, "mess": "模块不存在!"}
  113. #
  114. # # 新增
  115. # obj_in["creator"] = current_user.id
  116. # item = await crud_sys_api.create(db, obj_in)
  117. # return {"data": item}
  118. #
  119. #
  120. # @router.put("/apis/{id}", response_model=SysApiItemDetail, summary="更新接口")
  121. # async def update_api(api: UpdateSysApi,
  122. # id: int = Path(..., description="接口ID"),
  123. # db: AsyncSession = Depends(get_async_db),
  124. # current_user: SysUser = Depends(get_current_user)):
  125. # # 参数校验
  126. # obj_in = api.dict(exclude_none=True)
  127. # if not obj_in:
  128. # return {"errcode": 400, "mess": "请求参数为空!"}
  129. #
  130. # db_api = await crud_sys_api.find_one(db, {"id": id})
  131. # if not db_api:
  132. # return {"errcode": 404, "mess": "接口不存在!"}
  133. #
  134. # # 判断 module 是否存在
  135. # db_module = await crud_sys_module.find_one(db, {"id": api.module_id})
  136. # if db_module:
  137. # return {"errcode": 404, "mess": "模块不存在!"}
  138. #
  139. # # 更新
  140. # obj_in["editor"] = current_user.id
  141. # item = await crud_sys_api.update(db, db_api, obj_in)
  142. # return {"data": item}
  143. #
  144. #
  145. # @router.delete("/apis/{id}", response_model=SysApiItemDetail, summary="删除接口")
  146. # async def delete_api(id: int = Path(..., description="接口ID"),
  147. # db: AsyncSession = Depends(get_async_db),
  148. # current_user: SysUser = Depends(get_current_user)):
  149. #
  150. # db_obj = await crud_sys_api.find_one(db, {"id": id})
  151. # if not db_obj:
  152. # return {"errcode": 404, "mess": "接口不存在!"}
  153. #
  154. # # 删除
  155. # obj_in = DeleteSysApi(id=id, is_active=False, editor=current_user)
  156. # item = await crud_sys_api.update(db, db_obj, obj_in)
  157. #
  158. # # 1、从casbin_rule表中删除与本API相关的权限
  159. # enforcer.remove_filtered_policy(1, db_obj.path)
  160. # # 2、从角色的权限表里删除本API
  161. # stmt = delete(RoleApi).where(RoleApi.api_id == db_obj.id)
  162. # await crud_role_api.execute(db, stmt)
  163. #
  164. # return {"data": item}