Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
40 changes: 40 additions & 0 deletions openedx_learning/apps/authoring/publishing/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
"create_container_version",
"create_next_container_version",
"get_container",
"get_container_by_key",
"get_containers",
"ContainerEntityListEntry",
"get_entities_in_container",
"contains_unpublished_changes",
Expand Down Expand Up @@ -837,6 +839,44 @@ def get_container(pk: int) -> Container:
return Container.objects.get(pk=pk)


def get_container_by_key(learning_package_id: int, /, key: str) -> Container:
"""
[ 🛑 UNSTABLE ]
Get a container by its learning package and primary key.

Args:
learning_package_id: The ID of the learning package that contains the container.
key: The primary key of the container.

Returns:
The container with the given primary key.
"""
entity = get_publishable_entity_by_key(
learning_package_id,
key=key,
)
return get_container(entity.id)


def get_containers(
learning_package_id: int,
container_cls: type[ContainerModel] = Container, # type: ignore[assignment]
) -> QuerySet[ContainerModel]:
"""
[ 🛑 UNSTABLE ]
Get all containers in the given learning package.

Args:
learning_package_id: The primary key of the learning package
container_cls: The subclass of Container to use, if applicable

Returns:
A queryset containing the container associated with the given learning package.
"""
assert issubclass(container_cls, Container)
return container_cls.objects.filter(publishable_entity__learning_package=learning_package_id)


@dataclass(frozen=True)
class ContainerEntityListEntry:
"""
Expand Down
12 changes: 12 additions & 0 deletions openedx_learning/apps/authoring/units/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from dataclasses import dataclass
from datetime import datetime

from django.db.models import QuerySet
from django.db.transaction import atomic

from openedx_learning.apps.authoring.components.models import Component, ComponentVersion
Expand All @@ -21,6 +22,7 @@
"create_unit_and_version",
"get_unit",
"get_unit_version",
"get_units",
"get_latest_unit_version",
"UnitListEntry",
"get_components_in_unit",
Expand Down Expand Up @@ -211,6 +213,16 @@ def get_latest_unit_version(unit_pk: int) -> UnitVersion:
return Unit.objects.get(pk=unit_pk).versioning.latest


def get_units(learning_package_id: int) -> QuerySet[Unit]:
"""
[ 🛑 UNSTABLE ] Get all units in a given learning package.

Args:
learning_package_id: The learning package ID.
"""
return publishing_api.get_containers(learning_package_id, container_cls=Unit)


@dataclass(frozen=True)
class UnitListEntry:
"""
Expand Down
27 changes: 27 additions & 0 deletions tests/openedx_learning/apps/authoring/units/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@ def test_get_unit(self):
with self.assertNumQueries(0):
assert result.versioning.has_unpublished_changes

def test_get_units(self):
"""
Test get_units()
"""
unit = self.create_unit_with_components([])
with self.assertNumQueries(1):
result = list(authoring_api.get_units(self.learning_package.id))
assert result == [unit]
# Versioning data should be pre-loaded via select_related()
with self.assertNumQueries(0):
assert result[0].versioning.has_unpublished_changes

def test_get_container(self):
"""
Test get_container()
Expand All @@ -102,6 +114,21 @@ def test_get_container(self):
with self.assertNumQueries(0):
assert result.versioning.has_unpublished_changes

def test_get_container_by_key(self):
"""
Test get_container_by_key()
"""
unit = self.create_unit_with_components([])
with self.assertNumQueries(2):
result = authoring_api.get_container_by_key(
self.learning_package.id,
key=unit.publishable_entity.key,
)
assert result == unit.container
# Versioning data should be pre-loaded via select_related()
with self.assertNumQueries(0):
assert result.versioning.has_unpublished_changes

def test_unit_container_versioning(self):
"""
Test that the .versioning helper of a Unit returns a UnitVersion, and
Expand Down