Skip to content

Commit 15fcff5

Browse files
committed
feat: store content.child_usage_keys in Container search document
Stores the draft children + published children (if applicable) * lib_api.get_container does not take a "user" arg * fetch_customizable_fields_from_container does not need a "user" arg
1 parent 892caa8 commit 15fcff5

5 files changed

Lines changed: 61 additions & 9 deletions

File tree

cms/djangoapps/contentstore/rest_api/v2/views/downstreams.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ def put(self, request: _AuthenticatedRequest, usage_key_string: str) -> Response
267267
fetch_customizable_fields_from_block(downstream=downstream, user=request.user)
268268
else:
269269
assert isinstance(link.upstream_key, LibraryContainerLocator)
270-
fetch_customizable_fields_from_container(downstream=downstream, user=request.user)
270+
fetch_customizable_fields_from_container(downstream=downstream)
271271
except BadDownstream as exc:
272272
logger.exception(
273273
"'%s' is an invalid downstream; refusing to set its upstream to '%s'",

cms/lib/xblock/upstream_sync_container.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def sync_from_upstream_container(
4545
user,
4646
permission=lib_api.permissions.CAN_VIEW_THIS_CONTENT_LIBRARY,
4747
)
48-
upstream_meta = lib_api.get_container(link.upstream_key, user)
48+
upstream_meta = lib_api.get_container(link.upstream_key)
4949
upstream_children = lib_api.get_container_children(link.upstream_key, published=True)
5050
_update_customizable_fields(upstream=upstream_meta, downstream=downstream, only_fetch=False)
5151
_update_non_customizable_fields(upstream=upstream_meta, downstream=downstream)
@@ -54,15 +54,15 @@ def sync_from_upstream_container(
5454
return upstream_children
5555

5656

57-
def fetch_customizable_fields_from_container(*, downstream: XBlock, user: User) -> None:
57+
def fetch_customizable_fields_from_container(*, downstream: XBlock) -> None:
5858
"""
5959
Fetch upstream-defined value of customizable fields and save them on the downstream.
6060
6161
The container version only retrieves values from *published* containers.
6262
6363
Basically, this sets the value of "upstream_display_name" on the downstream block.
6464
"""
65-
upstream = lib_api.get_container(LibraryContainerLocator.from_string(downstream.upstream), user)
65+
upstream = lib_api.get_container(LibraryContainerLocator.from_string(downstream.upstream))
6666
_update_customizable_fields(upstream=upstream, downstream=downstream, only_fetch=True)
6767

6868

openedx/core/djangoapps/content/search/documents.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class Fields:
7171
# The "content" field is a dictionary of arbitrary data, depending on the block_type.
7272
# It comes from each XBlock's index_dictionary() method (if present) plus some processing.
7373
# Text (html) blocks have an "html_content" key in here, capa has "capa_content" and "problem_types", and so on.
74+
# Containers store their list of child usage keys here.
7475
content = "content"
7576

7677
# Collections use this field to communicate how many entities/components they contain.
@@ -87,6 +88,7 @@ class Fields:
8788
published = "published"
8889
published_display_name = "display_name"
8990
published_description = "description"
91+
published_content = "content"
9092
published_num_children = "num_children"
9193

9294
# Note: new fields or values can be added at any time, but if they need to be indexed for filtering or keyword
@@ -576,12 +578,21 @@ def searchable_doc_for_container(
576578
}
577579

578580
try:
579-
container = lib_api.get_container(container_key)
581+
container_obj = lib_api.get_container_from_key(container_key)
582+
container = lib_api.get_container(
583+
container_key,
584+
container=container_obj,
585+
)
580586
except lib_api.ContentLibraryContainerNotFound:
581587
# Container not found, so we can only return the base doc
588+
log.error(f"Container {container_key} not found")
582589
return doc
583590

584-
draft_num_children = lib_api.get_container_children_count(container_key, published=False)
591+
draft_children = lib_api.get_container_children(
592+
container_key,
593+
published=False,
594+
container=container_obj,
595+
)
585596
publish_status = PublishStatus.published
586597
if container.last_published is None:
587598
publish_status = PublishStatus.never
@@ -592,7 +603,13 @@ def searchable_doc_for_container(
592603
Fields.display_name: container.display_name,
593604
Fields.created: container.created.timestamp(),
594605
Fields.modified: container.modified.timestamp(),
595-
Fields.num_children: draft_num_children,
606+
Fields.num_children: len(draft_children),
607+
Fields.content: {
608+
"child_usage_keys": [
609+
str(child.usage_key)
610+
for child in draft_children
611+
],
612+
},
596613
Fields.publish_status: publish_status,
597614
Fields.last_published: container.last_published.timestamp() if container.last_published else None,
598615
})
@@ -601,10 +618,20 @@ def searchable_doc_for_container(
601618
doc[Fields.breadcrumbs] = [{"display_name": library.title}]
602619

603620
if container.published_version_num is not None:
604-
published_num_children = lib_api.get_container_children_count(container_key, published=True)
621+
published_children = lib_api.get_container_children(
622+
container_key,
623+
published=True,
624+
container=container_obj,
625+
)
605626
doc[Fields.published] = {
606-
Fields.published_num_children: published_num_children,
607627
Fields.published_display_name: container.published_display_name,
628+
Fields.published_num_children: len(published_children),
629+
Fields.published_content: {
630+
"child_usage_keys": [
631+
str(child.usage_key)
632+
for child in published_children
633+
],
634+
},
608635
}
609636

610637
return doc

openedx/core/djangoapps/content/search/tests/test_api.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ def setUp(self):
230230
"display_name": "Unit 1",
231231
# description is not set for containers
232232
"num_children": 0,
233+
"content": {"child_usage_keys": []},
233234
"publish_status": "never",
234235
"context_key": "lib:org1:lib",
235236
"org": "org1",

openedx/core/djangoapps/content/search/tests/test_documents.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,9 @@ def test_draft_container(self):
528528
"display_name": "A Unit in the Search Index",
529529
# description is not set for containers
530530
"num_children": 0,
531+
"content": {
532+
"child_usage_keys": [],
533+
},
531534
"publish_status": "never",
532535
"context_key": "lib:edX:2012_Fall",
533536
"access_id": self.library_access_id,
@@ -568,6 +571,11 @@ def test_published_container(self):
568571
"display_name": "A Unit in the Search Index",
569572
# description is not set for containers
570573
"num_children": 1,
574+
"content": {
575+
"child_usage_keys": [
576+
"lb:edX:2012_Fall:html:text2",
577+
],
578+
},
571579
"publish_status": "published",
572580
"context_key": "lib:edX:2012_Fall",
573581
"access_id": self.library_access_id,
@@ -582,6 +590,11 @@ def test_published_container(self):
582590
"published": {
583591
"num_children": 1,
584592
"display_name": "A Unit in the Search Index",
593+
"content": {
594+
"child_usage_keys": [
595+
"lb:edX:2012_Fall:html:text2",
596+
],
597+
},
585598
},
586599
}
587600

@@ -624,6 +637,12 @@ def test_published_container_with_changes(self):
624637
"display_name": "A Unit in the Search Index",
625638
# description is not set for containers
626639
"num_children": 2,
640+
"content": {
641+
"child_usage_keys": [
642+
"lb:edX:2012_Fall:html:text2",
643+
"lb:edX:2012_Fall:html:text3",
644+
],
645+
},
627646
"publish_status": "modified",
628647
"context_key": "lib:edX:2012_Fall",
629648
"access_id": self.library_access_id,
@@ -638,6 +657,11 @@ def test_published_container_with_changes(self):
638657
"published": {
639658
"num_children": 1,
640659
"display_name": "A Unit in the Search Index",
660+
"content": {
661+
"child_usage_keys": [
662+
"lb:edX:2012_Fall:html:text2",
663+
],
664+
},
641665
},
642666
}
643667

0 commit comments

Comments
 (0)