Skip to content

Commit

Permalink
refactor: delete documents that were never published on discard
Browse files Browse the repository at this point in the history
  • Loading branch information
navinkarkera committed Aug 7, 2024
1 parent 2654e85 commit 3530c0d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
17 changes: 11 additions & 6 deletions openedx/core/djangoapps/content/search/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down
4 changes: 3 additions & 1 deletion openedx/core/djangoapps/content/search/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
5 changes: 2 additions & 3 deletions openedx/core/djangoapps/content_libraries/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand Down

0 comments on commit 3530c0d

Please sign in to comment.