diff --git a/openedx_learning/__init__.py b/openedx_learning/__init__.py index bda4b1831..be9e6c288 100644 --- a/openedx_learning/__init__.py +++ b/openedx_learning/__init__.py @@ -2,4 +2,4 @@ Open edX Learning ("Learning Core"). """ -__version__ = "0.25.0" +__version__ = "0.26.0" diff --git a/openedx_learning/apps/authoring/publishing/api.py b/openedx_learning/apps/authoring/publishing/api.py index cf94fe6c9..6ba022ac1 100644 --- a/openedx_learning/apps/authoring/publishing/api.py +++ b/openedx_learning/apps/authoring/publishing/api.py @@ -1203,6 +1203,7 @@ def get_container_by_key(learning_package_id: int, /, key: str) -> Container: def get_containers( learning_package_id: int, container_cls: type[ContainerModel] = Container, # type: ignore[assignment] + include_deleted: bool | None = False, ) -> QuerySet[ContainerModel]: """ [ 🛑 UNSTABLE ] @@ -1211,12 +1212,17 @@ def get_containers( Args: learning_package_id: The primary key of the learning package container_cls: The subclass of Container to use, if applicable + include_deleted: If True, include deleted containers (with no draft version) in the result. 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) + container_qset = container_cls.objects.filter(publishable_entity__learning_package=learning_package_id) + if not include_deleted: + container_qset = container_qset.filter(publishable_entity__draft__version__isnull=False) + + return container_qset.order_by('pk') def get_collection_containers( diff --git a/tests/openedx_learning/apps/authoring/sections/test_api.py b/tests/openedx_learning/apps/authoring/sections/test_api.py index 4faa14c8e..da0870361 100644 --- a/tests/openedx_learning/apps/authoring/sections/test_api.py +++ b/tests/openedx_learning/apps/authoring/sections/test_api.py @@ -139,6 +139,34 @@ def test_get_containers(self): with self.assertNumQueries(0): assert result[0].versioning.has_unpublished_changes + def test_get_containers_deleted(self): + """ + Test that get_containers() does not return soft-deleted sections. + """ + section = self.create_section_with_subsections([]) + authoring_api.soft_delete_draft(section.pk) + + with self.assertNumQueries(1): + result = list(authoring_api.get_containers(self.learning_package.id, include_deleted=True)) + + assert result == [ + self.unit_1.container, + self.unit_2.container, + self.subsection_1.container, + self.subsection_2.container, + section.container, + ] + + with self.assertNumQueries(1): + result = list(authoring_api.get_containers(self.learning_package.id)) + + assert result == [ + self.unit_1.container, + self.unit_2.container, + self.subsection_1.container, + self.subsection_2.container, + ] + def test_get_container(self): """ Test get_container() diff --git a/tests/openedx_learning/apps/authoring/subsections/test_api.py b/tests/openedx_learning/apps/authoring/subsections/test_api.py index acbaf6713..9593d739e 100644 --- a/tests/openedx_learning/apps/authoring/subsections/test_api.py +++ b/tests/openedx_learning/apps/authoring/subsections/test_api.py @@ -136,6 +136,20 @@ def test_get_containers(self): with self.assertNumQueries(0): assert result[0].versioning.has_unpublished_changes + def test_get_containers_deleted(self): + """ + Test that get_containers() does not return soft-deleted sections. + """ + subsection = self.create_subsection_with_units([]) + authoring_api.soft_delete_draft(subsection.pk) + with self.assertNumQueries(1): + result = list(authoring_api.get_containers(self.learning_package.id, include_deleted=True)) + assert result == [self.unit_1.container, self.unit_2.container, subsection.container] + + with self.assertNumQueries(1): + result = list(authoring_api.get_containers(self.learning_package.id)) + assert result == [self.unit_1.container, self.unit_2.container] + def test_get_container(self): """ Test get_container() diff --git a/tests/openedx_learning/apps/authoring/units/test_api.py b/tests/openedx_learning/apps/authoring/units/test_api.py index 0e83c2cf2..8ea98d95a 100644 --- a/tests/openedx_learning/apps/authoring/units/test_api.py +++ b/tests/openedx_learning/apps/authoring/units/test_api.py @@ -124,6 +124,20 @@ def test_get_containers(self): with self.assertNumQueries(0): assert result[0].versioning.has_unpublished_changes + def test_get_containers_deleted(self): + """ + Test that get_containers() does not return soft-deleted units. + """ + unit = self.create_unit_with_components([]) + authoring_api.soft_delete_draft(unit.pk) + with self.assertNumQueries(1): + result = list(authoring_api.get_containers(self.learning_package.id, include_deleted=True)) + assert result == [unit.container] + + with self.assertNumQueries(1): + result = list(authoring_api.get_containers(self.learning_package.id)) + assert not result + def test_get_container(self): """ Test get_container()