From 49988101b244091605bc211ee34ee4ddf81bee63 Mon Sep 17 00:00:00 2001 From: cp-at-mit Date: Mon, 5 Aug 2024 13:44:22 -0400 Subject: [PATCH] Add prerequisites to courses API (#2321) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- courses/serializers/v2/courses.py | 14 ++++++++++ courses/serializers/v2/courses_test.py | 36 ++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/courses/serializers/v2/courses.py b/courses/serializers/v2/courses.py index d099bc08e8..6ea3ddf47c 100644 --- a/courses/serializers/v2/courses.py +++ b/courses/serializers/v2/courses.py @@ -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""" @@ -74,6 +87,7 @@ class Meta: "programs", "topics", "certificate_type", + "required_prerequisites", ] diff --git a/courses/serializers/v2/courses_test.py b/courses/serializers/v2/courses_test.py index 538f4bf522..d5530e0a38 100644 --- a/courses/serializers/v2/courses_test.py +++ b/courses/serializers/v2/courses_test.py @@ -3,6 +3,7 @@ from cms.serializers import CoursePageSerializer from courses.factories import ( + CourseFactory, CourseRunEnrollmentFactory, CourseRunFactory, ProgramFactory, @@ -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, + }, + )