views.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. from django.shortcuts import render
  2. # Create your views here.
  3. from rest_framework import status, permissions
  4. from rest_framework.decorators import permission_classes
  5. from rest_framework.views import APIView
  6. from rest_framework.viewsets import ModelViewSet
  7. from relation.serializer import *
  8. from rest_framework.response import Response
  9. class DaptViews(APIView):
  10. def get(self, request):
  11. parent_id = request.GET.get('parent_id')
  12. daptSet = Dapt.objects.filter(parent_id = parent_id).values()
  13. return Response(data=daptSet, status=status.HTTP_200_OK)
  14. def post(self, request):
  15. dapt = Dapt()
  16. data = request.data
  17. dapt.setObject(data)
  18. dapt.save()
  19. return Response(data=dapt.id, status=status.HTTP_200_OK)
  20. def delete(self, request):
  21. id = request.data['id']
  22. Dapt.objects.filter(id = id).delete()
  23. return Response(data=True, status=status.HTTP_200_OK)
  24. class PostViews(ModelViewSet):
  25. queryset = Post.objects.all()
  26. serializer_class = PostSerializer
  27. class UserViews(APIView):
  28. def post(self, request):
  29. data = request.data
  30. user = User(last_name = data['last_name'], id=data['id'])
  31. user.save()
  32. return Response(data=True, status=status.HTTP_200_OK)
  33. def get(self, request):
  34. user = User.objects.values()
  35. return Response(data=user, status=status.HTTP_200_OK)
  36. # class DaptPostViews(APIView):
  37. # def get(self, request):
  38. # get = request.GET
  39. # return Response(status=status.HTTP_200_OK)
  40. #部门人员
  41. class DaptUserViews(APIView):
  42. def get(self, request):
  43. daptId = request.GET.get('dapt_id')
  44. userId = request.GET.get('user_id')
  45. if daptId != None:
  46. daptUserList = list(DaptUser.objects.filter(dapt_id=daptId))
  47. userList = list()
  48. for daptUser in daptUserList:
  49. userList.append(daptUser.user)
  50. data = UserSerializer(userList, many=True)
  51. elif userId != None:
  52. daptUserList = list(DaptUser.objects.filter(user_id=userId))
  53. daptList = list()
  54. for daptUser in daptUserList:
  55. daptList.append(daptUser.dapt)
  56. data = DaptSerializer(daptList, many=True)
  57. return Response(data=data.data, status=status.HTTP_200_OK)
  58. def post(self, reques):
  59. data = reques.data
  60. daptUser = DaptUser()
  61. daptUser.setObject(data)
  62. daptUser.save()
  63. return Response(data=True, status=status.HTTP_200_OK)
  64. def delete(self, reques):
  65. userId = reques.data['user_id']
  66. daptId = reques.data['dapt_id']
  67. DaptUser.objects.get(user_id = userId, dapt_id=daptId).delete()
  68. return Response(data=True, status=status.HTTP_200_OK)