Commit 242db082 by Renzo Lucioni

Add option to serialize only program UUIDs

ProgramViewSet.list now returns a flat list of program UUIDs when `uuids_only=1` is included in the querystring. This allows clients which cache each program in an individual entry (e.g., LMS) to more quickly identify the programs they need to get from their cache.

LEARNER-382
parent 9c487206
...@@ -137,6 +137,28 @@ class ProgramViewSetTests(SerializationMixin, APITestCase): ...@@ -137,6 +137,28 @@ class ProgramViewSetTests(SerializationMixin, APITestCase):
# Verify that repeated list requests use the cache. # Verify that repeated list requests use the cache.
self.assert_list_results(self.list_path, expected, 2) self.assert_list_results(self.list_path, expected, 2)
def test_uuids_only(self):
"""
Verify that the list view returns a simply list of UUIDs when the
uuids_only query parameter is passed.
"""
active = ProgramFactory.create_batch(3)
retired = [ProgramFactory(status=ProgramStatus.Retired)]
programs = active + retired
querystring = {'uuids_only': 1}
url = '{base}?{query}'.format(base=self.list_path, query=urllib.parse.urlencode(querystring))
response = self.client.get(url)
assert set(response.data) == {program.uuid for program in programs}
# Verify that filtering (e.g., by status) is still supported.
querystring['status'] = ProgramStatus.Retired
url = '{base}?{query}'.format(base=self.list_path, query=urllib.parse.urlencode(querystring))
response = self.client.get(url)
assert set(response.data) == {program.uuid for program in retired}
def test_filter_by_type(self): def test_filter_by_type(self):
""" Verify that the endpoint filters programs to those of a given type. """ """ Verify that the endpoint filters programs to those of a given type. """
program_type_name = 'foo' program_type_name = 'foo'
......
from rest_framework import mixins, viewsets from rest_framework import mixins, viewsets
from rest_framework.filters import DjangoFilterBackend from rest_framework.filters import DjangoFilterBackend
from rest_framework.permissions import IsAuthenticated from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework_extensions.cache.mixins import CacheResponseMixin from rest_framework_extensions.cache.mixins import CacheResponseMixin
from course_discovery.apps.api import filters, serializers from course_discovery.apps.api import filters, serializers
from course_discovery.apps.api.pagination import ProxiedPagination from course_discovery.apps.api.pagination import ProxiedPagination
from course_discovery.apps.api.v1.views import get_query_param from course_discovery.apps.api.v1.views import get_query_param
from course_discovery.apps.course_metadata.models import ProgramType from course_discovery.apps.course_metadata.models import Program, ProgramType
# pylint: disable=no-member # pylint: disable=no-member
...@@ -91,6 +92,14 @@ class ProgramViewSet(CacheResponseMixin, viewsets.ReadOnlyModelViewSet): ...@@ -91,6 +92,14 @@ class ProgramViewSet(CacheResponseMixin, viewsets.ReadOnlyModelViewSet):
paramType: query paramType: query
multiple: false multiple: false
""" """
if get_query_param(self.request, 'uuids_only'):
# DRF serializers don't have good support for simple, flat
# representations like the one we want here.
queryset = self.filter_queryset(Program.objects.all())
uuids = queryset.values_list('uuid', flat=True)
return Response(uuids)
return super(ProgramViewSet, self).list(request, *args, **kwargs) return super(ProgramViewSet, self).list(request, *args, **kwargs)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment