Skip to content

[BB-9572] Add list and retrieve api for learning paths #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@ Unreleased

*

0.3.1 - 2025-04-14
******************

Added
=====

* API for listing and retrieving Learning Paths.

Fixed
=====

* Automatically create grading criteria for Learning Paths.

Changed
=======

* Replaced relative due dates with actual due dates from course runs.

0.3.0 - 2025-04-03
******************

Expand Down
101 changes: 100 additions & 1 deletion learning_paths/api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@

from rest_framework import serializers

from learning_paths.models import LearningPath, LearningPathEnrollment
from learning_paths.models import (
AcquiredSkill,
LearningPath,
LearningPathEnrollment,
LearningPathStep,
RequiredSkill,
Skill,
)

DEFAULT_STATUS = "active"
IMAGE_WIDTH = 1440
Expand Down Expand Up @@ -87,6 +94,98 @@ class LearningPathGradeSerializer(serializers.Serializer):
required_grade = serializers.FloatField()


class LearningPathStepSerializer(serializers.ModelSerializer):
class Meta:
model = LearningPathStep
fields = ["order", "course_key", "due_date", "weight"]


class LearningPathListSerializer(serializers.ModelSerializer):
"""Serializer for the learning path list."""

steps = LearningPathStepSerializer(many=True, read_only=True)
required_completion = serializers.FloatField(
source="grading_criteria.required_completion", read_only=True
)

class Meta:
model = LearningPath
fields = [
"key",
"slug",
"display_name",
"image_url",
"sequential",
"steps",
"required_completion",
]


class SkillSerializer(serializers.ModelSerializer):
class Meta:
model = Skill
fields = ["id", "display_name"]


class RequiredSkillSerializer(serializers.ModelSerializer):
"""
Serializer for required skill.
"""

skill = SkillSerializer()

class Meta:
model = RequiredSkill
fields = ["skill", "level"]


class AcquiredSkillSerializer(serializers.ModelSerializer):
"""
Serializer for acquired skill.
"""

skill = SkillSerializer()

class Meta:
model = AcquiredSkill
fields = ["skill", "level"]


class LearningPathDetailSerializer(serializers.ModelSerializer):
"""
Serializer for learning path details.
"""

steps = LearningPathStepSerializer(many=True, read_only=True)
required_skills = RequiredSkillSerializer(
source="requiredskill_set", many=True, read_only=True
)
acquired_skills = AcquiredSkillSerializer(
source="acquiredskill_set", many=True, read_only=True
)
required_completion = serializers.FloatField(
source="grading_criteria.required_completion", read_only=True
)

class Meta:
model = LearningPath
fields = [
"key",
"slug",
"display_name",
"subtitle",
"description",
"image_url",
"level",
"duration_in_days",
"sequential",
"steps",
"required_skills",
"acquired_skills",
"required_completion",
]


class LearningPathEnrollmentSerializer(serializers.ModelSerializer):
class Meta:
model = LearningPathEnrollment
Expand Down
39 changes: 39 additions & 0 deletions learning_paths/api/v1/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@

from learning_paths.keys import LearningPathKey
from learning_paths.models import (
AcquiredSkill,
LearningPath,
LearningPathEnrollment,
LearningPathGradingCriteria,
LearningPathStep,
RequiredSkill,
Skill,
)

User = auth.get_user_model()
Expand Down Expand Up @@ -54,6 +58,41 @@ class Meta:
required_grade = 0.75


class LearningPathStepFactory(factory.django.DjangoModelFactory):
class Meta:
model = LearningPathStep

learning_path = factory.SubFactory(LearningPathFactory)
course_key = "course-v1:edX+DemoX+Demo_Course"
order = factory.Sequence(lambda n: n + 1)
weight = 1


class SkillFactory(factory.django.DjangoModelFactory):
class Meta:
model = Skill

display_name = factory.Faker("word")


class RequiredSkillFactory(factory.django.DjangoModelFactory):
class Meta:
model = RequiredSkill

learning_path = factory.SubFactory(LearningPathFactory)
skill = factory.SubFactory(SkillFactory)
level = factory.Faker("random_int", min=1, max=5)


class AcquiredSkillFactory(factory.django.DjangoModelFactory):
class Meta:
model = AcquiredSkill

learning_path = factory.SubFactory(LearningPathFactory)
skill = factory.SubFactory(SkillFactory)
level = factory.Faker("random_int", min=1, max=5)


class LearningPathEnrollmentFactory(factory.django.DjangoModelFactory):
"""
Factory for LearningPathEnrollment model.
Expand Down
104 changes: 90 additions & 14 deletions learning_paths/api/v1/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# pylint: disable=missing-module-docstring,missing-class-docstring
from unittest.mock import patch
from datetime import datetime, timezone
from unittest.mock import PropertyMock, patch

from django.test import override_settings
from django.urls import reverse
Expand All @@ -11,16 +12,22 @@
LearningPathProgressSerializer,
)
from learning_paths.api.v1.tests.factories import (
AcquiredSkillFactory,
LearningPathEnrollmentFactory,
LearningPathFactory,
LearningPathGradingCriteriaFactory,
LearningPathStepFactory,
RequiredSkillFactory,
UserFactory,
)
from learning_paths.api.v1.views import (
LearningPathAsProgramViewSet,
LearningPathUserProgressView,
)
from learning_paths.models import LearningPathEnrollment, LearningPathEnrollmentAllowed
from learning_paths.models import (
LearningPathEnrollment,
LearningPathEnrollmentAllowed,
LearningPathStep,
)


class LearningPathAsProgramTests(APITestCase):
Expand Down Expand Up @@ -52,11 +59,6 @@ def setUp(self):
self.user = UserFactory()
self.client.force_authenticate(user=self.user)
self.learning_path = LearningPathFactory.create()
self.grading_criteria = LearningPathGradingCriteriaFactory.create(
learning_path=self.learning_path,
required_completion=0.80,
required_grade=0.75,
)

@patch("learning_paths.api.v1.views.get_aggregate_progress", return_value=0.75)
def test_learning_path_progress_success(
Expand Down Expand Up @@ -89,17 +91,12 @@ def setUp(self) -> None:
self.staff_user = UserFactory(is_staff=True)
self.client.force_authenticate(user=self.staff_user)
self.learning_path = LearningPathFactory.create()
self.grading_criteria = LearningPathGradingCriteriaFactory.create(
learning_path=self.learning_path,
required_completion=0.80,
required_grade=0.75,
)

def test_learning_path_grade_grading_criteria_not_found(self):
"""
Test that the grade view returns 404 if grading criteria are not found.
"""
self.grading_criteria.delete()
self.learning_path.grading_criteria.delete()
url = reverse("learning-path-grade", args=[self.learning_path.key])
response = self.client.get(url)

Expand Down Expand Up @@ -128,6 +125,85 @@ def test_learning_path_grade_success(
self.assertTrue(response.data["required_grade"], 0.75)


class LearningPathViewSetTests(APITestCase):
def setUp(self) -> None:
super().setUp()
self.user = UserFactory()
self.client.force_authenticate(user=self.user)
self.learning_paths = LearningPathFactory.create_batch(3)
fake_due_date = datetime(2025, 1, 1, tzinfo=timezone.utc)
for lp in self.learning_paths:
with patch.object(
LearningPathStep, "due_date", new_callable=PropertyMock
) as mock_due_date:
mock_due_date.return_value = fake_due_date
LearningPathStepFactory.create(
learning_path=lp,
order=1,
course_key="course-v1:edX+DemoX+Demo_Course",
)
LearningPathStepFactory.create(
learning_path=lp,
order=2,
course_key="course-v1:edX+DemoX+Another_Course",
)
RequiredSkillFactory.create(learning_path=lp)
AcquiredSkillFactory.create(learning_path=lp)

@patch.object(
LearningPathStep,
"due_date",
new_callable=PropertyMock,
return_value=datetime(2025, 1, 1, tzinfo=timezone.utc),
)
def test_learning_path_list(self, _mock_due_date):
"""
Test that the list endpoint returns all learning paths with basic fields.
"""
url = reverse("learning-path-list")
response = self.client.get(url, format="json")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(len(response.data), len(self.learning_paths))
first_item = response.data[0]
self.assertIn("key", first_item)
self.assertIn("slug", first_item)
self.assertIn("display_name", first_item)
self.assertIn("steps", first_item)

def test_learning_path_retrieve(self):
"""
Test that the retrieve endpoint returns the details of a learning path,
including steps and associated skills.
"""
lp = self.learning_paths[0]
url = reverse("learning-path-detail", args=[lp.key])
fake_due_date = datetime(2025, 1, 1, tzinfo=timezone.utc)
with patch.object(
LearningPathStep, "due_date", new_callable=PropertyMock
) as mock_due_date:
mock_due_date.return_value = fake_due_date
response = self.client.get(url, format="json")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertIn("steps", response.data)
self.assertIn("required_skills", response.data)
self.assertIn("acquired_skills", response.data)
if response.data["steps"]:
first_step = response.data["steps"][0]
self.assertIn("order", first_step)
self.assertIn("course_key", first_step)
self.assertIn("due_date", first_step)
self.assertIn("weight", first_step)

def test_invalid_learning_path_key_returns_404(self):
"""
Test that an invalid learning path key format returns a 404 response.
"""
url = reverse("learning-path-detail", args=["invalid-key-format"])
response = self.client.get(url, format="json")
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
self.assertEqual(response.data["detail"], "Invalid learning path key format.")


class LearningPathEnrollmentTests(APITestCase):
def setUp(self) -> None:
super().setUp()
Expand Down
2 changes: 2 additions & 0 deletions learning_paths/api/v1/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
LearningPathEnrollmentView,
LearningPathUserGradeView,
LearningPathUserProgressView,
LearningPathViewSet,
ListEnrollmentsView,
)
from learning_paths.keys import LEARNING_PATH_URL_PATTERN
Expand All @@ -17,6 +18,7 @@
router.register(
r"programs", LearningPathAsProgramViewSet, basename="learning-path-as-program"
)
router.register(r"learning-paths", LearningPathViewSet, basename="learning-path")

urlpatterns = router.urls + [
re_path(
Expand Down
Loading