Skip to content

Commit c47b3e0

Browse files
fix: better handling of deletion/un-deletion using openedx-core fixes
1 parent 28ef12e commit c47b3e0

1 file changed

Lines changed: 30 additions & 6 deletions

File tree

  • openedx/core/djangoapps/content_libraries

openedx/core/djangoapps/content_libraries/tasks.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
LIBRARY_CONTAINER_PUBLISHED,
6565
LIBRARY_CONTAINER_UPDATED,
6666
)
67+
from openedx_tagging import api as tagging_api
6768
from user_tasks.models import UserTaskArtifact
6869
from user_tasks.tasks import UserTask, UserTaskStatus
6970
from xblock.fields import Scope
@@ -118,10 +119,11 @@ def send_change_events_for_modified_entities(
118119

119120
for entity in entities:
120121
change = changes_by_entity_id[entity.id]
122+
entity_opaque_key: LibraryUsageLocatorV2 | LibraryContainerLocator
121123
if hasattr(entity, "component"):
122124
# This is a library XBlock (component)
123-
block_key = api.library_component_usage_key(library.library_key, entity.component)
124-
event_data = LibraryBlockData(library_key=library.library_key, usage_key=block_key)
125+
entity_opaque_key = api.library_component_usage_key(library.library_key, entity.component)
126+
event_data = LibraryBlockData(library_key=library.library_key, usage_key=entity_opaque_key)
125127
if change.old_version is None and change.new_version:
126128
# .. event_implemented_name: LIBRARY_BLOCK_CREATED
127129
# .. event_type: org.openedx.content_authoring.library_block.created.v1
@@ -137,8 +139,8 @@ def send_change_events_for_modified_entities(
137139
LIBRARY_BLOCK_UPDATED.send_event(library_block=event_data)
138140

139141
elif hasattr(entity, "container"):
140-
container_key = api.library_container_locator(library.library_key, entity.container)
141-
event_data = LibraryContainerData(container_key=container_key)
142+
entity_opaque_key = api.library_container_locator(library.library_key, entity.container)
143+
event_data = LibraryContainerData(container_key=entity_opaque_key)
142144
if change.old_version is None and change.new_version:
143145
# .. event_implemented_name: LIBRARY_CONTAINER_CREATED
144146
# .. event_type: org.openedx.content_authoring.content_library.container.created.v1
@@ -163,14 +165,36 @@ def send_change_events_for_modified_entities(
163165
# If entities were added/removed from this container, we need to notify things like the search index
164166
# that the list of parent containers for each entity has changed.
165167
check_container_content_changes.delay(
166-
container_key_str=str(container_key),
168+
container_key_str=str(entity_opaque_key),
167169
old_version_id=change.old_version_id,
168170
new_version_id=change.new_version_id,
169-
).forget() # Best practice: free celery result using forget() after calling delay()
171+
)
170172
else:
171173
log.error("Unknown publishable entity type: %s", entity)
172174
continue
173175

176+
if change.restored:
177+
# This block/container was previously soft-deleted and is now un-deleted. It may have tags or collections.
178+
# It would be best to expand the LIBRARY_BLOCK_CREATED event to include the "restored" flag, but in
179+
# the interests of minimizing breaking event changes for now we'll just emit a
180+
# CONTENT_OBJECT_ASSOCIATIONS_CHANGED event to ensure relevant search index records get updated.
181+
association_changes: list[str] = []
182+
if content_api.get_entity_collections(learning_package_id, entity_ref=entity.entity_ref).exists():
183+
association_changes.append("collections") # This entity is part of at least one collection
184+
if tagging_api.get_object_tags(str(entity_opaque_key)).exists():
185+
association_changes.append("tags") # This entity has tags
186+
if association_changes:
187+
# .. event_implemented_name: CONTENT_OBJECT_ASSOCIATIONS_CHANGED
188+
# .. event_type: org.openedx.content_authoring.content.object.associations.changed.v1
189+
CONTENT_OBJECT_ASSOCIATIONS_CHANGED.send_event(
190+
content_object=ContentObjectChangedData(
191+
object_id=str(entity_opaque_key),
192+
changes=association_changes,
193+
),
194+
)
195+
# Notifying parent containers that a child has been restored is not necessary here - they'll already be
196+
# included in 'change_list' [as side effects].
197+
174198

175199
@shared_task(base=LoggedTask)
176200
@set_code_owner_attribute

0 commit comments

Comments
 (0)