refactor: delete documents that were never published on discard

This commit is contained in:
Navin Karkera
2024-08-07 13:14:17 +05:30
committed by Navin Karkera
parent 140b9bb968
commit e1495398e2
3 changed files with 16 additions and 10 deletions

View File

@@ -18,7 +18,7 @@ from meilisearch import Client as MeilisearchClient
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):

View File

@@ -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)

View File

@@ -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,