mixins.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. from __future__ import print_function, unicode_literals
  2. import traceback
  3. from rest_framework import status
  4. from rest_framework.mixins import CreateModelMixin
  5. from rest_framework.response import Response
  6. __all__ = [
  7. 'BulkCreateModelMixin',
  8. 'BulkDestroyModelMixin',
  9. 'BulkUpdateModelMixin',
  10. ]
  11. class BulkCreateModelMixin(CreateModelMixin):
  12. """
  13. Either create a single or many model instances in bulk by using the
  14. Serializers ``many=True`` ability from Django REST >= 2.2.5.
  15. .. note::
  16. This mixin uses the same method to create model instances
  17. as ``CreateModelMixin`` because both non-bulk and bulk
  18. requests will use ``POST`` request method.
  19. """
  20. def create(self, request, *args, **kwargs):
  21. bulk = isinstance(request.data, list)
  22. if not bulk:
  23. return super(BulkCreateModelMixin, self).create(request, *args, **kwargs)
  24. else:
  25. serializer = self.get_serializer(data=request.data, many=True)
  26. serializer.is_valid(raise_exception=True)
  27. self.perform_bulk_create(serializer)
  28. return Response(serializer.data, status=status.HTTP_201_CREATED)
  29. def perform_bulk_create(self, serializer):
  30. return self.perform_create(serializer)
  31. class BulkUpdateModelMixin(object):
  32. """
  33. Update model instances in bulk by using the Serializers
  34. ``many=True`` ability from Django REST >= 2.2.5.
  35. """
  36. def get_object(self):
  37. lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field
  38. if lookup_url_kwarg in self.kwargs:
  39. return super(BulkUpdateModelMixin, self).get_object()
  40. # If the lookup_url_kwarg is not present
  41. # get_object() is most likely called as part of options()
  42. # which by default simply checks for object permissions
  43. # and raises permission denied if necessary.
  44. # Here we don't need to check for general permissions
  45. # and can simply return None since general permissions
  46. # are checked in initial() which always gets executed
  47. # before any of the API actions (e.g. create, update, etc)
  48. return
  49. def bulk_update(self, request, *args, **kwargs):
  50. partial = kwargs.pop('partial', False)
  51. # restrict the update to the filtered queryset
  52. serializer = self.get_serializer(
  53. self.filter_queryset(self.get_queryset()),
  54. data=request.data,
  55. many=True,
  56. partial=partial,
  57. )
  58. serializer.is_valid(raise_exception=True)
  59. self.perform_bulk_update(serializer)
  60. return Response(serializer.data, status=status.HTTP_200_OK)
  61. def partial_bulk_update(self, request, *args, **kwargs):
  62. kwargs['partial'] = True
  63. return self.bulk_update(request, *args, **kwargs)
  64. def perform_update(self, serializer):
  65. serializer.save()
  66. def perform_bulk_update(self, serializer):
  67. return self.perform_update(serializer)
  68. class BulkDestroyModelMixin(object):
  69. """
  70. Destroy model instances.
  71. """
  72. def allow_bulk_destroy(self, qs, filtered):
  73. """
  74. Hook to ensure that the bulk destroy should be allowed.
  75. By default this checks that the destroy is only applied to
  76. filtered querysets.
  77. """
  78. return qs is not filtered
  79. def bulk_destroy(self, request, *args, **kwargs):
  80. qs = self.get_queryset()
  81. filtered = self.filter_queryset(qs)
  82. if not self.allow_bulk_destroy(qs, filtered):
  83. return Response(status=status.HTTP_400_BAD_REQUEST)
  84. self.perform_bulk_destroy(filtered)
  85. return Response(status=status.HTTP_204_NO_CONTENT)
  86. def perform_destroy(self, instance):
  87. instance.delete()
  88. def perform_bulk_destroy(self, objects):
  89. for obj in objects:
  90. self.perform_destroy(obj)