Commit d0565ea7 by mikedikan Committed by GitHub

Merge pull request #386 from edx/mdikan/add-filter-for-unpublished-programs-to-course-run-endpoint

Exclude unpublished programs from the course run endpoint
parents 94d2e543 17b1de7c
...@@ -389,6 +389,9 @@ class CourseRunWithProgramsSerializer(CourseRunSerializer): ...@@ -389,6 +389,9 @@ class CourseRunWithProgramsSerializer(CourseRunSerializer):
if (self.context.get('include_deleted_programs') or if (self.context.get('include_deleted_programs') or
program.status != ProgramStatus.Deleted) and program.status != ProgramStatus.Deleted) and
obj.id not in (run.id for run in program.excluded_course_runs.all())] 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 return NestedProgramSerializer(programs, many=True).data
......
...@@ -323,6 +323,27 @@ class CourseRunWithProgramsSerializerTests(TestCase): ...@@ -323,6 +323,27 @@ class CourseRunWithProgramsSerializerTests(TestCase):
NestedProgramSerializer([deleted_program], many=True, context=self.serializer_context).data 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 class FlattenedCourseRunWithCourseSerializerTests(TestCase): # pragma: no cover
def serialize_seats(self, course_run): def serialize_seats(self, course_run):
......
...@@ -340,6 +340,7 @@ class CourseRunViewSet(viewsets.ReadOnlyModelViewSet): ...@@ -340,6 +340,7 @@ class CourseRunViewSet(viewsets.ReadOnlyModelViewSet):
context.update({ context.update({
'exclude_utm': get_query_param(self.request, 'exclude_utm'), 'exclude_utm': get_query_param(self.request, 'exclude_utm'),
'include_deleted_programs': get_query_param(self.request, 'include_deleted_programs'), 'include_deleted_programs': get_query_param(self.request, 'include_deleted_programs'),
'include_unpublished_programs': get_query_param(self.request, 'include_unpublished_programs'),
}) })
return context return context
...@@ -392,6 +393,12 @@ class CourseRunViewSet(viewsets.ReadOnlyModelViewSet): ...@@ -392,6 +393,12 @@ class CourseRunViewSet(viewsets.ReadOnlyModelViewSet):
type: integer type: integer
paramType: query paramType: query
multiple: false 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) return super(CourseRunViewSet, self).list(request, *args, **kwargs)
......
...@@ -241,7 +241,7 @@ class ProgramFactory(factory.django.DjangoModelFactory): ...@@ -241,7 +241,7 @@ class ProgramFactory(factory.django.DjangoModelFactory):
uuid = factory.LazyFunction(uuid4) uuid = factory.LazyFunction(uuid4)
subtitle = FuzzyText() subtitle = FuzzyText()
type = factory.SubFactory(ProgramTypeFactory) type = factory.SubFactory(ProgramTypeFactory)
status = ProgramStatus.Unpublished status = ProgramStatus.Active
marketing_slug = factory.Sequence(lambda n: 'test-slug-{}'.format(n)) # pylint: disable=unnecessary-lambda marketing_slug = factory.Sequence(lambda n: 'test-slug-{}'.format(n)) # pylint: disable=unnecessary-lambda
banner_image_url = FuzzyText(prefix='https://example.com/program/banner') banner_image_url = FuzzyText(prefix='https://example.com/program/banner')
card_image_url = FuzzyText(prefix='https://example.com/program/card') 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