Skip to content

Commit b9e5683

Browse files
authored
fix: Publish components/container in legacy libraries migration (#37644)
- Fix the issue described in openedx/frontend-app-authoring#2626 - Publish components and containers after migrate
1 parent d516736 commit b9e5683

File tree

5 files changed

+36
-6
lines changed

5 files changed

+36
-6
lines changed

cms/djangoapps/modulestore_migrator/tasks.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,8 @@ def _migrate_container(
10771077
entity_id=container.container_pk,
10781078
version_num=container.draft_version_num,
10791079
)
1080-
return authoring_api.create_next_container_version(
1080+
1081+
container_publishable_entity_version = authoring_api.create_next_container_version(
10811082
container.container_pk,
10821083
title=title,
10831084
entity_rows=[
@@ -1089,6 +1090,17 @@ def _migrate_container(
10891090
container_version_cls=container_type.container_model_classes[1],
10901091
).publishable_entity_version
10911092

1093+
# Publish the container
1094+
# Call post publish events synchronously to avoid
1095+
# an error when calling `wait_for_post_publish_events`
1096+
# inside a celery task.
1097+
libraries_api.publish_container_changes(
1098+
container.container_key,
1099+
context.created_by,
1100+
call_post_publish_events_sync=True,
1101+
)
1102+
return container_publishable_entity_version
1103+
10921104

10931105
def _migrate_component(
10941106
*,
@@ -1153,6 +1165,12 @@ def _migrate_component(
11531165
authoring_api.create_component_version_content(
11541166
component_version.pk, content_pk, key=new_path
11551167
)
1168+
1169+
# Publish the component
1170+
libraries_api.publish_component_changes(
1171+
libraries_api.library_component_usage_key(context.target_library_key, component),
1172+
context.created_by,
1173+
)
11561174
return component_version.publishable_entity_version
11571175

11581176

cms/djangoapps/modulestore_migrator/tests/test_tasks.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,9 @@ def test_migrate_component_success(self):
440440
"problem", result.componentversion.component.component_type.name
441441
)
442442

443+
# The component is published
444+
self.assertFalse(result.componentversion.component.versioning.has_unpublished_changes)
445+
443446
def test_migrate_component_with_static_content(self):
444447
"""
445448
Test _migrate_component with static file content
@@ -897,6 +900,8 @@ def test_migrate_container_different_container_types(self):
897900

898901
container_version = result.containerversion
899902
self.assertEqual(container_version.title, f"Test {block_type.title()}")
903+
# The container is published
904+
self.assertFalse(authoring_api.contains_unpublished_changes(container_version.container.pk))
900905

901906
def test_migrate_container_replace_existing_false(self):
902907
"""

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,7 @@ def delete_library_block_static_asset_file(usage_key, file_path, user=None):
956956
)
957957

958958

959-
def publish_component_changes(usage_key: LibraryUsageLocatorV2, user: UserType):
959+
def publish_component_changes(usage_key: LibraryUsageLocatorV2, user_id: int):
960960
"""
961961
Publish all pending changes in a single component.
962962
"""
@@ -969,7 +969,7 @@ def publish_component_changes(usage_key: LibraryUsageLocatorV2, user: UserType):
969969
drafts_to_publish = authoring_api.get_all_drafts(learning_package.id).filter(entity__key=component.key)
970970
# Publish the component and update anything that needs to be updated (e.g. search index):
971971
publish_log = authoring_api.publish_from_drafts(
972-
learning_package.id, draft_qset=drafts_to_publish, published_by=user.id,
972+
learning_package.id, draft_qset=drafts_to_publish, published_by=user_id,
973973
)
974974
# Since this is a single component, it should be safe to process synchronously and in-process:
975975
tasks.send_events_after_publish(publish_log.pk, str(library_key))

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,11 @@ def get_containers_contains_item(
575575
]
576576

577577

578-
def publish_container_changes(container_key: LibraryContainerLocator, user_id: int | None) -> None:
578+
def publish_container_changes(
579+
container_key: LibraryContainerLocator,
580+
user_id: int | None,
581+
call_post_publish_events_sync=False,
582+
) -> None:
579583
"""
580584
[ 🛑 UNSTABLE ] Publish all unpublished changes in a container and all its child
581585
containers/blocks.
@@ -595,7 +599,10 @@ def publish_container_changes(container_key: LibraryContainerLocator, user_id: i
595599
)
596600
# Update the search index (and anything else) for the affected container + blocks
597601
# This is mostly synchronous but may complete some work asynchronously if there are a lot of changes.
598-
tasks.wait_for_post_publish_events(publish_log, library_key)
602+
if call_post_publish_events_sync:
603+
tasks.send_events_after_publish(publish_log.pk, str(library_key))
604+
else:
605+
tasks.wait_for_post_publish_events(publish_log, library_key)
599606

600607

601608
def copy_container(container_key: LibraryContainerLocator, user_id: int) -> UserClipboardData:

openedx/core/djangoapps/content_libraries/rest_api/blocks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ def post(self, request, usage_key_str):
241241
request.user,
242242
authz_permissions.PUBLISH_LIBRARY_CONTENT
243243
)
244-
api.publish_component_changes(key, request.user)
244+
api.publish_component_changes(key, request.user.id)
245245
return Response({})
246246

247247

0 commit comments

Comments
 (0)