Skip to content

Commit c9cbbac

Browse files
committed
feat: adds get_containers and get_units to the authoring API
1 parent 0f63879 commit c9cbbac

3 files changed

Lines changed: 44 additions & 0 deletions

File tree

openedx_learning/apps/authoring/publishing/api.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"create_container_version",
7272
"create_next_container_version",
7373
"get_container",
74+
"get_containers",
7475
"ContainerEntityListEntry",
7576
"get_entities_in_container",
7677
"contains_unpublished_changes",
@@ -837,6 +838,25 @@ def get_container(pk: int) -> Container:
837838
return Container.objects.get(pk=pk)
838839

839840

841+
def get_containers(
842+
learning_package_id: int,
843+
container_cls: type[ContainerModel] = Container, # type: ignore[assignment]
844+
) -> QuerySet[ContainerModel]:
845+
"""
846+
[ 🛑 UNSTABLE ]
847+
Get all containers in the given learning package.
848+
849+
Args:
850+
learning_package_id: The primary key of the learning package
851+
container_cls: The subclass of Container to use, if applicable
852+
853+
Returns:
854+
A queryset containing the container associated with the given learning package.
855+
"""
856+
assert issubclass(container_cls, Container)
857+
return container_cls.objects.filter(publishable_entity__learning_package=learning_package_id)
858+
859+
840860
@dataclass(frozen=True)
841861
class ContainerEntityListEntry:
842862
"""

openedx_learning/apps/authoring/units/api.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from dataclasses import dataclass
66
from datetime import datetime
77

8+
from django.db.models import QuerySet
89
from django.db.transaction import atomic
910

1011
from openedx_learning.apps.authoring.components.models import Component, ComponentVersion
@@ -21,6 +22,7 @@
2122
"create_unit_and_version",
2223
"get_unit",
2324
"get_unit_version",
25+
"get_units",
2426
"get_latest_unit_version",
2527
"UnitListEntry",
2628
"get_components_in_unit",
@@ -211,6 +213,16 @@ def get_latest_unit_version(unit_pk: int) -> UnitVersion:
211213
return Unit.objects.get(pk=unit_pk).versioning.latest
212214

213215

216+
def get_units(learning_package_id: int) -> QuerySet[Unit]:
217+
"""
218+
[ 🛑 UNSTABLE ] Get all units in a given learning package.
219+
220+
Args:
221+
learning_package_id: The learning package ID.
222+
"""
223+
return publishing_api.get_containers(learning_package_id, container_cls=Unit)
224+
225+
214226
@dataclass(frozen=True)
215227
class UnitListEntry:
216228
"""

tests/openedx_learning/apps/authoring/units/test_api.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,18 @@ def test_get_unit(self):
9090
with self.assertNumQueries(0):
9191
assert result.versioning.has_unpublished_changes
9292

93+
def test_get_units(self):
94+
"""
95+
Test get_units()
96+
"""
97+
unit = self.create_unit_with_components([])
98+
with self.assertNumQueries(1):
99+
result = list(authoring_api.get_units(self.learning_package.id))
100+
assert result == [unit]
101+
# Versioning data should be pre-loaded via select_related()
102+
with self.assertNumQueries(0):
103+
assert result[0].versioning.has_unpublished_changes
104+
93105
def test_get_container(self):
94106
"""
95107
Test get_container()

0 commit comments

Comments
 (0)