views.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. from rest_framework import viewsets
  2. from .models import ListModel
  3. from . import serializers
  4. from utils.page import MyPageNumberPagination
  5. from rest_framework.filters import OrderingFilter
  6. from django_filters.rest_framework import DjangoFilterBackend
  7. from rest_framework.response import Response
  8. from .filter import Filter
  9. from rest_framework.exceptions import APIException
  10. class APIViewSet(viewsets.ModelViewSet):
  11. """
  12. retrieve:
  13. Response a data list(get)
  14. list:
  15. Response a data list(all)
  16. create:
  17. Create a data line(post)
  18. delete:
  19. Delete a data line(delete)
  20. partial_update:
  21. Partial_update a data(patch:partial_update)
  22. update:
  23. Update a data(put:update)
  24. """
  25. pagination_class = MyPageNumberPagination
  26. filter_backends = [DjangoFilterBackend, OrderingFilter, ]
  27. ordering_fields = ['id', "create_time", "update_time", ]
  28. filter_class = Filter
  29. def get_project(self):
  30. try:
  31. id = self.kwargs.get('pk')
  32. return id
  33. except:
  34. return None
  35. def get_queryset(self):
  36. id = self.get_project()
  37. if self.request.user:
  38. if id is None:
  39. return ListModel.objects.filter(openid=self.request.auth.openid, is_delete=False)
  40. else:
  41. return ListModel.objects.filter(openid=self.request.auth.openid, id=id, is_delete=False)
  42. else:
  43. return ListModel.objects.none()
  44. def get_serializer_class(self):
  45. if self.action in ['list', 'retrieve', 'destroy']:
  46. return serializers.WarehouseGetSerializer
  47. elif self.action in ['create']:
  48. return serializers.WarehousePostSerializer
  49. elif self.action in ['update']:
  50. return serializers.WarehouseUpdateSerializer
  51. elif self.action in ['partial_update']:
  52. return serializers.WarehousePartialUpdateSerializer
  53. else:
  54. return self.http_method_not_allowed(request=self.request)
  55. def create(self, request, *args, **kwargs):
  56. data = self.request.data
  57. data['openid'] = self.request.auth.openid
  58. if len(data['warehouse_name']) > 45:
  59. raise APIException({"detail": "The warehouse name is set to more than 45 characters"})
  60. if ListModel.objects.filter(openid=data['openid'], warehouse_name=data['warehouse_name'],
  61. is_delete=False).exists():
  62. raise APIException({"detail": "Data Exists"})
  63. else:
  64. if ListModel.objects.filter(openid=data['openid'], is_delete=False).count() >= 1:
  65. raise APIException({"detail": "You Just Can Add 1 Warehouse"})
  66. else:
  67. serializer = self.get_serializer(data=data)
  68. serializer.is_valid(raise_exception=True)
  69. serializer.save()
  70. headers = self.get_success_headers(serializer.data)
  71. return Response(serializer.data, status=200, headers=headers)
  72. def update(self, request, pk):
  73. qs = self.get_object()
  74. if qs.openid != self.request.auth.openid:
  75. raise APIException({"detail": "Cannot update data which not yours"})
  76. else:
  77. data = self.request.data
  78. if len(data['warehouse_name']) > 45:
  79. raise APIException({"detail": "The warehouse name is set to more than 45 characters"})
  80. serializer = self.get_serializer(qs, data=data)
  81. serializer.is_valid(raise_exception=True)
  82. serializer.save()
  83. headers = self.get_success_headers(serializer.data)
  84. return Response(serializer.data, status=200, headers=headers)
  85. def partial_update(self, request, pk):
  86. qs = self.get_object()
  87. if qs.openid != self.request.auth.openid:
  88. raise APIException({"detail": "Cannot partial_update data which not yours"})
  89. else:
  90. data = self.request.data
  91. if len(data['warehouse_name']) > 45:
  92. raise APIException({"detail": "The warehouse name is set to more than 45 characters"})
  93. serializer = self.get_serializer(qs, data=data, partial=True)
  94. serializer.is_valid(raise_exception=True)
  95. serializer.save()
  96. headers = self.get_success_headers(serializer.data)
  97. return Response(serializer.data, status=200, headers=headers)
  98. def destroy(self, request, pk):
  99. qs = self.get_object()
  100. if qs.openid != self.request.auth.openid:
  101. raise APIException({"detail": "Cannot delete data which not yours"})
  102. else:
  103. qs.is_delete = True
  104. qs.save()
  105. serializer = self.get_serializer(qs, many=False)
  106. headers = self.get_success_headers(serializer.data)
  107. return Response(serializer.data, status=200, headers=headers)