Skip to content

Commit

Permalink
remove channel labels from non-public ContentNode API
Browse files Browse the repository at this point in the history
  • Loading branch information
Pulkitxm committed Nov 14, 2024
1 parent 458bd60 commit e6fc051
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 21 deletions.
57 changes: 42 additions & 15 deletions kolibri/core/content/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -828,40 +828,55 @@ 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")
class ContentNodeViewset(InternalContentNodeMixin, RemoteMixin, ReadOnlyValuesViewset):
Expand Down Expand Up @@ -950,6 +965,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
Expand Down
18 changes: 12 additions & 6 deletions kolibri/core/content/utils/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,20 @@ 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):
Expand Down

0 comments on commit e6fc051

Please sign in to comment.