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