Skip to content

Commit e49d898

Browse files
committed
refactor: assign media at time of component version creation
1 parent f9f3943 commit e49d898

3 files changed

Lines changed: 35 additions & 32 deletions

File tree

cms/djangoapps/modulestore_migrator/tasks.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -959,16 +959,18 @@ def _migrate_component(
959959
return component.versioning.draft.publishable_entity_version, None
960960

961961
# If component existed and was deleted or we have to replace the current version
962-
# Create the new component version for it
963-
component_version = libraries_api.set_library_block_olx(target_key, new_olx_str=olx)
962+
paths_to_media_ids = {}
964963
for filename, media_pk in context.content_by_filename.items():
965964
filename_no_ext, _ = os.path.splitext(filename)
966965
if filename_no_ext not in olx:
967966
continue
968967
new_path = f"static/{filename}"
969-
content_api.create_component_version_media(
970-
component_version.pk, media_pk, path=new_path
971-
)
968+
paths_to_media_ids[new_path] = media_pk
969+
970+
# Create the new component version for it
971+
component_version = libraries_api.set_library_block_olx(
972+
target_key, new_olx_str=olx, paths_to_media=paths_to_media_ids,
973+
)
972974

973975
# Publish the component
974976
libraries_api.publish_component_changes(target_key, context.created_by)

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

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -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

requirements/constraints.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ numpy<2.0.0
6565
# breaking changes which openedx-core devs want to roll out manually. New patch versions
6666
# are OK to accept automatically.
6767
# Issue for unpinning: https://github.com/openedx/edx-platform/issues/35269
68-
openedx-core<0.47
68+
openedx-core<0.48
6969

7070
# Date: 2023-11-29
7171
# Open AI version 1.0.0 dropped support for openai.ChatCompletion which is currently in use in enterprise.

0 commit comments

Comments
 (0)