Skip to content

Commit 977d072

Browse files
authored
feat: filter out soft-deleted containers by default on get_containers (#311)
1 parent 16a7b10 commit 977d072

File tree

5 files changed

+64
-2
lines changed

5 files changed

+64
-2
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.25.0"
5+
__version__ = "0.26.0"

openedx_learning/apps/authoring/publishing/api.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,7 @@ def get_container_by_key(learning_package_id: int, /, key: str) -> Container:
12031203
def get_containers(
12041204
learning_package_id: int,
12051205
container_cls: type[ContainerModel] = Container, # type: ignore[assignment]
1206+
include_deleted: bool | None = False,
12061207
) -> QuerySet[ContainerModel]:
12071208
"""
12081209
[ 🛑 UNSTABLE ]
@@ -1211,12 +1212,17 @@ def get_containers(
12111212
Args:
12121213
learning_package_id: The primary key of the learning package
12131214
container_cls: The subclass of Container to use, if applicable
1215+
include_deleted: If True, include deleted containers (with no draft version) in the result.
12141216
12151217
Returns:
12161218
A queryset containing the container associated with the given learning package.
12171219
"""
12181220
assert issubclass(container_cls, Container)
1219-
return container_cls.objects.filter(publishable_entity__learning_package=learning_package_id)
1221+
container_qset = container_cls.objects.filter(publishable_entity__learning_package=learning_package_id)
1222+
if not include_deleted:
1223+
container_qset = container_qset.filter(publishable_entity__draft__version__isnull=False)
1224+
1225+
return container_qset.order_by('pk')
12201226

12211227

12221228
def get_collection_containers(

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,34 @@ def test_get_containers(self):
139139
with self.assertNumQueries(0):
140140
assert result[0].versioning.has_unpublished_changes
141141

142+
def test_get_containers_deleted(self):
143+
"""
144+
Test that get_containers() does not return soft-deleted sections.
145+
"""
146+
section = self.create_section_with_subsections([])
147+
authoring_api.soft_delete_draft(section.pk)
148+
149+
with self.assertNumQueries(1):
150+
result = list(authoring_api.get_containers(self.learning_package.id, include_deleted=True))
151+
152+
assert result == [
153+
self.unit_1.container,
154+
self.unit_2.container,
155+
self.subsection_1.container,
156+
self.subsection_2.container,
157+
section.container,
158+
]
159+
160+
with self.assertNumQueries(1):
161+
result = list(authoring_api.get_containers(self.learning_package.id))
162+
163+
assert result == [
164+
self.unit_1.container,
165+
self.unit_2.container,
166+
self.subsection_1.container,
167+
self.subsection_2.container,
168+
]
169+
142170
def test_get_container(self):
143171
"""
144172
Test get_container()

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,20 @@ def test_get_containers(self):
136136
with self.assertNumQueries(0):
137137
assert result[0].versioning.has_unpublished_changes
138138

139+
def test_get_containers_deleted(self):
140+
"""
141+
Test that get_containers() does not return soft-deleted sections.
142+
"""
143+
subsection = self.create_subsection_with_units([])
144+
authoring_api.soft_delete_draft(subsection.pk)
145+
with self.assertNumQueries(1):
146+
result = list(authoring_api.get_containers(self.learning_package.id, include_deleted=True))
147+
assert result == [self.unit_1.container, self.unit_2.container, subsection.container]
148+
149+
with self.assertNumQueries(1):
150+
result = list(authoring_api.get_containers(self.learning_package.id))
151+
assert result == [self.unit_1.container, self.unit_2.container]
152+
139153
def test_get_container(self):
140154
"""
141155
Test get_container()

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,20 @@ def test_get_containers(self):
124124
with self.assertNumQueries(0):
125125
assert result[0].versioning.has_unpublished_changes
126126

127+
def test_get_containers_deleted(self):
128+
"""
129+
Test that get_containers() does not return soft-deleted units.
130+
"""
131+
unit = self.create_unit_with_components([])
132+
authoring_api.soft_delete_draft(unit.pk)
133+
with self.assertNumQueries(1):
134+
result = list(authoring_api.get_containers(self.learning_package.id, include_deleted=True))
135+
assert result == [unit.container]
136+
137+
with self.assertNumQueries(1):
138+
result = list(authoring_api.get_containers(self.learning_package.id))
139+
assert not result
140+
127141
def test_get_container(self):
128142
"""
129143
Test get_container()

0 commit comments

Comments
 (0)