Commit af9351cc by Afzal Wali Committed by Douglas Hall

Use CourseRunSerializer over MinimalCourseRunSerializer when…

Use CourseRunSerializer over MinimalCourseRunSerializer when use_full_course_serializer is passed to the programs api.
parent cff1247d
...@@ -559,7 +559,11 @@ class MinimalProgramCourseSerializer(MinimalCourseSerializer): ...@@ -559,7 +559,11 @@ class MinimalProgramCourseSerializer(MinimalCourseSerializer):
if self.context.get('published_course_runs_only'): if self.context.get('published_course_runs_only'):
course_runs = [course_run for course_run in course_runs if course_run.status == CourseRunStatus.Published] course_runs = [course_run for course_run in course_runs if course_run.status == CourseRunStatus.Published]
return MinimalCourseRunSerializer( serializer_class = MinimalCourseRunSerializer
if self.context.get('use_full_course_serializer', False):
serializer_class = CourseRunSerializer
return serializer_class(
course_runs, course_runs,
many=True, many=True,
context={ context={
...@@ -610,6 +614,7 @@ class MinimalProgramSerializer(serializers.ModelSerializer): ...@@ -610,6 +614,7 @@ class MinimalProgramSerializer(serializers.ModelSerializer):
'exclude_utm': self.context.get('exclude_utm'), 'exclude_utm': self.context.get('exclude_utm'),
'program': program, 'program': program,
'course_runs': course_runs, 'course_runs': course_runs,
'use_full_course_serializer': self.context.get('use_full_course_serializer', False),
} }
) )
......
...@@ -832,6 +832,35 @@ class ProgramSerializerTests(MinimalProgramSerializerTests): ...@@ -832,6 +832,35 @@ class ProgramSerializerTests(MinimalProgramSerializerTests):
expected = self.get_expected_data(program, request) expected = self.get_expected_data(program, request)
self.assertDictEqual(serializer.data, expected) self.assertDictEqual(serializer.data, expected)
def test_use_full_course_serializer(self):
"""
Verify that we can use the `use_full_course_serializer` parameter to toggle the CourseRun
serializer used when returning program data.
"""
request = make_request()
program = self.create_program()
# Assert that the seats are not available when fetched without any flag
program_without_seats = self.serializer_class(program, context={'request': request}).data
course_run_without_seats = program_without_seats['courses'][0]['course_runs'][0]
assert 'seats' not in course_run_without_seats
# Assert that the seats are not available when fetched with the use_full_course_serializer param set to 0
program_without_seats = self.serializer_class(program, context={
'request': request,
'use_full_course_serializer': 0
}).data
course_run_without_seats = program_without_seats['courses'][0]['course_runs'][0]
assert 'seats' not in course_run_without_seats
# Assert that the seats are available when fetched with the use_full_course_serializer param set to 1
program_with_seats = self.serializer_class(
program,
context={'request': request, 'use_full_course_serializer': 1}
).data
course_run_with_seats = program_with_seats['courses'][0]['course_runs'][0]
assert 'seats' in course_run_with_seats
class ProgramTypeSerializerTests(TestCase): class ProgramTypeSerializerTests(TestCase):
serializer_class = ProgramTypeSerializer serializer_class = ProgramTypeSerializer
......
...@@ -37,6 +37,7 @@ class ProgramViewSet(viewsets.ReadOnlyModelViewSet): ...@@ -37,6 +37,7 @@ class ProgramViewSet(viewsets.ReadOnlyModelViewSet):
context.update({ context.update({
'published_course_runs_only': get_query_param(self.request, 'published_course_runs_only'), 'published_course_runs_only': get_query_param(self.request, 'published_course_runs_only'),
'exclude_utm': get_query_param(self.request, 'exclude_utm'), 'exclude_utm': get_query_param(self.request, 'exclude_utm'),
'use_full_course_serializer': get_query_param(self.request, 'use_full_course_serializer'),
}) })
return context return context
...@@ -70,6 +71,12 @@ class ProgramViewSet(viewsets.ReadOnlyModelViewSet): ...@@ -70,6 +71,12 @@ class ProgramViewSet(viewsets.ReadOnlyModelViewSet):
type: integer type: integer
paramType: query paramType: query
multiple: false multiple: false
- name: use_full_course_serializer
description: Return all serialized course information instead of a minimal amount of information.
required: false
type: integer
paramType: query
multiple: false
""" """
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