From 56c0a99386cb633b9c3e4209425b8d72ae44d8ff Mon Sep 17 00:00:00 2001 From: Jillian Vogel Date: Fri, 23 Aug 2024 17:20:34 +0930 Subject: [PATCH] feat: adds get_object_collections --- .../apps/authoring/collections/api.py | 11 +++++++++ .../apps/authoring/collections/test_api.py | 24 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/openedx_learning/apps/authoring/collections/api.py b/openedx_learning/apps/authoring/collections/api.py index c6837b0e..29764735 100644 --- a/openedx_learning/apps/authoring/collections/api.py +++ b/openedx_learning/apps/authoring/collections/api.py @@ -19,6 +19,7 @@ "create_collection", "get_collection", "get_learning_package_collections", + "get_object_collections", "remove_from_collections", "update_collection", ] @@ -138,6 +139,16 @@ def remove_from_collections( return total_deleted +def get_object_collections(object_id: int) -> QuerySet[Collection]: + """ + Get all collections associated with a given PublishableEntity. + + Only enabled collections are returned. + """ + entity = PublishableEntity.objects.get(pk=object_id) + return entity.collections.filter(enabled=True).order_by("pk") + + def get_learning_package_collections(learning_package_id: int) -> QuerySet[Collection]: """ Get all collections for a given learning package diff --git a/tests/openedx_learning/apps/authoring/collections/test_api.py b/tests/openedx_learning/apps/authoring/collections/test_api.py index 97254e27..526c2cba 100644 --- a/tests/openedx_learning/apps/authoring/collections/test_api.py +++ b/tests/openedx_learning/apps/authoring/collections/test_api.py @@ -157,6 +157,7 @@ class CollectionContentsTestCase(CollectionTestCase): collection0: Collection collection1: Collection collection2: Collection + disabled_collection: Collection @classmethod def setUpTestData(cls) -> None: @@ -226,6 +227,17 @@ def setUpTestData(cls) -> None: cls.draft_entity.id, ]), ) + cls.disabled_collection = collection_api.create_collection( + cls.learning_package.id, + title="Disabled Collection", + created_by=None, + description="This disabled collection contains 1 object", + contents_qset=PublishableEntity.objects.filter(id__in=[ + cls.published_entity.id, + ]), + ) + cls.disabled_collection.enabled = False + cls.disabled_collection.save() def test_create_collection_contents(self): """ @@ -301,6 +313,18 @@ def test_remove_from_collections(self): self.draft_entity, ] + def test_get_object_collections(self): + """ + Tests fetching the enabled collections which contain a given object. + """ + collections = collection_api.get_object_collections( + self.published_entity.id, + ) + assert list(collections) == [ + self.collection1, + self.collection2, + ] + class UpdateCollectionTestCase(CollectionTestCase): """