Commit 17b1de7c by Mike Dikan

Exclude unpublished programs from the course run endpoint

We currently include unpublished programs on the course run endpoint by default.  This change will change that default behavior
so that unpublished programs aren't presented unless the 'include_unpublished_programs' flag is set as a query string parameter.

ECOM-6013
parent 94d2e543
......@@ -389,6 +389,9 @@ class CourseRunWithProgramsSerializer(CourseRunSerializer):
if (self.context.get('include_deleted_programs') or
program.status != ProgramStatus.Deleted) and
obj.id not in (run.id for run in program.excluded_course_runs.all())]
# If flag is not set, remove programs from list that are unpublished
if not self.context.get('include_unpublished_programs'):
programs = [program for program in programs if program.status != ProgramStatus.Unpublished]
return NestedProgramSerializer(programs, many=True).data
......
......@@ -323,6 +323,27 @@ class CourseRunWithProgramsSerializerTests(TestCase):
NestedProgramSerializer([deleted_program], many=True, context=self.serializer_context).data
)
def test_exclude_unpublished_program(self):
"""
If a program is unpublished, that program should not be returned on the course run endpoint by default.
"""
ProgramFactory(courses=[self.course_run.course], status=ProgramStatus.Unpublished)
serializer = CourseRunWithProgramsSerializer(self.course_run, context=self.serializer_context)
self.assertEqual(serializer.data['programs'], [])
def test_include_unpublished_programs(self):
"""
If a program is unpublished, that program should only be returned on the course run endpoint if we are
sending the 'include_unpublished_programs' flag.
"""
unpublished_program = ProgramFactory(courses=[self.course_run.course], status=ProgramStatus.Unpublished)
self.serializer_context['include_unpublished_programs'] = 1
serializer = CourseRunWithProgramsSerializer(self.course_run, context=self.serializer_context)
self.assertEqual(
serializer.data['programs'],
NestedProgramSerializer([unpublished_program], many=True, context=self.serializer_context).data
)
class FlattenedCourseRunWithCourseSerializerTests(TestCase): # pragma: no cover
def serialize_seats(self, course_run):
......
......@@ -340,6 +340,7 @@ class CourseRunViewSet(viewsets.ReadOnlyModelViewSet):
context.update({
'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'),
})
return context
......@@ -392,6 +393,12 @@ class CourseRunViewSet(viewsets.ReadOnlyModelViewSet):
type: integer
paramType: query
multiple: false
- name: include_unpublished_programs
description: Will include unpublished programs in the associated programs array
required: false
type: integer
paramType: query
multiple: false
"""
return super(CourseRunViewSet, self).list(request, *args, **kwargs)
......
......@@ -241,7 +241,7 @@ class ProgramFactory(factory.django.DjangoModelFactory):
uuid = factory.LazyFunction(uuid4)
subtitle = FuzzyText()
type = factory.SubFactory(ProgramTypeFactory)
status = ProgramStatus.Unpublished
status = ProgramStatus.Active
marketing_slug = factory.Sequence(lambda n: 'test-slug-{}'.format(n)) # pylint: disable=unnecessary-lambda
banner_image_url = FuzzyText(prefix='https://example.com/program/banner')
card_image_url = FuzzyText(prefix='https://example.com/program/card')
......
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