From cafe2a3f9a8c98c44fe3472bf3d7ecaf3a4a2cfa Mon Sep 17 00:00:00 2001 From: XnpioChV Date: Mon, 28 Oct 2024 10:33:02 -0500 Subject: [PATCH 1/6] feat: Remove unpublished from collections func One use case for this function is when reverting to published. If a collection has unpublished entities and the learning package is reverted to the published version, the unpublished entities need to be removed. --- .../apps/authoring/collections/api.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/openedx_learning/apps/authoring/collections/api.py b/openedx_learning/apps/authoring/collections/api.py index 07b7a2eaf..518fd9595 100644 --- a/openedx_learning/apps/authoring/collections/api.py +++ b/openedx_learning/apps/authoring/collections/api.py @@ -27,6 +27,7 @@ "remove_from_collection", "restore_collection", "update_collection", + "remove_unpublished_from_collections", ] @@ -204,3 +205,25 @@ def get_collections(learning_package_id: int, enabled: bool | None = True) -> Qu if enabled is not None: qs = qs.filter(enabled=enabled) return qs.select_related("learning_package").order_by('pk') + + +def remove_unpublished_from_collections(learning_package_id: int) -> None: + """ + Removes all unpublished entities from collections for a + given learning package. + """ + collections = get_collections(learning_package_id) + + for collection in collections: + entities_for_remove = [] + + for entity in collection.entities.all(): + if not publishing_api.get_published_version(entity.id): + entities_for_remove.append(entity.id) + + if entities_for_remove: + remove_from_collection( + learning_package_id, + collection.key, + PublishableEntity.objects.filter(id__in=entities_for_remove) + ) From a88d8692ce98baca529a0b2cf1a4788b9196e3d0 Mon Sep 17 00:00:00 2001 From: XnpioChV Date: Mon, 28 Oct 2024 18:36:32 -0500 Subject: [PATCH 2/6] refactor: remove_unpublished_from_collections func to use less queries --- openedx_learning/apps/authoring/collections/api.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/openedx_learning/apps/authoring/collections/api.py b/openedx_learning/apps/authoring/collections/api.py index 518fd9595..fc32815fb 100644 --- a/openedx_learning/apps/authoring/collections/api.py +++ b/openedx_learning/apps/authoring/collections/api.py @@ -9,8 +9,8 @@ from django.db.models import QuerySet from ..publishing import api as publishing_api -from ..publishing.models import PublishableEntity -from .models import Collection +from ..publishing.models import PublishableEntity, Published +from .models import Collection, CollectionPublishableEntity # The public API that will be re-exported by openedx_learning.apps.authoring.api # is listed in the __all__ entries below. Internal helper functions that are @@ -214,11 +214,19 @@ def remove_unpublished_from_collections(learning_package_id: int) -> None: """ collections = get_collections(learning_package_id) + all_entities = CollectionPublishableEntity.objects.filter( + collection__learning_package__id=learning_package_id + ).values_list('entity_id', flat=True) + + entities_with_version = Published.objects.select_related("version").filter( + entity_id__in=all_entities + ).values_list('entity_id', flat=True) + for collection in collections: entities_for_remove = [] for entity in collection.entities.all(): - if not publishing_api.get_published_version(entity.id): + if entity.id not in entities_with_version: entities_for_remove.append(entity.id) if entities_for_remove: From 554f9660353f9bf92a939ed215cf232beab985cc Mon Sep 17 00:00:00 2001 From: XnpioChV Date: Tue, 29 Oct 2024 11:06:59 -0500 Subject: [PATCH 3/6] test: Test added for remove_unpublished_from_collections --- .../apps/authoring/collections/api.py | 2 +- .../apps/authoring/collections/test_api.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/openedx_learning/apps/authoring/collections/api.py b/openedx_learning/apps/authoring/collections/api.py index fc32815fb..51d5692a1 100644 --- a/openedx_learning/apps/authoring/collections/api.py +++ b/openedx_learning/apps/authoring/collections/api.py @@ -217,7 +217,7 @@ def remove_unpublished_from_collections(learning_package_id: int) -> None: all_entities = CollectionPublishableEntity.objects.filter( collection__learning_package__id=learning_package_id ).values_list('entity_id', flat=True) - + entities_with_version = Published.objects.select_related("version").filter( entity_id__in=all_entities ).values_list('entity_id', flat=True) diff --git a/tests/openedx_learning/apps/authoring/collections/test_api.py b/tests/openedx_learning/apps/authoring/collections/test_api.py index 179869a34..736bc8d3a 100644 --- a/tests/openedx_learning/apps/authoring/collections/test_api.py +++ b/tests/openedx_learning/apps/authoring/collections/test_api.py @@ -405,6 +405,22 @@ def test_get_collection_components(self): self.collection3.key, )) + def test_remove_unpublished_components(self): + api.remove_unpublished_from_collections(self.learning_package.id) + + assert list(api.get_collection_components( + self.learning_package.id, + self.collection1.key, + )) == [self.published_component] + assert list(api.get_collection_components( + self.learning_package.id, + self.collection2.key, + )) == [self.published_component] + assert not list(api.get_collection_components( + self.learning_package.id, + self.collection3.key, + )) + class UpdateCollectionTestCase(CollectionTestCase): """ From 772df4ece10be9ae10c69808a706738753f9b88a Mon Sep 17 00:00:00 2001 From: XnpioChV Date: Tue, 29 Oct 2024 14:45:31 -0500 Subject: [PATCH 4/6] chore: Bump version --- openedx_learning/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openedx_learning/__init__.py b/openedx_learning/__init__.py index 1521ec581..23b5c7c22 100644 --- a/openedx_learning/__init__.py +++ b/openedx_learning/__init__.py @@ -2,4 +2,4 @@ Open edX Learning ("Learning Core"). """ -__version__ = "0.16.3" +__version__ = "0.17.0" From 3b940ce9d9ad482fe83b5613650c36655c9c8694 Mon Sep 17 00:00:00 2001 From: XnpioChV Date: Thu, 31 Oct 2024 20:00:07 -0500 Subject: [PATCH 5/6] style: Add comments --- openedx_learning/apps/authoring/collections/api.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/openedx_learning/apps/authoring/collections/api.py b/openedx_learning/apps/authoring/collections/api.py index 51d5692a1..f5a5e2d0f 100644 --- a/openedx_learning/apps/authoring/collections/api.py +++ b/openedx_learning/apps/authoring/collections/api.py @@ -211,6 +211,10 @@ def remove_unpublished_from_collections(learning_package_id: int) -> None: """ Removes all unpublished entities from collections for a given learning package. + + One use case for this function is when reverting to published. + If a collection has unpublished entities and the learning package is reverted + to the published version, the unpublished entities need to be removed. """ collections = get_collections(learning_package_id) From e16f6a2671bbfeb0d81facd49b1b438f4830a37f Mon Sep 17 00:00:00 2001 From: XnpioChV Date: Fri, 1 Nov 2024 12:16:54 -0500 Subject: [PATCH 6/6] style: Update comment in remove_unpublished_from_collections --- openedx_learning/apps/authoring/collections/api.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/openedx_learning/apps/authoring/collections/api.py b/openedx_learning/apps/authoring/collections/api.py index f5a5e2d0f..8119ac060 100644 --- a/openedx_learning/apps/authoring/collections/api.py +++ b/openedx_learning/apps/authoring/collections/api.py @@ -212,9 +212,10 @@ def remove_unpublished_from_collections(learning_package_id: int) -> None: Removes all unpublished entities from collections for a given learning package. - One use case for this function is when reverting to published. - If a collection has unpublished entities and the learning package is reverted - to the published version, the unpublished entities need to be removed. + One use case for this function is when reverting a library to published version. + If a collection of a library has unpublished entities and the library is reverted + to the published version, the unpublished entities need to be removed + to reflect the published state of the library. """ collections = get_collections(learning_package_id)