role.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from typing import Optional
  4. from fastapi import APIRouter, Depends, Query
  5. from sqlalchemy import text
  6. from sqlalchemy.ext.asyncio import AsyncSession
  7. from crud.sysdata.role import crud_role, crud_permission
  8. from models.user import SysUser
  9. from schemas.base import ReturnField, OrderByField
  10. from schemas.sysdata.role import RoleList, RoleInfo, RoleDetail, PermissionList
  11. from utils.depends import get_async_db, get_current_user
  12. router = APIRouter()
  13. @router.post("/role", summary="新增角色", response_model=RoleDetail)
  14. async def create_role(info: RoleInfo,
  15. db: AsyncSession = Depends(get_async_db),
  16. current_user: SysUser = Depends(get_current_user)):
  17. info = info.dict(exclude_none=True)
  18. if not info:
  19. return {"errcode": 400, "mess": "请求参数为空!"}
  20. db_exist = await crud_role.find_one(db, {"name": info["name"]})
  21. if db_exist:
  22. return {"errcode": 400, "mess": "角色名称重复!"}
  23. info["creator_id"] = current_user.id
  24. info["creator_name"] = current_user.username
  25. db_obj = await crud_role.insert_one(db, info)
  26. return {"data": db_obj}
  27. @router.put("/role/{rid}", summary="修改角色", response_model=RoleDetail)
  28. async def update_role(rid: int,
  29. info: RoleInfo,
  30. db: AsyncSession = Depends(get_async_db),
  31. current_user: SysUser = Depends(get_current_user)):
  32. info = info.dict(exclude_none=True)
  33. if not info:
  34. return {"errcode": 400, "mess": "请求参数为空!"}
  35. db_exist = await crud_role.find_one(db, {"id": rid})
  36. if not db_exist:
  37. return {"errcode": 400, "mess": "角色不存在!"}
  38. info["editor_id"] = current_user.id
  39. info["editor_name"] = current_user.username
  40. db_obj = await crud_role.update(db, db_exist, info)
  41. return {"data": db_obj}
  42. @router.delete("/role/{rid}", summary="删除角色", response_model=RoleDetail)
  43. async def delete_role(rid: int,
  44. db: AsyncSession = Depends(get_async_db),
  45. current_user: SysUser = Depends(get_current_user)):
  46. existed = await crud_role.count(db, {"id": rid})
  47. if not existed:
  48. return {"errcode": 400, "mess": "角色不存在!"}
  49. await crud_role.delete(db, rid)
  50. return {"data": None}
  51. @router.get("/role/{rid}", summary="角色详情", response_model=RoleDetail)
  52. async def get_role(rid: int,
  53. db: AsyncSession = Depends(get_async_db),
  54. current_user: SysUser = Depends(get_current_user)):
  55. db_exist = await crud_role.find_one(db, {"id": rid})
  56. if not db_exist:
  57. return {"errcode": 400, "mess": "角色不存在!"}
  58. return {"data": db_exist}
  59. @router.get("/roles", summary="角色列表", response_model=RoleList)
  60. async def get_role_list(page: Optional[int] = None,
  61. size: Optional[int] = None,
  62. name: str = "",
  63. res: ReturnField = Query("",
  64. description="控制返回字段,字段逗号分隔"),
  65. order: OrderByField = Query(
  66. "-created_at",
  67. description="排序字段,用逗号分隔,升降序以-判断,默认-created_at"),
  68. db: AsyncSession = Depends(get_async_db),
  69. current_user: SysUser = Depends(get_current_user)):
  70. filters = []
  71. if name:
  72. filters.append(text(f"name LIKE '%{name}%'"))
  73. if ((page is not None) and page >= 1) and ((size is not None)
  74. and size >= 1):
  75. offset = (page - 1) * size
  76. else:
  77. size = offset = None
  78. if isinstance(order, str):
  79. order = [text(order)]
  80. total, items = await crud_role.find_all(db,
  81. filters=filters,
  82. offset=offset,
  83. limit=size,
  84. order_by=order,
  85. return_fields=res)
  86. return {"total": total, "data": items}
  87. @router.get("/permissions", summary="权限类别", response_model=PermissionList)
  88. async def get_permission_all(name: str = "",
  89. res: ReturnField = Query(
  90. "", description="控制返回字段,字段逗号分隔"),
  91. db: AsyncSession = Depends(get_async_db),
  92. current_user: SysUser = Depends(get_current_user)):
  93. filters = []
  94. if name:
  95. filters.append(text(f"name LIKE '%{name}%'"))
  96. items = await crud_permission.fetch_all(db,
  97. filters=filters,
  98. return_fields=res)
  99. return {"data": items}