Skip to content

Commit

Permalink
Add certificate_type to courses and programs serializer (#2307)
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
annagav and pre-commit-ci[bot] authored Jul 25, 2024
1 parent 6bbb241 commit 6bc1b1f
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 4 deletions.
9 changes: 9 additions & 0 deletions courses/serializers/v2/courses.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class CourseSerializer(BaseCourseSerializer):
page = CoursePageSerializer(read_only=True)
programs = serializers.SerializerMethodField()
topics = serializers.SerializerMethodField()
certificate_type = serializers.SerializerMethodField()

def get_next_run_id(self, instance):
"""Get next run id"""
Expand All @@ -54,6 +55,13 @@ def get_topics(self, instance):
)
return []

def get_certificate_type(self, instance):
if instance.programs:
program = instance.programs[0]
if "MicroMasters" in program.program_type:
return "MicroMasters Credential"
return "Certificate of Completion"

class Meta:
model = models.Course
fields = [
Expand All @@ -65,6 +73,7 @@ class Meta:
"page",
"programs",
"topics",
"certificate_type",
]


Expand Down
73 changes: 73 additions & 0 deletions courses/serializers/v2/courses_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import pytest
from django.contrib.auth.models import AnonymousUser

from cms.serializers import CoursePageSerializer
from courses.factories import (
CourseRunEnrollmentFactory,
CourseRunFactory,
ProgramFactory,
)
from courses.models import CoursesTopic, Department
from courses.serializers.v1.base import BaseProgramSerializer
from courses.serializers.v2.courses import (
CourseRunSerializer,
CourseWithCourseRunsSerializer,
)
from main.test_utils import assert_drf_json_equal

pytestmark = [pytest.mark.django_db]


@pytest.mark.parametrize("is_anonymous", [True, False])
@pytest.mark.parametrize("all_runs", [True, False])
@pytest.mark.parametrize(
"certificate_type", ["MicroMasters Credential", "Certificate of Completion"]
)
def test_serialize_course(
mocker, mock_context, is_anonymous, all_runs, certificate_type
):
"""Test Course serialization"""
if is_anonymous:
mock_context["request"].user = AnonymousUser()
if all_runs:
mock_context["all_runs"] = True
user = mock_context["request"].user
courseRun1 = CourseRunFactory.create()
courseRun2 = CourseRunFactory.create(course=courseRun1.course)
course = courseRun1.course
topics = [CoursesTopic.objects.create(name=f"topic{num}") for num in range(3)]
course.page.topics.set([topics[0], topics[1], topics[2]])
department = "a course departments"
course.departments.set([Department.objects.create(name=department)])
program = ProgramFactory.create(program_type="Series")
if certificate_type == "MicroMasters Credential":
program.program_type = "MicroMasters®"
program.add_requirement(course)
program.save()

CourseRunEnrollmentFactory.create(
run=courseRun1, **({} if is_anonymous else {"user": user})
)

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": [
CourseRunSerializer(courseRun1).data,
CourseRunSerializer(courseRun2).data,
],
"next_run_id": course.first_unexpired_run.id,
"departments": [{"name": department}],
"page": CoursePageSerializer(course.page).data,
"certificate_type": certificate_type,
"topics": [{"name": topic.name} for topic in topics],
"programs": BaseProgramSerializer(course.programs, many=True).data
if all_runs
else None,
},
)
7 changes: 7 additions & 0 deletions courses/serializers/v2/programs.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ProgramSerializer(serializers.ModelSerializer):
page = serializers.SerializerMethodField()
departments = serializers.StringRelatedField(many=True, read_only=True)
topics = serializers.SerializerMethodField()
certificate_type = serializers.SerializerMethodField()

def get_courses(self, instance):
return [course[0].id for course in instance.courses if course[0].live]
Expand Down Expand Up @@ -56,6 +57,11 @@ def get_topics(self, instance):
)
return [{"name": topic} for topic in sorted(topics)]

def get_certificate_type(self, instance):
if "MicroMasters" in instance.program_type:
return "MicroMasters Credential"
return "Certificate of Completion"

class Meta:
model = Program
fields = [
Expand All @@ -67,6 +73,7 @@ class Meta:
"req_tree",
"page",
"program_type",
"certificate_type",
"departments",
"live",
"topics",
Expand Down
17 changes: 13 additions & 4 deletions courses/serializers/v2/programs_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,20 @@
pytestmark = [pytest.mark.django_db]


@pytest.mark.parametrize("remove_tree", [True, False])
@pytest.mark.parametrize(
"remove_tree",
[True, False],
"certificate_type", ["MicroMasters Credential", "Certificate of Completion"]
)
def test_serialize_program(mock_context, remove_tree, program_with_empty_requirements): # noqa: F811
def test_serialize_program(
mock_context,
remove_tree,
certificate_type,
program_with_empty_requirements, # noqa: F811
):
"""Test Program serialization"""
if certificate_type == "MicroMasters Credential":
program_with_empty_requirements.program_type = "MicroMasters®"
program_with_empty_requirements.save()
run1 = CourseRunFactory.create(
course__page=None,
start_date=now() + timedelta(hours=1),
Expand Down Expand Up @@ -79,7 +87,8 @@ def test_serialize_program(mock_context, remove_tree, program_with_empty_require
program_with_empty_requirements.requirements_root
).data,
"page": ProgramPageSerializer(program_with_empty_requirements.page).data,
"program_type": "Series",
"program_type": program_with_empty_requirements.program_type,
"certificate_type": certificate_type,
"departments": [],
"live": True,
"topics": [{"name": topic.name} for topic in topics],
Expand Down

0 comments on commit 6bc1b1f

Please sign in to comment.