From f39115fa6dab2af8f927562a17ffc96e22ea5cab Mon Sep 17 00:00:00 2001 From: XnpioChV Date: Fri, 8 Nov 2024 16:21:48 -0500 Subject: [PATCH 1/4] feat: get_drafts and get_published functions on publishing api - Given a list of publishable entities returns current drafts. Not all entities may have current drafts. - Given a list of publishable entities returns those who have a published version. Not all entities may have current published version. --- .../apps/authoring/publishing/api.py | 28 +++++++ .../apps/authoring/publishing/test_api.py | 76 +++++++++++++++++++ 2 files changed, 104 insertions(+) diff --git a/openedx_learning/apps/authoring/publishing/api.py b/openedx_learning/apps/authoring/publishing/api.py index 7e2c6ba9..aca3360b 100644 --- a/openedx_learning/apps/authoring/publishing/api.py +++ b/openedx_learning/apps/authoring/publishing/api.py @@ -50,6 +50,8 @@ "soft_delete_draft", "reset_drafts_to_published", "register_content_models", + "get_drafts", + "get_published", ] @@ -493,3 +495,29 @@ def ready(self): return PublishableContentModelRegistry.register( content_model_cls, content_version_model_cls ) + + +def get_drafts(publishable_entities: QuerySet[PublishableEntity]) -> QuerySet[Draft]: + """ + Given a list of publishable entities returns current drafts. + Not all entities may have current drafts. + """ + entity_ids = publishable_entities.values_list("id", flat=True) + + return Draft.objects.filter( + entity_id__in=entity_ids, + version__isnull=False, + ).select_related("entity", "version") + + +def get_published(publishable_entities: QuerySet[PublishableEntity]) -> QuerySet[Published]: + """ + Given a list of publishable entities returns those who have a published version. + Not all entities may have current published version. + """ + entity_ids = publishable_entities.values_list("id", flat=True) + + return Published.objects.filter( + entity_id__in=entity_ids, + version__isnull=False, + ).select_related("entity", "version") diff --git a/tests/openedx_learning/apps/authoring/publishing/test_api.py b/tests/openedx_learning/apps/authoring/publishing/test_api.py index 421dd66c..f95579e7 100644 --- a/tests/openedx_learning/apps/authoring/publishing/test_api.py +++ b/tests/openedx_learning/apps/authoring/publishing/test_api.py @@ -368,6 +368,82 @@ def test_get_entities_with_unpublished_changes(self) -> None: # should not return published soft-deleted entities. assert len(entities) == 0 + def test_get_drafts(self) -> None: + n = 13 + x = 5 + + for i in range(n): + # Create entities with drafts + entity = publishing_api.create_publishable_entity( + self.learning_package.id, + f"entity_{i}", + created=self.now, + created_by=None, + ) + + publishing_api.create_publishable_entity_version( + entity.id, + version_num=1, + title=f"Entity_{i}", + created=self.now, + created_by=None, + ) + + for i in range(x): + # Create entities without drafts + entity = publishing_api.create_publishable_entity( + self.learning_package.id, + f"entity_no_drafts_{i}", + created=self.now, + created_by=None, + ) + + drafts = publishing_api.get_drafts(PublishableEntity.objects.all()) + assert len(drafts) == n + + def test_get_published(self) -> None: + n = 13 + x = 5 + + for i in range(n): + # Create entities to publish + entity = publishing_api.create_publishable_entity( + self.learning_package.id, + f"entity_{i}", + created=self.now, + created_by=None, + ) + + publishing_api.create_publishable_entity_version( + entity.id, + version_num=1, + title=f"Entity_{i}", + created=self.now, + created_by=None, + ) + + publishing_api.publish_all_drafts(self.learning_package.id) + + for i in range(x): + # Create entities without publish + entity = publishing_api.create_publishable_entity( + self.learning_package.id, + f"entity_no_drafts_{i}", + created=self.now, + created_by=None, + ) + + publishing_api.create_publishable_entity_version( + entity.id, + version_num=1, + title=f"Entity_{i}", + created=self.now, + created_by=None, + ) + + published = publishing_api.get_published(PublishableEntity.objects.all()) + assert len(published) == n + def _get_published_version_num(self, entity: PublishableEntity) -> int | None: published_version = publishing_api.get_published_version(entity.id) if published_version is not None: From 9d2bf13a48d43464ca649e575d50e23dcd7ca42c Mon Sep 17 00:00:00 2001 From: XnpioChV Date: Fri, 8 Nov 2024 16:23:36 -0500 Subject: [PATCH 2/4] 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 1521ec58..23b5c7c2 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 8a1adeb34977dd81136400c35e2fd513d1ae67f0 Mon Sep 17 00:00:00 2001 From: XnpioChV Date: Fri, 8 Nov 2024 17:41:15 -0500 Subject: [PATCH 3/4] chore: Bump version to 0.18.0 --- 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 23b5c7c2..f99dcab8 100644 --- a/openedx_learning/__init__.py +++ b/openedx_learning/__init__.py @@ -2,4 +2,4 @@ Open edX Learning ("Learning Core"). """ -__version__ = "0.17.0" +__version__ = "0.18.0" From 19f542b6b56b8b7e62827acaecdbf9d919f551f9 Mon Sep 17 00:00:00 2001 From: XnpioChV Date: Mon, 11 Nov 2024 15:09:27 -0500 Subject: [PATCH 4/4] refactor: Add filter_publishable_entities and remove get_drafts and get_published functions --- .../apps/authoring/publishing/api.py | 36 ++++----- .../apps/authoring/publishing/test_api.py | 78 +++++++++---------- 2 files changed, 52 insertions(+), 62 deletions(-) diff --git a/openedx_learning/apps/authoring/publishing/api.py b/openedx_learning/apps/authoring/publishing/api.py index aca3360b..3facc891 100644 --- a/openedx_learning/apps/authoring/publishing/api.py +++ b/openedx_learning/apps/authoring/publishing/api.py @@ -50,8 +50,7 @@ "soft_delete_draft", "reset_drafts_to_published", "register_content_models", - "get_drafts", - "get_published", + "filter_publishable_entities", ] @@ -497,27 +496,20 @@ def ready(self): ) -def get_drafts(publishable_entities: QuerySet[PublishableEntity]) -> QuerySet[Draft]: - """ - Given a list of publishable entities returns current drafts. - Not all entities may have current drafts. +def filter_publishable_entities( + entities: QuerySet[PublishableEntity], + has_draft=None, + has_published=None +) -> QuerySet[PublishableEntity]: """ - entity_ids = publishable_entities.values_list("id", flat=True) - - return Draft.objects.filter( - entity_id__in=entity_ids, - version__isnull=False, - ).select_related("entity", "version") - + Filter an entities query set. -def get_published(publishable_entities: QuerySet[PublishableEntity]) -> QuerySet[Published]: + has_draft: You can filter by entities that has a draft or not. + has_published: You can filter by entities that has a published version or not. """ - Given a list of publishable entities returns those who have a published version. - Not all entities may have current published version. - """ - entity_ids = publishable_entities.values_list("id", flat=True) + if has_draft is not None: + entities = entities.filter(draft__version__isnull=not has_draft) + if has_published is not None: + entities = entities.filter(published__version__isnull=not has_published) - return Published.objects.filter( - entity_id__in=entity_ids, - version__isnull=False, - ).select_related("entity", "version") + return entities diff --git a/tests/openedx_learning/apps/authoring/publishing/test_api.py b/tests/openedx_learning/apps/authoring/publishing/test_api.py index f95579e7..119a5548 100644 --- a/tests/openedx_learning/apps/authoring/publishing/test_api.py +++ b/tests/openedx_learning/apps/authoring/publishing/test_api.py @@ -368,15 +368,16 @@ def test_get_entities_with_unpublished_changes(self) -> None: # should not return published soft-deleted entities. assert len(entities) == 0 - def test_get_drafts(self) -> None: - n = 13 - x = 5 + def test_filter_publishable_entities(self) -> None: + count_published = 7 + count_drafts = 6 + count_no_drafts = 3 - for i in range(n): - # Create entities with drafts + for index in range(count_published): + # Create entities to publish entity = publishing_api.create_publishable_entity( self.learning_package.id, - f"entity_{i}", + f"entity_published_{index}", created=self.now, created_by=None, ) @@ -384,32 +385,18 @@ def test_get_drafts(self) -> None: publishing_api.create_publishable_entity_version( entity.id, version_num=1, - title=f"Entity_{i}", - created=self.now, - created_by=None, - ) - - for i in range(x): - # Create entities without drafts - entity = publishing_api.create_publishable_entity( - self.learning_package.id, - f"entity_no_drafts_{i}", + title=f"Entity_published_{index}", created=self.now, created_by=None, ) - drafts = publishing_api.get_drafts(PublishableEntity.objects.all()) - assert len(drafts) == n - - def test_get_published(self) -> None: - n = 13 - x = 5 + publishing_api.publish_all_drafts(self.learning_package.id) - for i in range(n): - # Create entities to publish + for index in range(count_drafts): + # Create entities with drafts entity = publishing_api.create_publishable_entity( self.learning_package.id, - f"entity_{i}", + f"entity_draft_{index}", created=self.now, created_by=None, ) @@ -417,32 +404,43 @@ def test_get_published(self) -> None: publishing_api.create_publishable_entity_version( entity.id, version_num=1, - title=f"Entity_{i}", + title=f"Entity_draft_{index}", created=self.now, created_by=None, ) - publishing_api.publish_all_drafts(self.learning_package.id) - - for i in range(x): - # Create entities without publish + for index in range(count_no_drafts): + # Create entities without drafts entity = publishing_api.create_publishable_entity( self.learning_package.id, - f"entity_no_drafts_{i}", + f"entity_no_draft_{index}", created=self.now, created_by=None, ) - publishing_api.create_publishable_entity_version( - entity.id, - version_num=1, - title=f"Entity_{i}", - created=self.now, - created_by=None, - ) + drafts = publishing_api.filter_publishable_entities( + PublishableEntity.objects.all(), + has_draft=True, + ) + assert drafts.count() == (count_published + count_drafts) + + no_drafts = publishing_api.filter_publishable_entities( + PublishableEntity.objects.all(), + has_draft=False, + ) + assert no_drafts.count() == count_no_drafts - published = publishing_api.get_published(PublishableEntity.objects.all()) - assert len(published) == n + published = publishing_api.filter_publishable_entities( + PublishableEntity.objects.all(), + has_published=True, + ) + assert published.count() == count_published + + no_published = publishing_api.filter_publishable_entities( + PublishableEntity.objects.all(), + has_published=False, + ) + assert no_published.count() == (count_drafts + count_no_drafts) def _get_published_version_num(self, entity: PublishableEntity) -> int | None: published_version = publishing_api.get_published_version(entity.id)