views.py 4.1 KB

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