Skip to content

Commit

Permalink
feat: add publish status to library meilisearch index [FC-0076] (#36031)
Browse files Browse the repository at this point in the history
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 authored Feb 5, 2025
1 parent e1a8b52 commit 182bfc4
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
20 changes: 20 additions & 0 deletions openedx/core/djangoapps/content/search/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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({
Expand All @@ -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))
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
17 changes: 17 additions & 0 deletions openedx/core/djangoapps/content/search/tests/test_documents.py
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,8 +423,22 @@ def test_html_published_library_block(self):
"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))
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 182bfc4

Please sign in to comment.