Skip to content

Commit 1d94788

Browse files
committed
feat: Send LIBRARY_CONTAINER_UPDATED signal when updating component of container
1 parent 3b49af3 commit 1d94788

5 files changed

Lines changed: 67 additions & 12 deletions

File tree

openedx/core/djangoapps/content_libraries/api/libraries.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,18 @@ def set_library_block_olx(usage_key: LibraryUsageLocatorV2, new_olx_str: str) ->
904904
)
905905
)
906906

907+
# For each container, trigger LIBRARY_CONTAINER_UPDATED signal and set background=True to trigger
908+
# container indexing asynchronously.
909+
affected_containers = lib_api.get_containers_contains_component(usage_key)
910+
for container in affected_containers:
911+
LIBRARY_CONTAINER_UPDATED.send_event(
912+
library_container=LibraryContainerData(
913+
library_key=usage_key.lib_key,
914+
container_key=container.container_pk,
915+
background=True,
916+
)
917+
)
918+
907919
return new_component_version
908920

909921

openedx/core/djangoapps/content_libraries/library_context.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
from django.core.exceptions import PermissionDenied
77
from rest_framework.exceptions import NotFound
88

9-
from openedx_events.content_authoring.data import LibraryBlockData
10-
from openedx_events.content_authoring.signals import LIBRARY_BLOCK_UPDATED
9+
from openedx_events.content_authoring.data import LibraryBlockData, LibraryContainerData
10+
from openedx_events.content_authoring.signals import LIBRARY_BLOCK_UPDATED, LIBRARY_CONTAINER_UPDATED
1111
from opaque_keys.edx.keys import UsageKeyV2
1212
from opaque_keys.edx.locator import LibraryUsageLocatorV2, LibraryLocatorV2
1313
from openedx_learning.api import authoring as authoring_api
@@ -114,3 +114,19 @@ def send_block_updated_event(self, usage_key: UsageKeyV2):
114114
usage_key=usage_key,
115115
)
116116
)
117+
118+
def send_container_updated_events(self, usage_key: UsageKeyV2):
119+
"""
120+
Send "container updated" events for containers that contains the library block
121+
with the given usage_key.
122+
"""
123+
assert isinstance(usage_key, LibraryUsageLocatorV2)
124+
affected_containers = api.get_containers_contains_component(usage_key)
125+
for container in affected_containers:
126+
LIBRARY_CONTAINER_UPDATED.send_event(
127+
library_container=LibraryContainerData(
128+
library_key=usage_key.lib_key,
129+
container_key=container.container_pk,
130+
background=True,
131+
)
132+
)

openedx/core/djangoapps/content_libraries/tests/test_api.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -812,14 +812,8 @@ def test_get_containers_contains_component(self):
812812
assert html_block_containers[1].container_key == self.unit2.container_key
813813

814814

815-
def test_call_container_update_signal_when_delete_component(self):
816-
container_update_event_receiver = mock.Mock()
817-
LIBRARY_CONTAINER_UPDATED.connect(container_update_event_receiver)
818-
819-
api.delete_library_block(self.html_block_usage_key)
820-
821-
assert container_update_event_receiver.call_count == 2
822-
815+
def _validate_calls_of_html_block(self, event_mock):
816+
assert event_mock.call_count == 2
823817
self.assertDictContainsSubset(
824818
{
825819
"signal": LIBRARY_CONTAINER_UPDATED,
@@ -830,7 +824,7 @@ def test_call_container_update_signal_when_delete_component(self):
830824
background=True,
831825
)
832826
},
833-
container_update_event_receiver.call_args_list[0].kwargs,
827+
event_mock.call_args_list[0].kwargs,
834828
)
835829
self.assertDictContainsSubset(
836830
{
@@ -842,5 +836,29 @@ def test_call_container_update_signal_when_delete_component(self):
842836
background=True,
843837
)
844838
},
845-
container_update_event_receiver.call_args_list[1].kwargs,
839+
event_mock.call_args_list[1].kwargs,
846840
)
841+
842+
def test_call_container_update_signal_when_delete_component(self):
843+
container_update_event_receiver = mock.Mock()
844+
LIBRARY_CONTAINER_UPDATED.connect(container_update_event_receiver)
845+
846+
api.delete_library_block(self.html_block_usage_key)
847+
self._validate_calls_of_html_block(container_update_event_receiver)
848+
849+
850+
def test_call_container_update_signal_when_update_olx(self):
851+
block_olx = "<html><b>Hello world!</b></html>"
852+
container_update_event_receiver = mock.Mock()
853+
LIBRARY_CONTAINER_UPDATED.connect(container_update_event_receiver)
854+
855+
self._set_library_block_olx(self.html_block_usage_key, block_olx)
856+
self._validate_calls_of_html_block(container_update_event_receiver)
857+
858+
def test_call_container_update_signal_when_update_component(self):
859+
block_olx = "<html><b>Hello world!</b></html>"
860+
container_update_event_receiver = mock.Mock()
861+
LIBRARY_CONTAINER_UPDATED.connect(container_update_event_receiver)
862+
863+
self._set_library_block_fields(self.html_block_usage_key, {"data": block_olx, "metadata": {}})
864+
self._validate_calls_of_html_block(container_update_event_receiver)

openedx/core/djangoapps/xblock/learning_context/learning_context.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,11 @@ def send_block_updated_event(self, usage_key):
7676
7777
usage_key: the UsageKeyV2 subclass used for this learning context
7878
"""
79+
80+
def send_container_updated_events(self, usage_key):
81+
"""
82+
Send "container updated" events for containers that contains the block with
83+
the given usage_key in this context.
84+
85+
usage_key: the UsageKeyV2 subclass used for this learning context
86+
"""

openedx/core/djangoapps/xblock/runtime/learning_core_runtime.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ def save_block(self, block):
317317
# Signal that we've modified this block
318318
learning_context = get_learning_context_impl(usage_key)
319319
learning_context.send_block_updated_event(usage_key)
320+
learning_context.send_container_updated_events(usage_key)
320321

321322
def _get_component_from_usage_key(self, usage_key):
322323
"""

0 commit comments

Comments
 (0)