Skip to content

Commit

Permalink
feat: add publish status to library meilisearch index
Browse files Browse the repository at this point in the history
# Description

Adds the publish status field to the libraries v2 meilisearch index in
order to support filtering by component publish status: published,
modified, never.
  • Loading branch information
DanielVZ96 committed Dec 13, 2024
1 parent ee30f1b commit 5fb0549
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 0 deletions.
22 changes: 22 additions & 0 deletions openedx/core/djangoapps/content/search/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ class Fields:
# Structural XBlocks may use this one day to indicate how many child blocks they ocntain.
num_children = "num_children"

# Publish status can be on of:
# "published",
# "modified" (for blocks that were published but have been modified since),
# "never" (for never-published blocks).
publish_status = "publish_status"

# Published data (dictionary) of this object
published = "published"
published_display_name = "display_name"
Expand All @@ -96,6 +102,15 @@ class DocType:
collection = "collection"


class PublishStatus:
"""
Values for the 'publish_status' field on each doc in the search index
"""
never = "never"
published = "published"
modified = "modified"


def meili_id_from_opaque_key(usage_key: UsageKey) -> str:
"""
Meilisearch requires each document to have a primary key that's either an
Expand Down Expand Up @@ -376,11 +391,17 @@ def searchable_doc_for_library_block(xblock_metadata: lib_api.LibraryXBlockMetad
library_name = lib_api.get_library(xblock_metadata.usage_key.context_key).title
block = xblock_api.load_block(xblock_metadata.usage_key, user=None)

publish_status = PublishStatus.published
try:
block_published = xblock_api.load_block(xblock_metadata.usage_key, user=None, version=LatestVersion.PUBLISHED)
if xblock_metadata.last_published and xblock_metadata.last_published < xblock_metadata.modified:
publish_status = PublishStatus.modified
print("DEBUG publish_status", publish_status)
print("DEBUG if xblock_metadata.last_published and xblock_metadata.last_published < xblock_metadata.modified:", xblock_metadata.last_published, xblock_metadata.modified)
except NotFound:
# Never published
block_published = None
publish_status = PublishStatus.never

doc = searchable_doc_for_usage_key(xblock_metadata.usage_key)
doc.update({
Expand All @@ -389,6 +410,7 @@ def searchable_doc_for_library_block(xblock_metadata: lib_api.LibraryXBlockMetad
Fields.created: xblock_metadata.created.timestamp(),
Fields.modified: xblock_metadata.modified.timestamp(),
Fields.last_published: xblock_metadata.last_published.timestamp() if xblock_metadata.last_published else None,
Fields.publish_status: publish_status,
})

doc.update(_fields_from_block(block))
Expand Down
1 change: 1 addition & 0 deletions openedx/core/djangoapps/content/search/index_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
Fields.access_id,
Fields.last_published,
Fields.content + "." + Fields.problem_types,
Fields.publish_status,
]

# Mark which attributes are used for keyword search, in order of importance:
Expand Down
2 changes: 2 additions & 0 deletions openedx/core/djangoapps/content/search/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ def setUp(self):
"last_published": None,
"created": created_date.timestamp(),
"modified": modified_date.timestamp(),
"publish_status": "never",
}
self.doc_problem2 = {
"id": "lborg1libproblemp2-b2f65e29",
Expand All @@ -164,6 +165,7 @@ def setUp(self):
"last_published": None,
"created": created_date.timestamp(),
"modified": created_date.timestamp(),
"publish_status": "never",
}

# Create a couple of taxonomies with tags
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ def test_html_library_block(self):
"taxonomy": ["Difficulty"],
"level0": ["Difficulty > Normal"],
},
"publish_status": "never",
}

def test_html_published_library_block(self):
Expand Down Expand Up @@ -337,6 +338,7 @@ def test_html_published_library_block(self):
"level0": ["Difficulty > Normal"],
},
'published': {'display_name': 'Text'},
"publish_status": "published",
}

# Update library block to create a draft
Expand Down Expand Up @@ -378,6 +380,7 @@ def test_html_published_library_block(self):
"level0": ["Difficulty > Normal"],
},
"published": {"display_name": "Text"},
"publish_status": "published",
}

# Publish new changes
Expand Down Expand Up @@ -420,6 +423,7 @@ def test_html_published_library_block(self):
"display_name": "Text 2",
"description": "This is a Test",
},
"publish_status": "published",
}

def test_collection_with_library(self):
Expand Down
2 changes: 2 additions & 0 deletions openedx/core/djangoapps/content/search/tests/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ def test_create_delete_library_block(self, meilisearch_client):
"last_published": None,
"created": created_date.timestamp(),
"modified": created_date.timestamp(),
"publish_status": "never",
}

meilisearch_client.return_value.index.return_value.update_documents.assert_called_with([doc_problem])
Expand All @@ -177,6 +178,7 @@ def test_create_delete_library_block(self, meilisearch_client):
library_api.publish_changes(library.key)
doc_problem["last_published"] = published_date.timestamp()
doc_problem["published"] = {"display_name": "Blank Problem"}
doc_problem["publish_status"] = "published"
meilisearch_client.return_value.index.return_value.update_documents.assert_called_with([doc_problem])

# Delete the Library Block
Expand Down

0 comments on commit 5fb0549

Please sign in to comment.