From fd211247ecaa8542351b94d767902e186801cc17 Mon Sep 17 00:00:00 2001 From: David Ormsbee Date: Sat, 28 Sep 2024 14:34:04 -0400 Subject: [PATCH] feat: get copy to take Learning Core static assets and put them into staged content --- .../xblock/runtime/learning_core_runtime.py | 37 +++++++++++++++++++ .../lib/xblock_serializer/block_serializer.py | 23 ++++++++++-- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/openedx/core/djangoapps/xblock/runtime/learning_core_runtime.py b/openedx/core/djangoapps/xblock/runtime/learning_core_runtime.py index ec5fb73f49c6..b95de8ae71d1 100644 --- a/openedx/core/djangoapps/xblock/runtime/learning_core_runtime.py +++ b/openedx/core/djangoapps/xblock/runtime/learning_core_runtime.py @@ -21,6 +21,7 @@ from xblock.field_data import FieldData from openedx.core.lib.xblock_serializer.api import serialize_modulestore_block_for_learning_core +from openedx.core.lib.xblock_serializer.data import StaticFile from ..learning_context.manager import get_learning_context_impl from .runtime import XBlockRuntime @@ -216,6 +217,42 @@ def get_block(self, usage_key, for_parent=None): return block + def get_block_assets(self, usage_key): + """ + This currently doesn't copy any + """ + component = self._get_component_from_usage_key(usage_key) + component_version = component.versioning.draft + + # If there is no Draft version, then this was soft-deleted + if component_version is None: + return [] + + # cvc = the ComponentVersionContent through table + cvc_set = ( + component_version + .componentversioncontent_set + .filter(content__has_file=True) + .order_by('key') + .select_related('content') + ) + + return [ + StaticFile( + name=cvc.key, + url=None, +# url=reverse( +# 'content_libraries:library-assets', +# kwargs={ +# 'component_version_uuid': component_version.uuid, +# 'asset_path': cvc.key, +# } +# ), + data=cvc.content.read_file().read() + ) + for cvc in cvc_set + ] + def save_block(self, block): """ Save any pending field data values to Learning Core data models. diff --git a/openedx/core/lib/xblock_serializer/block_serializer.py b/openedx/core/lib/xblock_serializer/block_serializer.py index e45e1392b143..4b5678b13625 100644 --- a/openedx/core/lib/xblock_serializer/block_serializer.py +++ b/openedx/core/lib/xblock_serializer/block_serializer.py @@ -38,10 +38,25 @@ def __init__(self, block): # Search the OLX for references to files stored in the course's # "Files & Uploads" (contentstore): self.olx_str = utils.rewrite_absolute_static_urls(self.olx_str, course_key) - # for asset in utils.collect_assets_from_text(self.olx_str, course_key): - # path = asset['path'] - # if path not in [sf.name for sf in self.static_files]: - # self.static_files.append(StaticFile(name=path, url=asset['url'], data=None)) + + # If a block supports explicit assets, there's no need to scan the + # content looking for static assets that it *might* be using. Learning + # Core backed content supports this, which currently means v2 Content + # Libraries. + runtime_supports_explicit_assets = hasattr(block.runtime, 'get_block_assets') + if runtime_supports_explicit_assets: + self.static_files.extend( + block.runtime.get_block_assets(block.scope_ids.usage_id) + ) + print(f"static_files: {self.static_files}") + return + + # Otherwise, we have to scan the content to extract associated asset + # files that are stored in the course-global Files and Uploads. + for asset in utils.collect_assets_from_text(self.olx_str, course_key): + path = asset['path'] + if path not in [sf.name for sf in self.static_files]: + self.static_files.append(StaticFile(name=path, url=asset['url'], data=None)) if block.scope_ids.usage_id.block_type in ['problem', 'vertical']: py_lib_zip_file = utils.get_python_lib_zip_if_using(self.olx_str, course_key)