mixins.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. from __future__ import unicode_literals, print_function
  2. from django.core.exceptions import ValidationError
  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. if serializer.is_valid():
  27. for obj in serializer.object:
  28. self.pre_save(obj)
  29. self.object = serializer.save(force_insert=True)
  30. for obj in self.object:
  31. self.post_save(obj, created=True)
  32. return Response(serializer.data, status=status.HTTP_201_CREATED)
  33. return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
  34. class BulkUpdateModelMixin(object):
  35. """
  36. Update model instances in bulk by using the Serializers
  37. ``many=True`` ability from Django REST >= 2.2.5.
  38. """
  39. def get_object(self, queryset=None):
  40. lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field
  41. if any((lookup_url_kwarg in self.kwargs,
  42. self.pk_url_kwarg in self.kwargs,
  43. self.slug_url_kwarg in self.kwargs)):
  44. return super(BulkUpdateModelMixin, self).get_object(queryset)
  45. # If the lookup_url_kwarg (or other deprecated variations)
  46. # are not present, get_object() is most likely called
  47. # as part of metadata() which by default simply checks
  48. # for object permissions and raises permission denied if necessary.
  49. # Here we don't need to check for general permissions
  50. # and can simply return None since general permissions
  51. # are checked in initial() which always gets executed
  52. # before any of the API actions (e.g. create, update, etc)
  53. return
  54. def bulk_update(self, request, *args, **kwargs):
  55. partial = kwargs.pop('partial', False)
  56. # restrict the update to the filtered queryset
  57. serializer = self.get_serializer(self.filter_queryset(self.get_queryset()),
  58. data=request.DATA,
  59. many=True,
  60. partial=partial)
  61. if serializer.is_valid():
  62. try:
  63. for obj in serializer.object:
  64. self.pre_save(obj)
  65. except ValidationError as err:
  66. # full_clean on model instances may be called in pre_save
  67. # so we have to handle eventual errors.
  68. return Response(err.message_dict, status=status.HTTP_400_BAD_REQUEST)
  69. self.object = serializer.save(force_update=True)
  70. for obj in self.object:
  71. self.post_save(obj, created=False)
  72. return Response(serializer.data, status=status.HTTP_200_OK)
  73. return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
  74. def partial_bulk_update(self, request, *args, **kwargs):
  75. kwargs['partial'] = True
  76. return self.bulk_update(request, *args, **kwargs)
  77. class BulkDestroyModelMixin(object):
  78. """
  79. Destroy model instances.
  80. """
  81. def allow_bulk_destroy(self, qs, filtered):
  82. """
  83. Hook to ensure that the bulk destroy should be allowed.
  84. By default this checks that the destroy is only applied to
  85. filtered querysets.
  86. """
  87. return qs is not filtered
  88. def bulk_destroy(self, request, *args, **kwargs):
  89. qs = self.get_queryset()
  90. filtered = self.filter_queryset(qs)
  91. if not self.allow_bulk_destroy(qs, filtered):
  92. return Response(status=status.HTTP_400_BAD_REQUEST)
  93. for obj in filtered:
  94. self.pre_delete(obj)
  95. obj.delete()
  96. self.post_delete(obj)
  97. return Response(status=status.HTTP_204_NO_CONTENT)