@@ -399,7 +399,11 @@ def get_library_component_creation_entry(
399399 )
400400
401401
402- def set_library_block_olx (usage_key : LibraryUsageLocatorV2 , new_olx_str : str ) -> ComponentVersion :
402+ def set_library_block_olx (
403+ usage_key : LibraryUsageLocatorV2 ,
404+ new_olx_str : str ,
405+ paths_to_media : dict = None ,
406+ ) -> ComponentVersion :
403407 """
404408 Replace the OLX source of the given XBlock.
405409
@@ -410,6 +414,7 @@ def set_library_block_olx(usage_key: LibraryUsageLocatorV2, new_olx_str: str) ->
410414 Returns the version number of the newly created ComponentVersion.
411415 """
412416 assert isinstance (usage_key , LibraryUsageLocatorV2 )
417+ paths_to_media = paths_to_media or {}
413418
414419 # HTMLBlock uses CDATA to preserve HTML inside the XML, so make sure we
415420 # don't strip that out.
@@ -446,7 +451,7 @@ def set_library_block_olx(usage_key: LibraryUsageLocatorV2, new_olx_str: str) ->
446451 now = datetime .now (tz = timezone .utc ) # noqa: UP017
447452
448453 with transaction .atomic ():
449- new_content = content_api .get_or_create_text_media (
454+ new_olx_media = content_api .get_or_create_text_media (
450455 component .learning_package_id ,
451456 get_or_create_olx_media_type (usage_key .block_type ).id ,
452457 text = new_olx_str ,
@@ -456,7 +461,8 @@ def set_library_block_olx(usage_key: LibraryUsageLocatorV2, new_olx_str: str) ->
456461 component .id ,
457462 title = new_title ,
458463 media_to_replace = {
459- 'block.xml' : new_content .pk ,
464+ ** paths_to_media ,
465+ 'block.xml' : new_olx_media .pk ,
460466 },
461467 created = now ,
462468 )
@@ -606,11 +612,7 @@ def _import_staged_block(
606612 created_by = user .id ,
607613 )
608614
609- # This will create the first component version and set the OLX/title
610- # appropriately. It will not publish. Once we get the newly created
611- # ComponentVersion back from this, we can attach all our files to it.
612- component_version = set_library_block_olx (usage_key , olx_str )
613-
615+ paths_to_media = {}
614616 for staged_content_file_data in staged_content_files :
615617 # The ``data`` attribute is going to be None because the clipboard
616618 # is optimized to not do redundant file copying when copying/pasting
@@ -657,17 +659,18 @@ def _import_staged_block(
657659 media_type_str = "application/octet-stream"
658660
659661 media_type = content_api .get_or_create_media_type (media_type_str )
660- content = content_api .get_or_create_file_media (
662+ media = content_api .get_or_create_file_media (
661663 learning_package .id ,
662664 media_type .id ,
663665 data = file_data ,
664666 created = now ,
665667 )
666- content_api .create_component_version_media (
667- component_version .pk ,
668- content .id ,
669- path = filename ,
670- )
668+ paths_to_media [filename ] = media .id
669+
670+ # This will create the first component version and set the OLX/title
671+ # appropriately. It will not publish. Once we get the newly created
672+ # ComponentVersion back from this, we can attach all our files to it.
673+ set_library_block_olx (usage_key , olx_str , paths_to_media )
671674
672675 # Now return the metadata about the new block
673676 return get_library_block (usage_key )
@@ -1087,25 +1090,23 @@ def _create_component_for_block(
10871090 component_type = content_api .get_or_create_component_type (
10881091 "xblock.v1" , usage_key .block_type
10891092 )
1090- component , component_version = content_api .create_component_and_version (
1093+ block_olx_media = content_api .get_or_create_text_media (
1094+ learning_package .id ,
1095+ get_or_create_olx_media_type (usage_key .block_type ).id ,
1096+ text = xml_text ,
1097+ created = now ,
1098+ )
1099+ _component , component_version = content_api .create_component_and_version (
10911100 learning_package .id ,
10921101 component_type = component_type ,
10931102 component_code = usage_key .block_id ,
10941103 title = display_name ,
10951104 created = now ,
10961105 created_by = user_id ,
10971106 can_stand_alone = can_stand_alone ,
1098- )
1099- content = content_api .get_or_create_text_media (
1100- learning_package .id ,
1101- get_or_create_olx_media_type (usage_key .block_type ).id ,
1102- text = xml_text ,
1103- created = now ,
1104- )
1105- content_api .create_component_version_media (
1106- component_version .pk ,
1107- content .id ,
1108- path = "block.xml" ,
1107+ media = {
1108+ 'block.xml' : block_olx_media
1109+ }
11091110 )
11101111
11111112 return component_version
0 commit comments