|
6 | 6 | import zipfile |
7 | 7 | from datetime import datetime, timezone |
8 | 8 | from pathlib import Path |
9 | | -from typing import List, Optional, Tuple |
| 9 | +from typing import Any, List, Optional, Tuple |
10 | 10 |
|
| 11 | +from django.db import transaction |
11 | 12 | from django.db.models import Prefetch, QuerySet |
12 | 13 | from django.utils.text import slugify |
13 | 14 |
|
|
21 | 22 | PublishableEntityVersion, |
22 | 23 | ) |
23 | 24 | from openedx_learning.apps.authoring.backup_restore.toml import ( |
| 25 | + parse_learning_package_toml, |
24 | 26 | toml_collection, |
25 | 27 | toml_learning_package, |
26 | 28 | toml_publishable_entity, |
@@ -366,3 +368,164 @@ def create_zip(self, path: str) -> None: |
366 | 368 | toml_collection(collection, list(entity_keys_related)), |
367 | 369 | timestamp=collection.modified, |
368 | 370 | ) |
| 371 | + |
| 372 | + |
| 373 | +class LearningPackageUnzipper: |
| 374 | + """ |
| 375 | + Handles extraction and restoration of learning package data from a zip archive. |
| 376 | +
|
| 377 | + Main responsibilities: |
| 378 | + - Parse and organize files from the zip structure. |
| 379 | + - Restore learning package, containers, components, and collections to the database. |
| 380 | + - Ensure atomicity of the restore process. |
| 381 | +
|
| 382 | + Usage: |
| 383 | + unzipper = LearningPackageUnzipper() |
| 384 | + summary = unzipper.load("/path/to/backup.zip") |
| 385 | + """ |
| 386 | + |
| 387 | + def __init__(self) -> None: |
| 388 | + self.utc_now: datetime = datetime.now(tz=timezone.utc) |
| 389 | + |
| 390 | + # -------------------------- |
| 391 | + # Public API |
| 392 | + # -------------------------- |
| 393 | + |
| 394 | + @transaction.atomic |
| 395 | + def load(self, zipf: zipfile.ZipFile) -> dict[str, Any]: |
| 396 | + """ |
| 397 | + Extracts and restores all objects from the ZIP archive in an atomic transaction. |
| 398 | +
|
| 399 | + Args: |
| 400 | + zipf (ZipFile): An open ZipFile instance. |
| 401 | +
|
| 402 | + Returns: |
| 403 | + dict: Summary of restored objects (keys, counts, etc.). |
| 404 | +
|
| 405 | + Raises: |
| 406 | + FileNotFoundError: If required files are missing. |
| 407 | + ValueError: If TOML parsing fails. |
| 408 | + Exception: For any database errors (transaction will rollback). |
| 409 | + """ |
| 410 | + organized_files = self._get_organized_file_list(zipf.namelist()) |
| 411 | + |
| 412 | + # Validate required files |
| 413 | + if not organized_files["learning_package"]: |
| 414 | + raise FileNotFoundError(f"Missing required {TOML_PACKAGE_NAME} in archive.") |
| 415 | + |
| 416 | + # Restore objects |
| 417 | + learning_package = self._load_learning_package(zipf, organized_files["learning_package"]) |
| 418 | + self._restore_components(zipf, organized_files["components"], learning_package) |
| 419 | + self._restore_containers(zipf, organized_files["containers"], learning_package) |
| 420 | + self._restore_collections(zipf, organized_files["collections"], learning_package) |
| 421 | + |
| 422 | + return { |
| 423 | + "learning_package": learning_package.key, |
| 424 | + "containers": len(organized_files["containers"]), |
| 425 | + "components": len(organized_files["components"]), |
| 426 | + "collections": len(organized_files["collections"]), |
| 427 | + } |
| 428 | + |
| 429 | + # -------------------------- |
| 430 | + # Loading methods |
| 431 | + # -------------------------- |
| 432 | + |
| 433 | + def _load_learning_package(self, zipf: zipfile.ZipFile, package_file: str) -> LearningPackage: |
| 434 | + """Load and persist the learning package TOML file.""" |
| 435 | + toml_content = self._read_file_from_zip(zipf, package_file) |
| 436 | + data = parse_learning_package_toml(toml_content) |
| 437 | + |
| 438 | + return publishing_api.create_learning_package( |
| 439 | + key=data["key"], |
| 440 | + title=data["title"], |
| 441 | + description=data["description"], |
| 442 | + ) |
| 443 | + |
| 444 | + def _restore_containers( |
| 445 | + self, zipf: zipfile.ZipFile, container_files: List[str], learning_package: LearningPackage |
| 446 | + ) -> None: |
| 447 | + """Restore containers from the zip archive.""" |
| 448 | + for container_file in container_files: |
| 449 | + self._load_container(zipf, container_file, learning_package) |
| 450 | + |
| 451 | + def _restore_components( |
| 452 | + self, zipf: zipfile.ZipFile, component_files: List[str], learning_package: LearningPackage |
| 453 | + ) -> None: |
| 454 | + """Restore components from the zip archive.""" |
| 455 | + for component_file in component_files: |
| 456 | + self._load_component(zipf, component_file, learning_package) |
| 457 | + |
| 458 | + def _restore_collections( |
| 459 | + self, zipf: zipfile.ZipFile, collection_files: List[str], learning_package: LearningPackage |
| 460 | + ) -> None: |
| 461 | + """Restore collections from the zip archive (future extension).""" |
| 462 | + # pylint: disable=W0613 |
| 463 | + for collection_file in collection_files: # pylint: disable=W0612 |
| 464 | + # Placeholder for collection restore logic |
| 465 | + pass |
| 466 | + |
| 467 | + # -------------------------- |
| 468 | + # Individual object loaders |
| 469 | + # -------------------------- |
| 470 | + |
| 471 | + def _load_container( |
| 472 | + self, zipf: zipfile.ZipFile, container_file: str, learning_package: LearningPackage |
| 473 | + ): # pylint: disable=W0613 |
| 474 | + """Load and persist a container (placeholder).""" |
| 475 | + # TODO: parse TOML here |
| 476 | + # pylint: disable=W0105 |
| 477 | + """ |
| 478 | + container = publishing_api.create_container( |
| 479 | + learning_package_id=learning_package.id, |
| 480 | + key="container_key_placeholder", |
| 481 | + title="Container Title Placeholder", |
| 482 | + description="Container Description Placeholder", |
| 483 | + ) |
| 484 | + publishing_api.create_container_version( |
| 485 | + container_id=container.id, |
| 486 | + title="Container Version Title Placeholder", |
| 487 | + created_by=None, |
| 488 | + ) |
| 489 | + """ |
| 490 | + |
| 491 | + def _load_component( |
| 492 | + self, zipf: zipfile.ZipFile, component_file: str, learning_package: LearningPackage |
| 493 | + ): # pylint: disable=W0613 |
| 494 | + """Load and persist a component (placeholder).""" |
| 495 | + # TODO: implement actual parsing |
| 496 | + return None |
| 497 | + |
| 498 | + # -------------------------- |
| 499 | + # Utilities |
| 500 | + # -------------------------- |
| 501 | + |
| 502 | + def _read_file_from_zip(self, zipf: zipfile.ZipFile, filename: str) -> str: |
| 503 | + """Read and decode a UTF-8 file from the zip archive.""" |
| 504 | + with zipf.open(filename) as f: |
| 505 | + return f.read().decode("utf-8") |
| 506 | + |
| 507 | + def _get_organized_file_list(self, file_paths: List[str]) -> dict[str, Any]: |
| 508 | + """ |
| 509 | + Organize file paths into categories: learning_package, containers, components, collections. |
| 510 | + """ |
| 511 | + organized: dict[str, Any] = { |
| 512 | + "learning_package": None, |
| 513 | + "containers": [], |
| 514 | + "components": [], |
| 515 | + "collections": [], |
| 516 | + } |
| 517 | + |
| 518 | + for path in file_paths: |
| 519 | + if path.endswith("/"): # skip directories |
| 520 | + continue |
| 521 | + |
| 522 | + if path == TOML_PACKAGE_NAME: |
| 523 | + organized["learning_package"] = path |
| 524 | + elif path.startswith("entities/") and str(Path(path).parent) == "entities": |
| 525 | + organized["containers"].append(path) |
| 526 | + elif path.startswith("entities/"): |
| 527 | + organized["components"].append(path) |
| 528 | + elif path.startswith("collections/"): |
| 529 | + organized["collections"].append(path) |
| 530 | + |
| 531 | + return organized |
0 commit comments