Skip to content

Commit 9adaaf6

Browse files
authored
feat: adds get_containers and get_container_by_key to the authoring API [FC-0083] (#287)
* feat: adds get_containers and get_container_by_key to authoring api * test: improve test coverage of units api * chore: bumps package version
1 parent 0c72e8e commit 9adaaf6

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed

openedx_learning/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
Open edX Learning ("Learning Core").
33
"""
44

5-
__version__ = "0.19.0"
5+
__version__ = "0.19.1"

openedx_learning/apps/authoring/publishing/api.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@
7171
"create_container_version",
7272
"create_next_container_version",
7373
"get_container",
74+
"get_container_by_key",
75+
"get_containers",
7476
"ContainerEntityListEntry",
7577
"get_entities_in_container",
7678
"contains_unpublished_changes",
@@ -847,6 +849,43 @@ def get_container(pk: int) -> Container:
847849
return Container.objects.get(pk=pk)
848850

849851

852+
def get_container_by_key(learning_package_id: int, /, key: str) -> Container:
853+
"""
854+
[ 🛑 UNSTABLE ]
855+
Get a container by its learning package and primary key.
856+
857+
Args:
858+
learning_package_id: The ID of the learning package that contains the container.
859+
key: The primary key of the container.
860+
861+
Returns:
862+
The container with the given primary key.
863+
"""
864+
return Container.objects.get(
865+
publishable_entity__learning_package_id=learning_package_id,
866+
publishable_entity__key=key,
867+
)
868+
869+
870+
def get_containers(
871+
learning_package_id: int,
872+
container_cls: type[ContainerModel] = Container, # type: ignore[assignment]
873+
) -> QuerySet[ContainerModel]:
874+
"""
875+
[ 🛑 UNSTABLE ]
876+
Get all containers in the given learning package.
877+
878+
Args:
879+
learning_package_id: The primary key of the learning package
880+
container_cls: The subclass of Container to use, if applicable
881+
882+
Returns:
883+
A queryset containing the container associated with the given learning package.
884+
"""
885+
assert issubclass(container_cls, Container)
886+
return container_cls.objects.filter(publishable_entity__learning_package=learning_package_id)
887+
888+
850889
@dataclass(frozen=True)
851890
class ContainerEntityListEntry:
852891
"""

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

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

93+
def test_get_unit_version(self):
94+
"""
95+
Test get_unit_version()
96+
"""
97+
unit = self.create_unit_with_components([])
98+
draft = unit.versioning.draft
99+
with self.assertNumQueries(1):
100+
result = authoring_api.get_unit_version(draft.pk)
101+
assert result == draft
102+
103+
def test_get_latest_unit_version(self):
104+
"""
105+
Test test_get_latest_unit_version()
106+
"""
107+
unit = self.create_unit_with_components([])
108+
draft = unit.versioning.draft
109+
with self.assertNumQueries(2):
110+
result = authoring_api.get_latest_unit_version(unit.pk)
111+
assert result == draft
112+
113+
def test_get_containers(self):
114+
"""
115+
Test get_containers()
116+
"""
117+
unit = self.create_unit_with_components([])
118+
with self.assertNumQueries(1):
119+
result = list(authoring_api.get_containers(self.learning_package.id))
120+
assert result == [unit.container]
121+
# Versioning data should be pre-loaded via select_related()
122+
with self.assertNumQueries(0):
123+
assert result[0].versioning.has_unpublished_changes
124+
93125
def test_get_container(self):
94126
"""
95127
Test get_container()
@@ -102,6 +134,21 @@ def test_get_container(self):
102134
with self.assertNumQueries(0):
103135
assert result.versioning.has_unpublished_changes
104136

137+
def test_get_container_by_key(self):
138+
"""
139+
Test get_container_by_key()
140+
"""
141+
unit = self.create_unit_with_components([])
142+
with self.assertNumQueries(1):
143+
result = authoring_api.get_container_by_key(
144+
self.learning_package.id,
145+
key=unit.publishable_entity.key,
146+
)
147+
assert result == unit.container
148+
# Versioning data should be pre-loaded via select_related()
149+
with self.assertNumQueries(0):
150+
assert result.versioning.has_unpublished_changes
151+
105152
def test_unit_container_versioning(self):
106153
"""
107154
Test that the .versioning helper of a Unit returns a UnitVersion, and
@@ -807,6 +854,8 @@ def test_snapshots_of_published_unit(self):
807854
# At first the unit has one component (unpinned):
808855
unit = self.create_unit_with_components([self.component_1])
809856
self.modify_component(self.component_1, title="Component 1 as of checkpoint 1")
857+
before_publish = authoring_api.get_components_in_published_unit_as_of(unit, 0)
858+
assert before_publish is None
810859

811860
# Publish everything, creating Checkpoint 1
812861
checkpoint_1 = authoring_api.publish_all_drafts(self.learning_package.id, message="checkpoint 1")

0 commit comments

Comments
 (0)