Item: add bulk_get and a note about bulk creating.

This commit is contained in:
Tom Hacohen 2020-02-26 14:20:52 +02:00
parent d6df94facf
commit 358c59f6d7

View File

@ -123,6 +123,8 @@ class CollectionItemViewSet(BaseViewSet):
def create(self, request, collection_uid=None): def create(self, request, collection_uid=None):
collection_object = get_object_or_404(self.get_collection_queryset(Collection.objects), uid=collection_uid) collection_object = get_object_or_404(self.get_collection_queryset(Collection.objects), uid=collection_uid)
# FIXME: change this to also support bulk update, or have another endpoint for that.
# See https://www.django-rest-framework.org/api-guide/serializers/#customizing-multiple-update
many = isinstance(request.data, list) many = isinstance(request.data, list)
serializer = self.serializer_class(data=request.data, many=many) serializer = self.serializer_class(data=request.data, many=many)
if serializer.is_valid(): if serializer.is_valid():
@ -156,6 +158,16 @@ class CollectionItemViewSet(BaseViewSet):
serializer = CollectionItemRevisionSerializer(col_it.revisions.order_by('-id'), many=True) serializer = CollectionItemRevisionSerializer(col_it.revisions.order_by('-id'), many=True)
return Response(serializer.data) return Response(serializer.data)
@action_decorator(detail=False, methods=['POST'])
def bulk_get(self, request, collection_uid=None):
queryset = self.get_queryset()
if isinstance(request.data, list):
queryset = queryset.filter(uid__in=request.data)
serializer = self.get_serializer_class()(queryset, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
class CollectionItemChunkViewSet(viewsets.ViewSet): class CollectionItemChunkViewSet(viewsets.ViewSet):
allowed_methods = ['GET', 'POST'] allowed_methods = ['GET', 'POST']