Skip to content

Commit

Permalink
stream response on endpoint /<username>/xformsManifest/<form_id>
Browse files Browse the repository at this point in the history
stream response to avoid serialization using too much memory
  • Loading branch information
kelvin-muchiri committed Oct 25, 2023
1 parent c8c0ac9 commit 9b0490c
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions onadata/apps/api/viewsets/xform_list_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
OpenRosa Form List API - https://docs.getodk.org/openrosa-form-list/
"""
from django.conf import settings
from django.http import Http404
from django.http import Http404, StreamingHttpResponse
from django.shortcuts import get_object_or_404
from django.views.decorators.cache import never_cache

Expand Down Expand Up @@ -31,6 +31,7 @@
)
from onadata.libs.utils.common_tags import GROUP_DELIMETER_TAG, REPEAT_INDEX_TAGS
from onadata.libs.utils.export_builder import ExportBuilder
from onadata.libs.utils.model_tools import queryset_iterator

BaseViewset = get_baseviewset_class()

Expand Down Expand Up @@ -167,10 +168,15 @@ def manifest(self, request, *args, **kwargs):
context = self.get_serializer_context()
context[GROUP_DELIMETER_TAG] = ExportBuilder.GROUP_DELIMITER_DOT
context[REPEAT_INDEX_TAGS] = "_,_"
serializer = XFormManifestSerializer(object_list, many=True, context=context)

return Response(
serializer.data, headers=get_openrosa_headers(request, location=False)
def serialize_data():
for obj in queryset_iterator(object_list, chunksize=20):
serializer = XFormManifestSerializer(obj, context=context)
yield serializer.data

return StreamingHttpResponse(
serialize_data(),
headers=get_openrosa_headers(request, location=False),
)

@action(methods=["GET", "HEAD"], detail=True)
Expand Down

0 comments on commit 9b0490c

Please sign in to comment.