diff --git a/openedx_learning/__init__.py b/openedx_learning/__init__.py index 354ad372..cf87e3bf 100644 --- a/openedx_learning/__init__.py +++ b/openedx_learning/__init__.py @@ -1,4 +1,4 @@ """ Open edX Learning ("Learning Core"). """ -__version__ = "0.11.1" +__version__ = "0.11.2" diff --git a/openedx_learning/apps/authoring/collections/api.py b/openedx_learning/apps/authoring/collections/api.py index 0539dc33..942903b7 100644 --- a/openedx_learning/apps/authoring/collections/api.py +++ b/openedx_learning/apps/authoring/collections/api.py @@ -15,6 +15,7 @@ __all__ = [ "create_collection", "get_collection", + "get_collections", "get_learning_package_collections", "update_collection", ] @@ -77,4 +78,15 @@ def get_learning_package_collections(learning_package_id: int) -> QuerySet[Colle """ return Collection.objects \ .filter(learning_package_id=learning_package_id, enabled=True) \ - .select_related("learning_package") + .select_related("learning_package") \ + .order_by('pk') + + +def get_collections(enabled: bool | None = None) -> QuerySet[Collection]: + """ + Get all collections, optionally caller can filter by enabled flag + """ + qs = Collection.objects.all() + if enabled is not None: + qs = qs.filter(enabled=enabled) + return qs.select_related("learning_package").order_by('pk') diff --git a/tests/openedx_learning/apps/authoring/collections/test_api.py b/tests/openedx_learning/apps/authoring/collections/test_api.py index cf355541..9f4eef70 100644 --- a/tests/openedx_learning/apps/authoring/collections/test_api.py +++ b/tests/openedx_learning/apps/authoring/collections/test_api.py @@ -21,6 +21,7 @@ class CollectionTestCase(TestCase): Base-class for setting up commonly used test data. """ learning_package: LearningPackage + learning_package_2: LearningPackage now: datetime @classmethod @@ -29,6 +30,10 @@ def setUpTestData(cls) -> None: key="ComponentTestCase-test-key", title="Components Test Case Learning Package", ) + cls.learning_package_2 = publishing_api.create_learning_package( + key="ComponentTestCase-test-key-2", + title="Components Test Case another Learning Package", + ) cls.now = datetime(2024, 8, 5, tzinfo=timezone.utc) @@ -38,6 +43,7 @@ class GetCollectionTestCase(CollectionTestCase): """ collection1: Collection collection2: Collection + collection3: Collection disabled_collection: Collection @classmethod @@ -58,6 +64,12 @@ def setUpTestData(cls) -> None: title="Collection 2", description="Description of Collection 2", ) + cls.collection3 = collection_api.create_collection( + cls.learning_package_2.id, + created_by=None, + title="Collection 3", + description="Description of Collection 3", + ) cls.disabled_collection = collection_api.create_collection( cls.learning_package.id, created_by=None, @@ -98,6 +110,36 @@ def test_get_invalid_learning_package_collections(self): collections = collection_api.get_learning_package_collections(12345) assert not list(collections) + def test_get_all_collections(self): + """ + Test getting all collections. + """ + collections = collection_api.get_collections() + self.assertQuerySetEqual(collections, [ + self.collection1, + self.collection2, + self.collection3, + self.disabled_collection, + ], ordered=True) + + def test_get_all_enabled_collections(self): + """ + Test getting all ENABLED collections. + """ + collections = collection_api.get_collections(enabled=True) + self.assertQuerySetEqual(collections, [ + self.collection1, + self.collection2, + self.collection3, + ], ordered=True) + + def test_get_all_disabled_collections(self): + """ + Test getting all DISABLED collections. + """ + collections = collection_api.get_collections(enabled=False) + assert list(collections) == [self.disabled_collection] + class CollectionCreateTestCase(CollectionTestCase): """