From 358c59f6d75d90892732d8ff5860feaa6dbf3bb5 Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Wed, 26 Feb 2020 14:20:52 +0200 Subject: [PATCH] Item: add bulk_get and a note about bulk creating. --- django_etesync/views.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/django_etesync/views.py b/django_etesync/views.py index 4c744bd..9150db0 100644 --- a/django_etesync/views.py +++ b/django_etesync/views.py @@ -123,6 +123,8 @@ class CollectionItemViewSet(BaseViewSet): def create(self, request, collection_uid=None): 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) serializer = self.serializer_class(data=request.data, many=many) if serializer.is_valid(): @@ -156,6 +158,16 @@ class CollectionItemViewSet(BaseViewSet): serializer = CollectionItemRevisionSerializer(col_it.revisions.order_by('-id'), many=True) 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): allowed_methods = ['GET', 'POST']