Skip to content

Commit

Permalink
Add prerequisites to courses API (#2321)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
cp-at-mit and pre-commit-ci[bot] authored Aug 5, 2024
1 parent 152791d commit 4998810
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
14 changes: 14 additions & 0 deletions courses/serializers/v2/courses.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ class CourseSerializer(BaseCourseSerializer):
programs = serializers.SerializerMethodField()
topics = serializers.SerializerMethodField()
certificate_type = serializers.SerializerMethodField()
required_prerequisites = serializers.SerializerMethodField()

def get_required_prerequisites(self, instance):
"""
Check if the prerequisites field is populated in the course page CMS.
Returns:
bool: True when the prerequisites field is populated in the course page CMS. False otherwise.
"""
return bool(
hasattr(instance, "page")
and hasattr(instance.page, "prerequisites")
and instance.page.prerequisites != ""
)

def get_next_run_id(self, instance):
"""Get next run id"""
Expand Down Expand Up @@ -74,6 +87,7 @@ class Meta:
"programs",
"topics",
"certificate_type",
"required_prerequisites",
]


Expand Down
36 changes: 36 additions & 0 deletions courses/serializers/v2/courses_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from cms.serializers import CoursePageSerializer
from courses.factories import (
CourseFactory,
CourseRunEnrollmentFactory,
CourseRunFactory,
ProgramFactory,
Expand Down Expand Up @@ -66,8 +67,43 @@ def test_serialize_course(
"page": CoursePageSerializer(course.page).data,
"certificate_type": certificate_type,
"topics": [{"name": topic.name} for topic in topics],
"required_prerequisites": True,
"programs": BaseProgramSerializer(course.programs, many=True).data
if all_runs
else None,
},
)


@pytest.mark.parametrize("prerequisites_cms_value", ["mock value", None, ""])
def test_serialize_course_required_prerequisites(
mocker, mock_context, prerequisites_cms_value, settings
):
"""Test Course serialization to ensure that required_prerequisites is set to True if prerequisites is defined in the CMS and no an empty string, otherwise False"""
course = CourseFactory.create()
expected_required_prerequisites = False
if prerequisites_cms_value is not None:
# When prerequisites_cms_value is None, the course page has been created but prerequisites has never been populated.
# If the prerequisites have previously been populated but are now empty, the value of prerequisites will be an empty string.
course.page.prerequisites = prerequisites_cms_value
if prerequisites_cms_value != "":
expected_required_prerequisites = True

data = CourseWithCourseRunsSerializer(instance=course, context=mock_context).data

assert_drf_json_equal(
data,
{
"title": course.title,
"readable_id": course.readable_id,
"id": course.id,
"courseruns": [],
"next_run_id": None,
"departments": [],
"page": CoursePageSerializer(course.page).data,
"certificate_type": "Certificate of Completion",
"topics": [],
"required_prerequisites": expected_required_prerequisites,
"programs": None,
},
)

0 comments on commit 4998810

Please sign in to comment.