views.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import json
  2. from django.contrib.auth.models import User
  3. from django.db.models import Q
  4. from django.forms.models import model_to_dict
  5. from rest_framework import status
  6. from rest_framework.response import Response
  7. from rest_framework.views import APIView
  8. from even_table.models import Dept, Jobs, Dept2Jobs
  9. # Create your views here.
  10. # 部门增删查改
  11. class deptView(APIView):
  12. """
  13. request {name名字},parent_id{父级id,可空}
  14. 创建一个节点
  15. """
  16. def post(self, request):
  17. data = request.data
  18. res = []
  19. if 'parent_id' in data:
  20. # try:
  21. # 创建部门表,返回创建数据成功
  22. # print(data['parent_id'])
  23. # print(model_to_dict(Dept.objects.get(id=data['parent_id'])))
  24. res = Dept.objects.create(
  25. name=data['name'],
  26. parent_id=data['parent_id']
  27. )
  28. res = model_to_dict(res)
  29. res = {'node': res, 'message': data['name'] + ' 新增叶子成功'}
  30. return Response(data=res, status=status.HTTP_200_OK)
  31. # except Exception:
  32. # print(json.dump(Exception))
  33. # return Response(data={"message": '错误'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
  34. else:
  35. # try:
  36. res = Dept.objects.create(
  37. name=data['name'],
  38. )
  39. res = model_to_dict(res)
  40. res = {'node': res, 'message': data['name'] + ' 新增父级成功'}
  41. return Response(data=res, status=status.HTTP_200_OK)
  42. # except Exception:
  43. # return Response(data={"message": '错误'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
  44. """
  45. 得到节点
  46. request{parent_id(节点id,可空)}
  47. """
  48. def get(self, request):
  49. data = request.GET
  50. res = []
  51. if 'parent_id' in data:
  52. # 给parent_id 拿到子集
  53. res = Dept.objects.filter(parent_id=data['parent_id']).values().all()
  54. else:
  55. # 没有传name 则给第0层
  56. res = Dept.objects.filter(parent_id=None).values().all()
  57. return Response(data=res, status=status.HTTP_200_OK)
  58. """
  59. 删除节点
  60. {id:部门id}
  61. """
  62. def delete(self, request):
  63. data = request.data
  64. # 岗位 删除这个部门下面的所有岗位
  65. # 这个部门下面的所有岗位
  66. jobs = Dept2Jobs.objects.filter(dept_id=data['id']).values('job_id').all()
  67. job_ids = []
  68. for job in jobs:
  69. job_ids.append(job['job_id'])
  70. Jobs.objects.filter(id__in=job_ids).delete()
  71. # 部门 删除这个部门
  72. res = Dept.objects.filter(id=data['id']).delete()
  73. # 关联表 删除跟这个部门关联的
  74. Dept2Jobs.objects.filter(dept_id=data['id']).delete()
  75. return Response(data="删除成功", status=status.HTTP_200_OK)
  76. """
  77. 更新节点
  78. {id:节点id,name:节点名字,parent_id:父节点id(可空)}
  79. """
  80. def put(self, request):
  81. data = request.data
  82. if 'parent_id' in data:
  83. res = Dept.objects.filter(id=data['id']).update(name=data['name'], parent_id=data['parent_id'])
  84. else:
  85. res = Dept.objects.filter(id=data['id']).update(name=data['name'])
  86. return Response(data=res, status=status.HTTP_200_OK)
  87. # 岗位增删查改
  88. class jobsView(APIView):
  89. # 增 {name:岗位名字,dept_id:父级部门id}
  90. def post(self, request):
  91. data = request.data
  92. # 创建岗位
  93. res = Jobs.objects.create(
  94. name=data['name']
  95. )
  96. res = model_to_dict(res)
  97. if res:
  98. # 创建关联
  99. Dept2Jobs.objects.create(
  100. dept_id=data['dept_id'],
  101. job_id=res['id']
  102. )
  103. return Response(data={'result': res, 'message': '创建岗位成功'}, status=status.HTTP_200_OK)
  104. # 查 {dept_id:父级部门id(可空,空则查全部)}
  105. def get(self, request):
  106. # 从关联表里 查到 这个父级的id下的所有岗位id 查到job下面的所有name
  107. names = Dept2Jobs.objects.filter(dept=request.GET['dept_id']).values("job", "job__name")
  108. # print(ids)
  109. return Response(data={'result': names, 'message': '查询成功'}, status=status.HTTP_200_OK)
  110. # 更新 {id:岗位id,name:岗位新名字}
  111. def put(self, request):
  112. pass
  113. # 删 {id:岗位id}
  114. def delete(self, request):
  115. # 删除关联表数据
  116. data = request.data
  117. Dept2Jobs.objects.filter(job_id=data['id']).delete()
  118. # 删除岗位
  119. Jobs.objects.filter(id=data['id']).delete()
  120. return Response(data={'message': data['id'] + '删除成功'}, status=status.HTTP_200_OK)
  121. # 用户增删查改
  122. class userView(APIView):
  123. def post(self, request):
  124. pass
  125. # 用户查询 {job_id:岗位id(可空,有则选这个岗位下的用户)}
  126. def get(self, request):
  127. data = request.GET
  128. if 'job_id' in data:
  129. job_ids = Dept2Jobs.objects.filter(job_id=data['job_id'], user_id__isnull=False).values()
  130. ids = []
  131. for id in job_ids:
  132. ids.append(id)
  133. res = User.objects.filter(id__in=ids).values()
  134. else:
  135. res = User.objects.values()
  136. return Response(data={'result': res, 'message': '查询用户基础数据'}, status=status.HTTP_200_OK)
  137. # 用户替换部门 {job_id:岗位id,user_ids:用户id数组,type:移除用户还是新增用户(0移除,1新增)}
  138. def put(self, request):
  139. data = request.data
  140. res = {}
  141. if data['type'] == 0:
  142. # 岗位下删除这个用户
  143. Dept2Jobs.objects.filter(job_id=data['job_id'], user_id__in=data['user_ids']).delete()
  144. res = {'message': '删除用户'}
  145. elif data['type'] == 1:
  146. # 岗位下新增用户
  147. for user_id in data['user_ids']:
  148. Dept2Jobs.objects.create(job_id=data['job_id'], user_id=user_id)
  149. res = {'message': '新增用户'}
  150. return Response(data=res, status=status.HTTP_200_OK)
  151. def delete(self, request):
  152. pass