Skip to content

Commit

Permalink
Add pagination class for the Messaging Stats viewset
Browse files Browse the repository at this point in the history
  • Loading branch information
DavisRayM committed Mar 13, 2023
1 parent 0257f40 commit ff8d4b4
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions onadata/apps/api/viewsets/v2/messaging_stats_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
TargetTypeFilterBackend,
)
from onadata.apps.messaging.permissions import TargetObjectPermissions
from onadata.libs.pagination import StandardPageNumberPagination


class MessagingStatsViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
Expand Down Expand Up @@ -51,13 +52,15 @@ class MessagingStatsViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
queryset = Action.objects.filter(verb__in=MESSAGE_VERBS)
permission_classes = [IsAuthenticated, TargetObjectPermissions]
filter_backends = [TargetTypeFilterBackend, TargetIDFilterBackend]
pagination_class = StandardPageNumberPagination

def _stream_annotated_queryset(self, queryset):
yield "["
group = None
prev_group = None
out = {}
for item in queryset.iterator():
iterator = iter(queryset) if isinstance(queryset, list) else queryset.iterator()
for item in iterator:
if group is None:
group = item.get("group")
elif group != item.get("group"):
Expand Down Expand Up @@ -94,7 +97,14 @@ def list(self, request, *args, **kwargs):
queryset = self._generate_annotated_queryset(
request, self.filter_queryset(self.get_queryset())
)
response = StreamingHttpResponse( # pylint: disable=http-response-with-content-type-json
self._stream_annotated_queryset(queryset), content_type="application/json"
page = self.paginate_queryset(queryset)
headers = {}
if page:
stream = self._stream_annotated_queryset(page)
headers = self.paginator.generate_link_header(request, page)
else:
stream = self._stream_annotated_queryset(queryset)
response = StreamingHttpResponse(
stream, content_type="application/json", headers=headers
)
return response

0 comments on commit ff8d4b4

Please sign in to comment.