Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add publish status to library meilisearch index #36031

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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,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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm.. technically this status can be determined using filters on the existing xblock document metadata fields, so we don't strictly need a field publish_status field in the search index. e.g to filter out unpublished items when sorting by "Recently Published", we add a last_published IS NOT NULL filter.

But I'm not sure what our stance is on having too much logic living in the frontend.. @bradenmacdonald what do you think?


doc = searchable_doc_for_usage_key(xblock_metadata.usage_key)
doc.update({
Expand All @@ -389,6 +408,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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(If we end up keeping this field) could you please add a test for "publish_status": "modified"?

}

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
Loading