feat: add publish status to library meilisearch index [FC-0076] (#36031)
Adds the publish status field to the libraries v2 meilisearch index in order to support filtering by component publish status: published, modified, never.
This commit is contained in:
committed by
GitHub
parent
e1a8b52dc2
commit
182bfc4031
@@ -77,6 +77,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"
|
||||
@@ -97,6 +103,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
|
||||
@@ -380,11 +395,15 @@ 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
|
||||
except NotFound:
|
||||
# Never published
|
||||
block_published = None
|
||||
publish_status = PublishStatus.never
|
||||
|
||||
doc = searchable_doc_for_usage_key(xblock_metadata.usage_key)
|
||||
doc.update({
|
||||
@@ -393,6 +412,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))
|
||||
|
||||
@@ -25,6 +25,7 @@ INDEX_FILTERABLE_ATTRIBUTES = [
|
||||
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:
|
||||
|
||||
@@ -148,6 +148,7 @@ class TestSearchApi(ModuleStoreTestCase):
|
||||
"last_published": None,
|
||||
"created": created_date.timestamp(),
|
||||
"modified": modified_date.timestamp(),
|
||||
"publish_status": "never",
|
||||
}
|
||||
self.doc_problem2 = {
|
||||
"id": "lborg1libproblemp2-b2f65e29",
|
||||
@@ -164,6 +165,7 @@ class TestSearchApi(ModuleStoreTestCase):
|
||||
"last_published": None,
|
||||
"created": created_date.timestamp(),
|
||||
"modified": created_date.timestamp(),
|
||||
"publish_status": "never",
|
||||
}
|
||||
|
||||
# Create a couple of taxonomies with tags
|
||||
|
||||
@@ -298,6 +298,7 @@ class StudioDocumentsTest(SharedModuleStoreTestCase):
|
||||
"taxonomy": ["Difficulty"],
|
||||
"level0": ["Difficulty > Normal"],
|
||||
},
|
||||
"publish_status": "never",
|
||||
}
|
||||
|
||||
def test_html_published_library_block(self):
|
||||
@@ -337,6 +338,7 @@ class StudioDocumentsTest(SharedModuleStoreTestCase):
|
||||
"level0": ["Difficulty > Normal"],
|
||||
},
|
||||
'published': {'display_name': 'Text'},
|
||||
"publish_status": "published",
|
||||
}
|
||||
|
||||
# Update library block to create a draft
|
||||
@@ -378,6 +380,7 @@ class StudioDocumentsTest(SharedModuleStoreTestCase):
|
||||
"level0": ["Difficulty > Normal"],
|
||||
},
|
||||
"published": {"display_name": "Text"},
|
||||
"publish_status": "published",
|
||||
}
|
||||
|
||||
# Publish new changes
|
||||
@@ -420,8 +423,22 @@ class StudioDocumentsTest(SharedModuleStoreTestCase):
|
||||
"display_name": "Text 2",
|
||||
"description": "This is a Test",
|
||||
},
|
||||
"publish_status": "published",
|
||||
}
|
||||
|
||||
# Verify publish status is set to modified
|
||||
old_modified = self.library_block.modified
|
||||
old_published = self.library_block.last_published
|
||||
self.library_block.modified = datetime(2024, 4, 5, 6, 7, 8, tzinfo=timezone.utc)
|
||||
self.library_block.last_published = datetime(2023, 4, 5, 6, 7, 8, tzinfo=timezone.utc)
|
||||
doc = searchable_doc_for_library_block(self.library_block)
|
||||
doc.update(searchable_doc_tags(self.library_block.usage_key))
|
||||
doc.update(searchable_doc_collections(self.library_block.usage_key))
|
||||
assert doc["publish_status"] == "modified"
|
||||
|
||||
self.library_block.modified = old_modified
|
||||
self.library_block.last_published = old_published
|
||||
|
||||
def test_collection_with_library(self):
|
||||
doc = searchable_doc_for_collection(self.library.key, self.collection.key)
|
||||
doc.update(searchable_doc_tags_for_collection(self.library.key, self.collection.key))
|
||||
|
||||
@@ -153,6 +153,7 @@ class TestUpdateIndexHandlers(ModuleStoreTestCase, LiveServerTestCase):
|
||||
"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])
|
||||
@@ -177,6 +178,7 @@ class TestUpdateIndexHandlers(ModuleStoreTestCase, LiveServerTestCase):
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user