Skip to content

Commit 27d2c71

Browse files
fix: move export_library_v2_to_zip to backup file as well
1 parent 20245ec commit 27d2c71

3 files changed

Lines changed: 46 additions & 46 deletions

File tree

cms/djangoapps/contentstore/git_export_utils.py

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66

77
import logging
88
import os
9-
import shutil
109
import subprocess
11-
import zipfile
1210
from urllib.parse import urlparse
1311

1412
from django.conf import settings
@@ -17,6 +15,7 @@
1715
from django.utils.translation import gettext_lazy as _
1816
from opaque_keys.edx.locator import LibraryLocator, LibraryLocatorV2
1917

18+
from openedx.core.djangoapps.content_libraries.api.backup import export_library_v2_to_zip
2019
from xmodule.contentstore.django import contentstore
2120
from xmodule.modulestore.django import modulestore
2221
from xmodule.modulestore.xml_exporter import export_course_to_xml, export_library_to_xml
@@ -69,47 +68,6 @@ def cmd_log(cmd, cwd):
6968
return output
7069

7170

72-
def export_library_v2_to_zip(library_key, root_dir, library_dir, user=None):
73-
"""
74-
Export a v2 library using the backup API.
75-
76-
V2 libraries are stored in Learning Core and use a zip-based backup mechanism.
77-
This function creates a zip backup and extracts it to the specified directory.
78-
79-
Args:
80-
library_key: LibraryLocatorV2 for the library to export
81-
root_dir: Root directory where library_dir will be created
82-
library_dir: Directory name for the exported library content
83-
user: Username string for the backup API (optional)
84-
85-
Raises:
86-
Exception: If backup creation or extraction fails
87-
"""
88-
from openedx.core.djangoapps.content_libraries.api import create_library_v2_zip
89-
90-
# Get user object for backup API
91-
user_obj = User.objects.filter(username=user).first()
92-
temp_dir, zip_path = create_library_v2_zip(library_key, user_obj)
93-
94-
try:
95-
# Target directory for extraction
96-
target_dir = os.path.join(root_dir, library_dir)
97-
98-
# Create target directory if it doesn't exist
99-
os.makedirs(target_dir, exist_ok=True)
100-
101-
# Extract zip contents (will overwrite existing files)
102-
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
103-
zip_ref.extractall(target_dir)
104-
105-
log.info('Extracted library v2 backup to %s', target_dir)
106-
107-
finally:
108-
# Cleanup temporary files
109-
if temp_dir.exists():
110-
shutil.rmtree(temp_dir)
111-
112-
11371
def export_to_git(content_key, repo, user='', rdir=None):
11472
"""
11573
Export a course or library to git.

openedx/core/djangoapps/content_libraries/api/backup.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55

66
import os
77
from datetime import datetime
8+
import shutil
89
from tempfile import mkdtemp
10+
import zipfile
911

1012
from django.conf import settings
1113
from django.utils.text import slugify
12-
from opaque_keys.edx.locator import LibraryLocatorV2
14+
from opaque_keys.edx.locator import LibraryLocatorV2, log
1315
from path import Path
1416

1517
from openedx_content.api import create_zip_file as create_lib_zip_file
@@ -39,3 +41,44 @@ def create_library_v2_zip(library_key: LibraryLocatorV2, user) -> tuple:
3941
origin_server = getattr(settings, 'CMS_BASE', None)
4042
create_lib_zip_file(lp_key=str(library_key), path=file_path, user=user, origin_server=origin_server)
4143
return root_dir, file_path
44+
45+
46+
def export_library_v2_to_zip(library_key, root_dir, library_dir, user=None):
47+
"""
48+
Export a v2 library using the backup API.
49+
50+
V2 libraries are stored in Learning Core and use a zip-based backup mechanism.
51+
This function creates a zip backup and extracts it to the specified directory.
52+
53+
Args:
54+
library_key: LibraryLocatorV2 for the library to export
55+
root_dir: Root directory where library_dir will be created
56+
library_dir: Directory name for the exported library content
57+
user: Username string for the backup API (optional)
58+
59+
Raises:
60+
Exception: If backup creation or extraction fails
61+
"""
62+
from cms.djangoapps.contentstore.exams import User
63+
64+
# Get user object for backup API
65+
user_obj = User.objects.filter(username=user).first()
66+
temp_dir, zip_path = create_library_v2_zip(library_key, user_obj)
67+
68+
try:
69+
# Target directory for extraction
70+
target_dir = os.path.join(root_dir, library_dir)
71+
72+
# Create target directory if it doesn't exist
73+
os.makedirs(target_dir, exist_ok=True)
74+
75+
# Extract zip contents (will overwrite existing files)
76+
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
77+
zip_ref.extractall(target_dir)
78+
79+
log.info('Extracted library v2 backup to %s', target_dir)
80+
81+
finally:
82+
# Cleanup temporary files
83+
if temp_dir.exists():
84+
shutil.rmtree(temp_dir)

openedx/core/djangoapps/content_libraries/tasks.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
from cms.djangoapps.contentstore.storage import course_import_export_storage
7272

7373
from . import api
74-
from .api import create_library_v2_zip
7574
from .models import ContentLibraryBlockImportTask
7675

7776
log = logging.getLogger(__name__)
@@ -550,7 +549,7 @@ def backup_library(self, user_id: int, library_key_str: str) -> None:
550549
set_custom_attribute("exporting_started", str(library_key))
551550

552551
user = User.objects.get(id=user_id)
553-
_root_dir, file_path = create_library_v2_zip(library_key, user)
552+
_root_dir, file_path = api.create_library_v2_zip(library_key, user)
554553
set_custom_attribute("exporting_completed", str(library_key))
555554

556555
with open(file_path, 'rb') as zipfile:

0 commit comments

Comments
 (0)