role.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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.sysdata.role import Role, Permission
  9. from models.user import Admin
  10. from schemas.base import ReturnField, OrderByField
  11. from schemas.sysdata.role import RoleList, RoleDetail, NewRoleInfo, UpdateRoleInfo
  12. from utils.depends import get_async_db, get_current_user
  13. router = APIRouter()
  14. @router.post("/role", summary="新增角色", response_model=RoleDetail)
  15. async def create_role(info: NewRoleInfo,
  16. db: AsyncSession = Depends(get_async_db),
  17. current_user: Admin = Depends(get_current_user)):
  18. info = info.dict(exclude_none=True)
  19. if not info:
  20. return {"errcode": 400, "mess": "请求参数为空!"}
  21. db_role = await crud_role.find_one(db, filters={"name": info["name"]})
  22. if db_role:
  23. return {"errcode": 400, "mess": "角色名称重复!"}
  24. info["creator_id"] = current_user.id
  25. info["creator_name"] = current_user.username
  26. db_role = await crud_role.insert_one(db, info)
  27. return {"data": db_role}
  28. @router.put("/role/{rid}", summary="修改角色", response_model=RoleDetail)
  29. async def update_role(rid: int,
  30. info: UpdateRoleInfo,
  31. db: AsyncSession = Depends(get_async_db),
  32. current_user: Admin = Depends(get_current_user)):
  33. info_dict = info.dict(exclude_none=True)
  34. if not info_dict:
  35. return {"errcode": 400, "mess": "请求参数为空!"}
  36. db_role = await crud_role.find_one(db, filters={"id": rid})
  37. if not db_role:
  38. return {"errcode": 400, "mess": "角色不存在!"}
  39. info_dict["editor_id"] = current_user.id
  40. info_dict["editor_name"] = current_user.username
  41. db_role = await crud_role.update(db, db_role, info_dict)
  42. return {"data": db_role}
  43. @router.delete("/role/{rid}", summary="删除角色", response_model=RoleDetail)
  44. async def delete_role(rid: int,
  45. db: AsyncSession = Depends(get_async_db),
  46. current_user: Admin = Depends(get_current_user)):
  47. # 不允许删除初始管理员角色,ID=1
  48. if rid == 1:
  49. return {"errcode": 400, "mess": "该角色不允许删除!"}
  50. db_role = await crud_role.count(db, filters={"id": rid})
  51. if not db_role:
  52. return {"errcode": 404, "mess": "角色不存在!"}
  53. await crud_role.delete(db, obj_id=rid)
  54. return {"data": None}
  55. @router.get("/role/{rid}", summary="角色详情", response_model=RoleDetail)
  56. async def get_role(rid: int,
  57. db: AsyncSession = Depends(get_async_db),
  58. current_user: Admin = Depends(get_current_user)):
  59. db_role = await crud_role.find_one(db, filters={"id": rid})
  60. if not db_role:
  61. return {"errcode": 404, "mess": "角色不存在!"}
  62. return {"data": db_role}
  63. @router.get("/roles", summary="角色列表", response_model=RoleList)
  64. async def get_role_list(page: Optional[int] = None,
  65. size: Optional[int] = None,
  66. platform: str = "",
  67. name: str = "",
  68. res: ReturnField = Query("", description="控制返回字段,字段逗号分隔"),
  69. order: OrderByField = Query("-created_at",
  70. description="排序字段,用逗号分隔,升降序以-判断,默认-created_at"),
  71. db: AsyncSession = Depends(get_async_db),
  72. current_user: Admin = Depends(get_current_user)):
  73. _q = []
  74. if name:
  75. _q.append(Role.name.like(f"%{name}%"))
  76. if platform:
  77. _q.append(Role.platform == platform)
  78. if ((page is not None) and page >= 1) and ((size is not None) and size >= 1):
  79. offset = (page - 1) * size
  80. else:
  81. size = offset = None
  82. if isinstance(order, str):
  83. order = [text(order)]
  84. total, db_roles = await crud_role.find_all(db,
  85. filters=_q,
  86. offset=offset,
  87. limit=size,
  88. order_by=order,
  89. return_fields=res)
  90. return {"data": db_roles, "total": total}
  91. @router.get("/permissions", summary="权限类别")
  92. async def get_permission_all(name: str = "",
  93. res: ReturnField = Query("", description="控制返回字段,字段逗号分隔"),
  94. db: AsyncSession = Depends(get_async_db),
  95. current_user: Admin = Depends(get_current_user)):
  96. _q = []
  97. if name:
  98. _q.append(Permission.name.like(f"%{name}%"))
  99. total, db_perms = await crud_permission.find_all(db, filters=_q, return_fields=res)
  100. db_perms = [{
  101. "name": "首页",
  102. "value": "menu.*",
  103. "children": [{
  104. "name": "首页",
  105. "value": "menu.menu",
  106. "children": []
  107. }]
  108. }, {
  109. "name":
  110. "学校管理",
  111. "value":
  112. "menu2.*",
  113. "children": [{
  114. "name": "学校管理",
  115. "value": "menu2.menu2-1",
  116. "children": []
  117. }, {
  118. "name": "班级管理",
  119. "value": "menu2.menu2-2",
  120. "children": []
  121. }]
  122. }, {
  123. "name":
  124. "师生信息",
  125. "value":
  126. "menu3.*",
  127. "children": [{
  128. "name": "教师管理",
  129. "value": "menu3.menu3-1",
  130. "children": []
  131. }, {
  132. "name": "学生管理",
  133. "value": "menu3.menu3-2",
  134. "children": []
  135. }]
  136. }, {
  137. "name":
  138. "分类管理",
  139. "value":
  140. "menu8.*",
  141. "children": [{
  142. "name": "作业分类",
  143. "value": "menu8.menu8-1",
  144. "children": []
  145. }, {
  146. "name": "试卷分类",
  147. "value": "menu8.menu8-2",
  148. "children": []
  149. }]
  150. }, {
  151. "name":
  152. "资源管理",
  153. "value":
  154. "menu4.*",
  155. "children": [{
  156. "name": "作业资源",
  157. "value": "menu4.menu4-1",
  158. "children": []
  159. }, {
  160. "name": "考试资源",
  161. "value": "menu4.menu4-2",
  162. "children": []
  163. }]
  164. }, {
  165. "name":
  166. "阅卷管理",
  167. "value":
  168. "menu5.*",
  169. "children": [{
  170. "name": "作业管理",
  171. "value": "menu5.menu5-1",
  172. "children": []
  173. }, {
  174. "name": "作业批阅",
  175. "value": "menu5.menu5-2",
  176. "children": []
  177. }, {
  178. "name": "考试批阅",
  179. "value": "menu5.menu5-3",
  180. "children": []
  181. }, {
  182. "name": "考试管理",
  183. "value": "menu5.menu5-4",
  184. "children": []
  185. }]
  186. }, {
  187. "name":
  188. "错题管理",
  189. "value":
  190. "menu6.*",
  191. "children": [{
  192. "name": "班级错题",
  193. "value": "menu6.menu6-1",
  194. "children": []
  195. }, {
  196. "name": "学生错题",
  197. "value": "menu6.menu6-2",
  198. "children": []
  199. }, {
  200. "name": "错题打印",
  201. "value": "menu6.menu6-3",
  202. "children": []
  203. }]
  204. }, {
  205. "name":
  206. "系统管理",
  207. "value":
  208. "menu7.*",
  209. "children": [{
  210. "name": "账号管理",
  211. "value": "menu7.menu7-1",
  212. "children": []
  213. }, {
  214. "name": "角色管理",
  215. "value": "menu7.menu7-2",
  216. "children": []
  217. }]
  218. }]
  219. return {"data": db_perms, "total": total}