Commit e46a8dfd by tasawernawaz Committed by Tasawer Nawaz

exclude retired programs from course run api

LEARNER-2281
parent c0faf2f2
......@@ -502,6 +502,10 @@ class CourseRunWithProgramsSerializer(CourseRunSerializer):
if not self.context.get('include_unpublished_programs'):
programs = [program for program in programs if program.status != ProgramStatus.Unpublished]
# If flag is not set, remove programs from list that are retired
if not self.context.get('include_retired_programs'):
programs = [program for program in programs if program.status != ProgramStatus.Retired]
return NestedProgramSerializer(programs, many=True).data
class Meta(CourseRunSerializer.Meta):
......
......@@ -359,6 +359,27 @@ class CourseRunWithProgramsSerializerTests(TestCase):
NestedProgramSerializer([unpublished_program], many=True, context=self.serializer_context).data
)
def test_exclude_retired_program(self):
"""
If a program is retired, that program should not be returned on the course run endpoint by default.
"""
ProgramFactory(courses=[self.course_run.course], status=ProgramStatus.Retired)
serializer = CourseRunWithProgramsSerializer(self.course_run, context=self.serializer_context)
self.assertEqual(serializer.data['programs'], [])
def test_include_retired_programs(self):
"""
If a program is retired, that program should only be returned on the course run endpoint if we are
sending the 'include_retired_programs' flag.
"""
retired_program = ProgramFactory(courses=[self.course_run.course], status=ProgramStatus.Retired)
self.serializer_context['include_retired_programs'] = 1
serializer = CourseRunWithProgramsSerializer(self.course_run, context=self.serializer_context)
self.assertEqual(
serializer.data['programs'],
NestedProgramSerializer([retired_program], many=True, context=self.serializer_context).data
)
class FlattenedCourseRunWithCourseSerializerTests(TestCase): # pragma: no cover
def serialize_seats(self, course_run):
......
......@@ -59,6 +59,7 @@ class CourseRunViewSet(viewsets.ModelViewSet):
'exclude_utm': get_query_param(self.request, 'exclude_utm'),
'include_deleted_programs': get_query_param(self.request, 'include_deleted_programs'),
'include_unpublished_programs': get_query_param(self.request, 'include_unpublished_programs'),
'include_retired_programs': get_query_param(self.request, 'include_retired_programs'),
})
return context
......@@ -117,6 +118,12 @@ class CourseRunViewSet(viewsets.ModelViewSet):
type: integer
paramType: query
multiple: false
- name: include_retired_programs
description: Will include retired programs in the associated programs array
required: false
type: integer
paramType: query
multiple: false
"""
return super(CourseRunViewSet, 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