views.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. from django.db.models import F
  2. from rest_framework import status
  3. from rest_framework.response import Response
  4. from rest_framework.views import APIView
  5. from rest_framework.viewsets import ModelViewSet
  6. from treeAndTable.models import Department, Dept_job, Job
  7. from treeAndTable.serializer import Dept_Job_Serializer
  8. class DepartmentView(APIView):
  9. def get(self, request):
  10. reqData = request.GET
  11. # 前端可以传0值也可没没有parent键
  12. parent = reqData.get('parent', '0')
  13. # 处理获取祖节点时数据库值为null的时候问题
  14. parentId = None if parent == '0' else parent
  15. if not parent.isdigit():
  16. return Response(data={}, status=status.HTTP_400_BAD_REQUEST)
  17. resData = Department.objects.filter(parent_id=parentId).annotate(has_leaf=F('rght') - F('lft') - 1).values('id',
  18. 'name',
  19. 'has_leaf',
  20. 'parent_id').all()
  21. for res in resData:
  22. res['has_leaf'] = res['has_leaf'] // 2 == 0
  23. return Response(data=resData, status=status.HTTP_200_OK)
  24. def post(self, request):
  25. reqData = request.data
  26. # 获取前端给的父类id获取父类
  27. parent = Department.objects.get(id=reqData['id'])
  28. # 对象类型转换成dict类型
  29. data = Department.objects.create(name=reqData['name'], parent=parent).__dict__
  30. # 不删除该属性会转换json错误
  31. data.pop('_state')
  32. return Response(data=data, status=status.HTTP_200_OK)
  33. def put(self, request):
  34. data = request.data
  35. parent = Department.objects.get(id=data['id'])
  36. parent.name = data['name']
  37. parent.save()
  38. d = parent.__dict__
  39. d.pop('_state')
  40. return Response(data=d, status=status.HTTP_200_OK)
  41. def delete(self, request):
  42. try:
  43. # 当传递的id值不存在数据库或没有传id值直接捕获异常
  44. data = Department.objects.get(id=request.GET.get('id')).delete()
  45. except Exception:
  46. return Response(data={}, status=status.HTTP_400_BAD_REQUEST)
  47. return Response(data=data, status=status.HTTP_200_OK)
  48. '''
  49. post
  50. {
  51. "dept_id":"33",
  52. "name":"舍长"
  53. }
  54. delete
  55. 路由带上id
  56. Put
  57. {
  58. "dept":18,
  59. "job":6
  60. }
  61. '''
  62. class DeptAndJobView(ModelViewSet):
  63. queryset = Dept_job.objects.all()
  64. serializer_class = Dept_Job_Serializer
  65. '''
  66. 需要传递部门id和岗位名称
  67. '''
  68. def create(self, request):
  69. '''
  70. 判断参数是否正确
  71. 根据部门id和岗位名字创建对应岗位和对应的中间表数据
  72. :param request:
  73. :return:
  74. '''
  75. try:
  76. deptId = request.data['dept_id']
  77. name = request.data['name']
  78. except Exception:
  79. return Response(data={'msg':'请求参数错误'}, status=status.HTTP_400_BAD_REQUEST)
  80. try:
  81. # 如果给定的岗位名称不存在岗位表直接新建一个岗位
  82. job = Job.objects.get(name=name)
  83. except Exception:
  84. job = Job.objects.create(name=name)
  85. # 创建对应的中间表数据库
  86. Dept_job.objects.create(dept_id=deptId, job_id=job.id)
  87. return Response(data={'msg': 'success'}, status=status.HTTP_200_OK)