Skip to content

Commit 74f0d91

Browse files
committed
feat: add collections support for containers
1 parent ca8a355 commit 74f0d91

File tree

2 files changed

+55
-1
lines changed
  • openedx_learning/apps/authoring/publishing
  • tests/openedx_learning/apps/authoring/collections

2 files changed

+55
-1
lines changed

openedx_learning/apps/authoring/publishing/api.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
"get_container",
7575
"get_container_by_key",
7676
"get_containers",
77+
"get_collection_containers",
7778
"ChildrenEntitiesAction",
7879
"ContainerEntityListEntry",
7980
"get_entities_in_container",
@@ -955,6 +956,21 @@ def get_containers(
955956
return container_cls.objects.filter(publishable_entity__learning_package=learning_package_id)
956957

957958

959+
def get_collection_containers(
960+
learning_package_id: int,
961+
collection_key: str,
962+
) -> QuerySet[Container]:
963+
"""
964+
Returns a QuerySet of Containers relating to the PublishableEntities in a Collection.
965+
966+
Containers have a one-to-one relationship with PublishableEntity, but the reverse may not always be true.
967+
"""
968+
return Container.objects.filter(
969+
publishable_entity__learning_package_id=learning_package_id,
970+
publishable_entity__collections__key=collection_key,
971+
).order_by('pk')
972+
973+
958974
@dataclass(frozen=True)
959975
class ContainerEntityListEntry:
960976
"""

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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
LearningPackage,
1919
PublishableEntity,
2020
)
21+
from openedx_learning.apps.authoring.publishing.models import learning_package
22+
from openedx_learning.apps.authoring.units.models import Unit
2123
from openedx_learning.lib.test_utils import TestCase
2224

2325
User = get_user_model()
@@ -221,10 +223,11 @@ def test_create_collection_without_description(self):
221223

222224
class CollectionEntitiesTestCase(CollectionsTestCase):
223225
"""
224-
Base class with collections that contain components.
226+
Base class with collections that contain entities.
225227
"""
226228
published_component: Component
227229
draft_component: Component
230+
draft_unit: Unit
228231
user: UserType
229232
html_type: ComponentType
230233
problem_type: ComponentType
@@ -243,6 +246,13 @@ def setUpTestData(cls) -> None:
243246

244247
cls.html_type = api.get_or_create_component_type("xblock.v1", "html")
245248
cls.problem_type = api.get_or_create_component_type("xblock.v1", "problem")
249+
created_time = datetime(2025, 4, 1, tzinfo=timezone.utc)
250+
cls.draft_unit = api.create_unit(
251+
learning_package_id=cls.learning_package.id,
252+
key="unit-1",
253+
created=created_time,
254+
created_by=cls.user.id,
255+
)
246256

247257
# Make and publish one Component
248258
cls.published_component, _ = api.create_component_and_version(
@@ -284,6 +294,7 @@ def setUpTestData(cls) -> None:
284294
entities_qset=PublishableEntity.objects.filter(id__in=[
285295
cls.published_component.pk,
286296
cls.draft_component.pk,
297+
cls.draft_unit.pk,
287298
]),
288299
)
289300
cls.disabled_collection = api.add_to_collection(
@@ -308,6 +319,7 @@ def test_create_collection_entities(self):
308319
self.published_component.publishable_entity,
309320
]
310321
assert list(self.collection2.entities.all()) == [
322+
self.draft_unit.publishable_entity,
311323
self.published_component.publishable_entity,
312324
self.draft_component.publishable_entity,
313325
]
@@ -325,11 +337,13 @@ def test_add_to_collection(self):
325337
self.collection1.key,
326338
PublishableEntity.objects.filter(id__in=[
327339
self.draft_component.pk,
340+
self.draft_unit.pk,
328341
]),
329342
created_by=self.user.id,
330343
)
331344

332345
assert list(self.collection1.entities.all()) == [
346+
self.draft_unit.publishable_entity,
333347
self.published_component.publishable_entity,
334348
self.draft_component.publishable_entity,
335349
]
@@ -352,6 +366,7 @@ def test_add_to_collection_again(self):
352366
)
353367

354368
assert list(self.collection2.entities.all()) == [
369+
self.draft_unit.publishable_entity,
355370
self.published_component.publishable_entity,
356371
self.draft_component.publishable_entity,
357372
]
@@ -383,6 +398,7 @@ def test_remove_from_collection(self):
383398
self.collection2.key,
384399
PublishableEntity.objects.filter(id__in=[
385400
self.published_component.pk,
401+
self.draft_unit.pk,
386402
]),
387403
)
388404

@@ -422,6 +438,24 @@ def test_get_collection_components(self):
422438
self.another_library_collection.key,
423439
))
424440

441+
def test_get_collection_containers(self):
442+
assert not list(api.get_collection_containers(
443+
self.learning_package.id,
444+
self.collection1.key,
445+
))
446+
assert list(api.get_collection_containers(
447+
self.learning_package.id,
448+
self.collection2.key,
449+
)) == [self.draft_unit.container]
450+
assert not list(api.get_collection_containers(
451+
self.learning_package.id,
452+
self.collection3.key,
453+
))
454+
assert not list(api.get_collection_containers(
455+
self.learning_package.id,
456+
self.another_library_collection.key,
457+
))
458+
425459

426460
class UpdateCollectionTestCase(CollectionTestCase):
427461
"""
@@ -533,6 +567,7 @@ def test_soft_delete(self):
533567
assert collection == api.get_collection(self.learning_package.id, collection.key)
534568
# ...and the collection's entities remain intact.
535569
assert list(collection.entities.all()) == [
570+
self.draft_unit.publishable_entity,
536571
self.published_component.publishable_entity,
537572
self.draft_component.publishable_entity,
538573
]
@@ -586,6 +621,7 @@ def test_restore(self):
586621
assert collection == api.get_collection(self.learning_package.id, collection.key)
587622
# ...and the collection's entities remain intact.
588623
assert list(collection.entities.all()) == [
624+
self.draft_unit.publishable_entity,
589625
self.published_component.publishable_entity,
590626
self.draft_component.publishable_entity,
591627
]
@@ -615,6 +651,7 @@ def test_set_collections(self):
615651
self.draft_component.publishable_entity,
616652
]
617653
assert list(self.collection2.entities.all()) == [
654+
self.draft_unit.publishable_entity,
618655
self.published_component.publishable_entity,
619656
self.draft_component.publishable_entity,
620657
]
@@ -651,6 +688,7 @@ def test_set_collections(self):
651688
self.published_component.publishable_entity,
652689
]
653690
assert list(self.collection2.entities.all()) == [
691+
self.draft_unit.publishable_entity,
654692
self.published_component.publishable_entity,
655693
self.draft_component.publishable_entity,
656694
]

0 commit comments

Comments
 (0)