From 0a18a26cecc3be7431fc2dce9bfb84b0e0fba522 Mon Sep 17 00:00:00 2001 From: XnpioChV Date: Fri, 18 Apr 2025 10:49:20 -0500 Subject: [PATCH] feat: Add new publish field on container search document * `punlish_display_name` and `last_published` --- .../djangoapps/content/search/documents.py | 4 +++- .../content/search/tests/test_api.py | 1 + .../content/search/tests/test_documents.py | 18 ++++++++++++++---- .../content_libraries/api/containers.py | 2 ++ 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/openedx/core/djangoapps/content/search/documents.py b/openedx/core/djangoapps/content/search/documents.py index c07048057a3f..110c40021915 100644 --- a/openedx/core/djangoapps/content/search/documents.py +++ b/openedx/core/djangoapps/content/search/documents.py @@ -572,6 +572,7 @@ def searchable_doc_for_container( Fields.block_id: container_key.container_id, # Field name isn't exact but this is the closest match Fields.access_id: _meili_access_id_from_context_key(container_key.library_key), Fields.publish_status: PublishStatus.never, + Fields.last_published: None, } try: @@ -593,6 +594,7 @@ def searchable_doc_for_container( Fields.modified: container.modified.timestamp(), Fields.num_children: draft_num_children, Fields.publish_status: publish_status, + Fields.last_published: container.last_published.timestamp() if container.last_published else None, }) library = lib_api.get_library(container_key.library_key) if library: @@ -601,8 +603,8 @@ def searchable_doc_for_container( if container.published_version_num is not None: published_num_children = lib_api.get_container_children_count(container_key, published=True) doc[Fields.published] = { - # Fields.published_display_name: container_published.title, TODO: set the published title Fields.published_num_children: published_num_children, + Fields.published_display_name: container.published_display_name, } return doc diff --git a/openedx/core/djangoapps/content/search/tests/test_api.py b/openedx/core/djangoapps/content/search/tests/test_api.py index 8a486aa99c60..ee70bd444721 100644 --- a/openedx/core/djangoapps/content/search/tests/test_api.py +++ b/openedx/core/djangoapps/content/search/tests/test_api.py @@ -235,6 +235,7 @@ def setUp(self): "org": "org1", "created": created_date.timestamp(), "modified": created_date.timestamp(), + "last_published": None, "access_id": lib_access.id, "breadcrumbs": [{"display_name": "Library"}], # "published" is not set since we haven't published it yet diff --git a/openedx/core/djangoapps/content/search/tests/test_documents.py b/openedx/core/djangoapps/content/search/tests/test_documents.py index c4c3213ce33d..0a5d871fbb96 100644 --- a/openedx/core/djangoapps/content/search/tests/test_documents.py +++ b/openedx/core/djangoapps/content/search/tests/test_documents.py @@ -534,6 +534,7 @@ def test_draft_container(self): "breadcrumbs": [{"display_name": "some content_library"}], "created": 1680674828.0, "modified": 1680674828.0, + "last_published": None, "tags": { "taxonomy": ["Difficulty"], "level0": ["Difficulty > Normal"] @@ -552,7 +553,7 @@ def test_published_container(self): [self.library_block.usage_key], user_id=None, ) - library_api.publish_changes(self.library.key) + library_api.publish_changes(self.library.key) doc = searchable_doc_for_container(self.container.container_key) doc.update(searchable_doc_tags(self.container.container_key)) @@ -573,11 +574,15 @@ def test_published_container(self): "breadcrumbs": [{"display_name": "some content_library"}], "created": 1680674828.0, "modified": 1680674828.0, + "last_published": 1680674828.0, "tags": { "taxonomy": ["Difficulty"], "level0": ["Difficulty > Normal"] }, - "published": {"num_children": 1}, + "published": { + "num_children": 1, + "display_name": "A Unit in the Search Index", + }, } def test_published_container_with_changes(self): @@ -589,7 +594,8 @@ def test_published_container_with_changes(self): [self.library_block.usage_key], user_id=None, ) - library_api.publish_changes(self.library.key) + with freeze_time(self.container.created): + library_api.publish_changes(self.library.key) block_2 = library_api.create_library_block( self.library.key, "html", @@ -624,11 +630,15 @@ def test_published_container_with_changes(self): "breadcrumbs": [{"display_name": "some content_library"}], "created": 1680674828.0, "modified": 1680674828.0, + "last_published": 1680674828.0, "tags": { "taxonomy": ["Difficulty"], "level0": ["Difficulty > Normal"] }, - "published": {"num_children": 1}, + "published": { + "num_children": 1, + "display_name": "A Unit in the Search Index", + }, } def test_mathjax_plain_text_conversion_for_search(self): diff --git a/openedx/core/djangoapps/content_libraries/api/containers.py b/openedx/core/djangoapps/content_libraries/api/containers.py index 800cbc0585c5..5be11ec9e6eb 100644 --- a/openedx/core/djangoapps/content_libraries/api/containers.py +++ b/openedx/core/djangoapps/content_libraries/api/containers.py @@ -70,6 +70,7 @@ class ContainerMetadata(PublishableItem): """ container_key: LibraryContainerLocator container_type: ContainerType + published_display_name: str | None @classmethod def from_container(cls, library_key, container: Container, associated_collections=None): @@ -103,6 +104,7 @@ def from_container(cls, library_key, container: Container, associated_collection modified=draft.created, draft_version_num=draft.version_num, published_version_num=published.version_num if published else None, + published_display_name=published.title if published else None, last_published=None if last_publish_log is None else last_publish_log.published_at, published_by=published_by, last_draft_created=last_draft_created,