Skip to content

Commit 30baaab

Browse files
fix: move export_library_v2_to_zip to backup file as well
1 parent ced05ff commit 30baaab

File tree

3 files changed

+44
-46
lines changed

3 files changed

+44
-46
lines changed

cms/djangoapps/contentstore/git_export_utils.py

Lines changed: 0 additions & 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
@@ -69,47 +67,6 @@ def cmd_log(cmd, cwd):
6967
return output
7068

7169

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-
11370
def export_to_git(content_key, repo, user='', rdir=None):
11471
"""
11572
Export a course or library to git.

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

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
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
15+
from cms.djangoapps.contentstore.exams import User
1316
from path import Path
1417

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