From 2ec9070c726b9b122a75b9c868d76490520699b9 Mon Sep 17 00:00:00 2001 From: Navin Karkera Date: Wed, 7 Aug 2024 13:14:17 +0530 Subject: [PATCH] refactor: delete documents that were never published on discard --- openedx/core/djangoapps/content/search/api.py | 17 +++++++++++------ openedx/core/djangoapps/content/search/tasks.py | 4 +++- .../core/djangoapps/content_libraries/api.py | 5 ++--- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/openedx/core/djangoapps/content/search/api.py b/openedx/core/djangoapps/content/search/api.py index 79726df65de6..bad46dab91a6 100644 --- a/openedx/core/djangoapps/content/search/api.py +++ b/openedx/core/djangoapps/content/search/api.py @@ -18,7 +18,7 @@ from meilisearch.errors import MeilisearchError from meilisearch.models.task import TaskInfo from opaque_keys.edx.keys import UsageKey -from opaque_keys.edx.locator import LibraryLocatorV2, LibraryUsageLocatorV2 +from opaque_keys.edx.locator import LibraryLocatorV2 from common.djangoapps.student.roles import GlobalStaff from rest_framework.request import Request from common.djangoapps.student.role_helpers import get_course_roles @@ -231,7 +231,7 @@ def _recurse_children(block, fn, status_cb: Callable[[str], None] | None = None) fn(child) -def _update_index_docs(docs) -> list[TaskInfo]: +def _update_index_docs(docs) -> None: """ Helper function that updates the documents in the search index @@ -467,13 +467,18 @@ def delete_index_doc(usage_key: UsageKey) -> None: _wait_for_meili_tasks(tasks) -def delete_all_index_docs_for_library(library_key: LibraryLocatorV2) -> None: +def delete_all_draft_docs_for_library(library_key: LibraryLocatorV2) -> None: """ - Deletes documents for the given XBlocks from the search index + Deletes draft documents for the given XBlocks from the search index """ current_rebuild_index_name = _get_running_rebuild_index_name() client = _get_meilisearch_client() - filter = f'{Fields.context_key}="{library_key}"' + # Delete all documents where last_published is null i.e. never published before. + filter = [ + f'{Fields.context_key}="{library_key}"', + # inner arrays are connected by an OR + [f'{Fields.last_published} IS EMPTY', f'{Fields.last_published} IS NULL'], + ] tasks = [] if current_rebuild_index_name: @@ -509,7 +514,7 @@ def upsert_content_library_index_docs(library_key: LibraryLocatorV2) -> None: doc = searchable_doc_for_library_block(metadata) docs.append(doc) - return _update_index_docs(docs) + _update_index_docs(docs) def upsert_block_tags_index_docs(usage_key: UsageKey): diff --git a/openedx/core/djangoapps/content/search/tasks.py b/openedx/core/djangoapps/content/search/tasks.py index 73dcfe284d02..dfd603776981 100644 --- a/openedx/core/djangoapps/content/search/tasks.py +++ b/openedx/core/djangoapps/content/search/tasks.py @@ -80,5 +80,7 @@ def update_content_library_index_docs(library_key_str: str) -> None: log.info("Updating content index documents for library with id: %s", library_key) - api.delete_all_index_docs_for_library(library_key) api.upsert_content_library_index_docs(library_key) + # Delete all documents in this library that were not published by above function + # as this task is also triggered on discard event. + api.delete_all_draft_docs_for_library(library_key) diff --git a/openedx/core/djangoapps/content_libraries/api.py b/openedx/core/djangoapps/content_libraries/api.py index 2b3f176d8268..d2c6eeeeabd9 100644 --- a/openedx/core/djangoapps/content_libraries/api.py +++ b/openedx/core/djangoapps/content_libraries/api.py @@ -642,7 +642,7 @@ def _get_library_component_tags_count(library_key) -> dict: return get_object_tag_counts(library_key_pattern, count_implicit=True) -def get_library_components(library_key, text_search=None, block_types=None, draft=True, published=None) -> QuerySet[Component]: +def get_library_components(library_key, text_search=None, block_types=None) -> QuerySet[Component]: """ Get the library components and filter. @@ -653,8 +653,7 @@ def get_library_components(library_key, text_search=None, block_types=None, draf learning_package = lib.learning_package components = authoring_api.get_components( learning_package.id, - draft=draft, - published=published, + draft=True, namespace='xblock.v1', type_names=block_types, draft_title=text_search,