|
47 | 47 | ) |
48 | 48 | from openedx_learning.apps.authoring.collections import api as collections_api |
49 | 49 | from openedx_learning.apps.authoring.components import api as components_api |
| 50 | +from openedx_learning.apps.authoring.contents import api as contents_api |
50 | 51 | from openedx_learning.apps.authoring.publishing import api as publishing_api |
51 | 52 | from openedx_learning.apps.authoring.sections import api as sections_api |
52 | 53 | from openedx_learning.apps.authoring.subsections import api as subsections_api |
@@ -493,6 +494,7 @@ def __init__(self, zipf: zipfile.ZipFile, key: str | None = None, user: UserType |
493 | 494 | self.zipf = zipf |
494 | 495 | self.user = user |
495 | 496 | self.lp_key = key # If provided, use this key for the restored learning package |
| 497 | + self.learning_package_id: int | None = None # Will be set upon restoration |
496 | 498 | self.utc_now: datetime = datetime.now(timezone.utc) |
497 | 499 | self.component_types_cache: dict[tuple[str, str], ComponentType] = {} |
498 | 500 | self.errors: list[dict[str, Any]] = [] |
@@ -735,6 +737,7 @@ def _save( |
735 | 737 | learning_package["key"] = self.lp_key |
736 | 738 |
|
737 | 739 | learning_package_obj = publishing_api.create_learning_package(**learning_package) |
| 740 | + self.learning_package_id = learning_package_obj.id |
738 | 741 |
|
739 | 742 | with publishing_api.bulk_draft_changes_for(learning_package_obj.id): |
740 | 743 | self._save_components(learning_package_obj, components, component_static_files) |
@@ -937,16 +940,31 @@ def _resolve_static_files( |
937 | 940 | num_version: int, |
938 | 941 | entity_key: str, |
939 | 942 | static_files_map: dict[str, List[str]] |
940 | | - ) -> dict[str, bytes]: |
| 943 | + ) -> dict[str, bytes | int]: |
941 | 944 | """Resolve static file paths into their binary content.""" |
942 | | - resolved_files: dict[str, bytes] = {} |
| 945 | + resolved_files: dict[str, bytes | int] = {} |
943 | 946 |
|
944 | | - static_file_key = f"{entity_key}:v{num_version}" # e.g., "my_component:123:v1" |
| 947 | + static_file_key = f"{entity_key}:v{num_version}" # e.g., "xblock.v1:html:my_component_123456:v1" |
| 948 | + block_type = entity_key.split(":")[1] # e.g., "html" |
945 | 949 | static_files = static_files_map.get(static_file_key, []) |
946 | 950 | for static_file in static_files: |
947 | 951 | local_key = static_file.split(f"v{num_version}/")[-1] |
948 | 952 | with self.zipf.open(static_file, "r") as f: |
949 | | - resolved_files[local_key] = f.read() |
| 953 | + content_bytes = f.read() |
| 954 | + if local_key == "block.xml": |
| 955 | + # Special handling for block.xml to ensure |
| 956 | + # storing the value as a content instance |
| 957 | + if not self.learning_package_id: |
| 958 | + raise ValueError("learning_package_id must be set before resolving static files.") |
| 959 | + text_content = contents_api.get_or_create_text_content( |
| 960 | + self.learning_package_id, |
| 961 | + contents_api.get_or_create_media_type(f"application/vnd.openedx.xblock.v1.{block_type}+xml").id, |
| 962 | + text=content_bytes.decode("utf-8"), |
| 963 | + created=self.utc_now, |
| 964 | + ) |
| 965 | + resolved_files[local_key] = text_content.id |
| 966 | + else: |
| 967 | + resolved_files[local_key] = content_bytes |
950 | 968 | return resolved_files |
951 | 969 |
|
952 | 970 | def _resolve_children(self, entity_data: dict[str, Any], lookup_map: dict[str, Any]) -> list[Any]: |
|
0 commit comments