Skip to content

Commit 39d3dc8

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

1 file changed

Lines changed: 27 additions & 26 deletions

File tree

  • openedx/core/djangoapps/content_libraries/api

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

0 commit comments

Comments
 (0)