diff --git a/kolibri/core/content/api.py b/kolibri/core/content/api.py index d4331f3c5d5..76ecda6a28c 100644 --- a/kolibri/core/content/api.py +++ b/kolibri/core/content/api.py @@ -828,39 +828,56 @@ class OptionalContentNodePagination(OptionalPagination): def paginate_queryset(self, queryset, request, view=None): # Record the queryset for use in returning available filters self.queryset = queryset + self.use_deprecated_channels_labels = ( + request.query_params.get("use_deprecated_channels_labels", "false").lower() + == "true" + ) return super(OptionalContentNodePagination, self).paginate_queryset( queryset, request, view=view ) def get_paginated_response(self, data): + labels = get_available_metadata_labels(self.queryset) + if self.use_deprecated_channels_labels: + labels["channels"] = list( + self.queryset.values_list("channel_id", flat=True).distinct() + ) return Response( OrderedDict( [ ("more", self.get_more()), ("results", data), - ("labels", get_available_metadata_labels(self.queryset)), + ("labels", labels), ] ) ) def get_paginated_response_schema(self, schema): - return { - "type": "object", - "properties": { - "more": { - "type": "object", - "nullable": True, - "example": { - "cursor": "asdadshjashjadh", - }, - }, - "results": schema, - "labels": { - "type": "object", - "example": {"accessibility_labels": ["id1", "id2"]}, + properties = { + "more": { + "type": "object", + "nullable": True, + "example": { + "cursor": "asdadshjashjadh", }, }, + "results": schema, + "labels": { + "type": "object", + "example": {"accessibility_labels": ["id1", "id2"]}, + }, } + if self.use_deprecated_channels_labels: + properties["labels"]["example"]["channels"] = ["channel_id1", "channel_id2"] + return { + "type": "object", + "properties": properties, + } + + class DeprecatedChannelsLabelsPagination(OptionalContentNodePagination): + def paginate_queryset(self, queryset, request, view=None): + self.use_deprecated_channels_labels = True + return super().paginate_queryset(queryset, request, view) @method_decorator(remote_metadata_cache, name="dispatch") @@ -950,6 +967,18 @@ def recommendations_for(self, request, **kwargs): ) return Response(self.serialize(queryset)) + def get_paginated_response(self, data): + labels = get_available_metadata_labels(self.queryset) + return Response( + OrderedDict( + [ + ("more", self.get_more()), + ("results", data), + ("labels", labels), + ] + ) + ) + # The max recursed page size should be less than 25 for a couple of reasons: # 1. At this size the query appears to be relatively performant, and will deliver most of the tree diff --git a/kolibri/core/content/utils/search.py b/kolibri/core/content/utils/search.py index 1658744492d..1c6dea29e95 100644 --- a/kolibri/core/content/utils/search.py +++ b/kolibri/core/content/utils/search.py @@ -77,14 +77,22 @@ def _get_available_languages(base_queryset): return list(langs) -def _get_available_channels(base_queryset): +def _get_available_channels(base_queryset, include_labels=False): from kolibri.core.content.models import ChannelMetadata - return list( - ChannelMetadata.objects.filter( - id__in=base_queryset.values_list("channel_id", flat=True).distinct() - ).values("id", "name") - ) + channels = ChannelMetadata.objects.filter( + id__in=base_queryset.values_list("channel_id", flat=True).distinct() + ).values("id", "name") + + if include_labels: + for channel in channels: + channel["labels"] = list( + ChannelMetadata.objects.filter(id=channel["id"]).values_list( + "labels", flat=True + ) + ) + + return list(channels) class SQLiteBitwiseORAggregate(Aggregate):