fix: Publish components/container in legacy libraries migration (#37644)
- Fix the issue described in https://github.com/openedx/frontend-app-authoring/issues/2626 - Publish components and containers after migrate
This commit is contained in:
@@ -1077,7 +1077,8 @@ def _migrate_container(
|
||||
entity_id=container.container_pk,
|
||||
version_num=container.draft_version_num,
|
||||
)
|
||||
return authoring_api.create_next_container_version(
|
||||
|
||||
container_publishable_entity_version = authoring_api.create_next_container_version(
|
||||
container.container_pk,
|
||||
title=title,
|
||||
entity_rows=[
|
||||
@@ -1089,6 +1090,17 @@ def _migrate_container(
|
||||
container_version_cls=container_type.container_model_classes[1],
|
||||
).publishable_entity_version
|
||||
|
||||
# Publish the container
|
||||
# Call post publish events synchronously to avoid
|
||||
# an error when calling `wait_for_post_publish_events`
|
||||
# inside a celery task.
|
||||
libraries_api.publish_container_changes(
|
||||
container.container_key,
|
||||
context.created_by,
|
||||
call_post_publish_events_sync=True,
|
||||
)
|
||||
return container_publishable_entity_version
|
||||
|
||||
|
||||
def _migrate_component(
|
||||
*,
|
||||
@@ -1153,6 +1165,12 @@ def _migrate_component(
|
||||
authoring_api.create_component_version_content(
|
||||
component_version.pk, content_pk, key=new_path
|
||||
)
|
||||
|
||||
# Publish the component
|
||||
libraries_api.publish_component_changes(
|
||||
libraries_api.library_component_usage_key(context.target_library_key, component),
|
||||
context.created_by,
|
||||
)
|
||||
return component_version.publishable_entity_version
|
||||
|
||||
|
||||
|
||||
@@ -440,6 +440,9 @@ class TestMigrateFromModulestore(ModuleStoreTestCase):
|
||||
"problem", result.componentversion.component.component_type.name
|
||||
)
|
||||
|
||||
# The component is published
|
||||
self.assertFalse(result.componentversion.component.versioning.has_unpublished_changes)
|
||||
|
||||
def test_migrate_component_with_static_content(self):
|
||||
"""
|
||||
Test _migrate_component with static file content
|
||||
@@ -897,6 +900,8 @@ class TestMigrateFromModulestore(ModuleStoreTestCase):
|
||||
|
||||
container_version = result.containerversion
|
||||
self.assertEqual(container_version.title, f"Test {block_type.title()}")
|
||||
# The container is published
|
||||
self.assertFalse(authoring_api.contains_unpublished_changes(container_version.container.pk))
|
||||
|
||||
def test_migrate_container_replace_existing_false(self):
|
||||
"""
|
||||
|
||||
@@ -956,7 +956,7 @@ def delete_library_block_static_asset_file(usage_key, file_path, user=None):
|
||||
)
|
||||
|
||||
|
||||
def publish_component_changes(usage_key: LibraryUsageLocatorV2, user: UserType):
|
||||
def publish_component_changes(usage_key: LibraryUsageLocatorV2, user_id: int):
|
||||
"""
|
||||
Publish all pending changes in a single component.
|
||||
"""
|
||||
@@ -969,7 +969,7 @@ def publish_component_changes(usage_key: LibraryUsageLocatorV2, user: UserType):
|
||||
drafts_to_publish = authoring_api.get_all_drafts(learning_package.id).filter(entity__key=component.key)
|
||||
# Publish the component and update anything that needs to be updated (e.g. search index):
|
||||
publish_log = authoring_api.publish_from_drafts(
|
||||
learning_package.id, draft_qset=drafts_to_publish, published_by=user.id,
|
||||
learning_package.id, draft_qset=drafts_to_publish, published_by=user_id,
|
||||
)
|
||||
# Since this is a single component, it should be safe to process synchronously and in-process:
|
||||
tasks.send_events_after_publish(publish_log.pk, str(library_key))
|
||||
|
||||
@@ -575,7 +575,11 @@ def get_containers_contains_item(
|
||||
]
|
||||
|
||||
|
||||
def publish_container_changes(container_key: LibraryContainerLocator, user_id: int | None) -> None:
|
||||
def publish_container_changes(
|
||||
container_key: LibraryContainerLocator,
|
||||
user_id: int | None,
|
||||
call_post_publish_events_sync=False,
|
||||
) -> None:
|
||||
"""
|
||||
[ 🛑 UNSTABLE ] Publish all unpublished changes in a container and all its child
|
||||
containers/blocks.
|
||||
@@ -595,7 +599,10 @@ def publish_container_changes(container_key: LibraryContainerLocator, user_id: i
|
||||
)
|
||||
# Update the search index (and anything else) for the affected container + blocks
|
||||
# This is mostly synchronous but may complete some work asynchronously if there are a lot of changes.
|
||||
tasks.wait_for_post_publish_events(publish_log, library_key)
|
||||
if call_post_publish_events_sync:
|
||||
tasks.send_events_after_publish(publish_log.pk, str(library_key))
|
||||
else:
|
||||
tasks.wait_for_post_publish_events(publish_log, library_key)
|
||||
|
||||
|
||||
def copy_container(container_key: LibraryContainerLocator, user_id: int) -> UserClipboardData:
|
||||
|
||||
@@ -241,7 +241,7 @@ class LibraryBlockPublishView(APIView):
|
||||
request.user,
|
||||
authz_permissions.PUBLISH_LIBRARY_CONTENT
|
||||
)
|
||||
api.publish_component_changes(key, request.user)
|
||||
api.publish_component_changes(key, request.user.id)
|
||||
return Response({})
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user