Skip to content

Commit 6500856

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

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
@@ -963,16 +963,18 @@ def _migrate_component(
963963
return component.versioning.draft.publishable_entity_version, None
964964

965965
# If component existed and was deleted or we have to replace the current version
966-
# Create the new component version for it
967-
component_version = libraries_api.set_library_block_olx(target_key, new_olx_str=olx)
966+
paths_to_media_ids = {}
968967
for filename, media_pk in context.content_by_filename.items():
969968
filename_no_ext, _ = os.path.splitext(filename)
970969
if filename_no_ext not in olx:
971970
continue
972971
new_path = f"static/{filename}"
973-
content_api.create_component_version_media(
974-
component_version.pk, media_pk, path=new_path
975-
)
972+
paths_to_media_ids[new_path] = media_pk
973+
974+
# Create the new component version for it
975+
component_version = libraries_api.set_library_block_olx(
976+
target_key, new_olx_str=olx, paths_to_media=paths_to_media_ids,
977+
)
976978

977979
# Publish the component
978980
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
@@ -193,7 +193,11 @@ def get_library_block(usage_key: LibraryUsageLocatorV2, include_collections=Fals
193193
return xblock_metadata
194194

195195

196-
def set_library_block_olx(usage_key: LibraryUsageLocatorV2, new_olx_str: str) -> ComponentVersion:
196+
def set_library_block_olx(
197+
usage_key: LibraryUsageLocatorV2,
198+
new_olx_str: str,
199+
paths_to_media: dict = None,
200+
) -> ComponentVersion:
197201
"""
198202
Replace the OLX source of the given XBlock.
199203
@@ -204,6 +208,7 @@ def set_library_block_olx(usage_key: LibraryUsageLocatorV2, new_olx_str: str) ->
204208
Returns the version number of the newly created ComponentVersion.
205209
"""
206210
assert isinstance(usage_key, LibraryUsageLocatorV2)
211+
paths_to_media = paths_to_media or {}
207212

208213
# HTMLBlock uses CDATA to preserve HTML inside the XML, so make sure we
209214
# don't strip that out.
@@ -240,7 +245,7 @@ def set_library_block_olx(usage_key: LibraryUsageLocatorV2, new_olx_str: str) ->
240245
now = datetime.now(tz=timezone.utc) # noqa: UP017
241246

242247
with transaction.atomic():
243-
new_content = content_api.get_or_create_text_media(
248+
new_olx_media = content_api.get_or_create_text_media(
244249
component.learning_package_id,
245250
get_or_create_olx_media_type(usage_key.block_type).id,
246251
text=new_olx_str,
@@ -250,7 +255,8 @@ def set_library_block_olx(usage_key: LibraryUsageLocatorV2, new_olx_str: str) ->
250255
component.id,
251256
title=new_title,
252257
media_to_replace={
253-
'block.xml': new_content.pk,
258+
**paths_to_media,
259+
'block.xml': new_olx_media.pk,
254260
},
255261
created=now,
256262
)
@@ -433,11 +439,7 @@ def _import_staged_block(
433439
created_by=user.id,
434440
)
435441

436-
# This will create the first component version and set the OLX/title
437-
# appropriately. It will not publish. Once we get the newly created
438-
# ComponentVersion back from this, we can attach all our files to it.
439-
component_version = set_library_block_olx(usage_key, olx_str)
440-
442+
paths_to_media = {}
441443
for staged_content_file_data in staged_content_files:
442444
# The ``data`` attribute is going to be None because the clipboard
443445
# is optimized to not do redundant file copying when copying/pasting
@@ -484,17 +486,18 @@ def _import_staged_block(
484486
media_type_str = "application/octet-stream"
485487

486488
media_type = content_api.get_or_create_media_type(media_type_str)
487-
content = content_api.get_or_create_file_media(
489+
media = content_api.get_or_create_file_media(
488490
learning_package.id,
489491
media_type.id,
490492
data=file_data,
491493
created=now,
492494
)
493-
content_api.create_component_version_media(
494-
component_version.pk,
495-
content.id,
496-
path=filename,
497-
)
495+
paths_to_media[filename] = media.id
496+
497+
# This will create the first component version and set the OLX/title
498+
# appropriately. It will not publish. Once we get the newly created
499+
# ComponentVersion back from this, we can attach all our files to it.
500+
set_library_block_olx(usage_key, olx_str, paths_to_media)
498501

499502
# Emit library block created event
500503
# .. event_implemented_name: LIBRARY_BLOCK_CREATED
@@ -1046,25 +1049,23 @@ def _create_component_for_block(
10461049
component_type = content_api.get_or_create_component_type(
10471050
"xblock.v1", usage_key.block_type
10481051
)
1049-
component, component_version = content_api.create_component_and_version(
1052+
block_olx_media = content_api.get_or_create_text_media(
1053+
learning_package.id,
1054+
get_or_create_olx_media_type(usage_key.block_type).id,
1055+
text=xml_text,
1056+
created=now,
1057+
)
1058+
_component, component_version = content_api.create_component_and_version(
10501059
learning_package.id,
10511060
component_type=component_type,
10521061
component_code=usage_key.block_id,
10531062
title=display_name,
10541063
created=now,
10551064
created_by=user_id,
10561065
can_stand_alone=can_stand_alone,
1057-
)
1058-
content = content_api.get_or_create_text_media(
1059-
learning_package.id,
1060-
get_or_create_olx_media_type(usage_key.block_type).id,
1061-
text=xml_text,
1062-
created=now,
1063-
)
1064-
content_api.create_component_version_media(
1065-
component_version.pk,
1066-
content.id,
1067-
path="block.xml",
1066+
media={
1067+
'block.xml': block_olx_media
1068+
}
10681069
)
10691070

10701071
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.45
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)