Skip to content

Commit 009dd30

Browse files
authored
Merge pull request #36849 from dwong2708/dw/upgrade-django-5-course-import
feat: removing get_storage_class from COURSE_IMPORT_STORAGE
2 parents 1efee34 + e0a0d01 commit 009dd30

File tree

2 files changed

+86
-2
lines changed

2 files changed

+86
-2
lines changed

cms/djangoapps/contentstore/storage.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
from django.conf import settings
7-
from django.core.files.storage import get_storage_class
7+
from common.djangoapps.util.storage import resolve_storage_backend
88
from storages.backends.s3boto3 import S3Boto3Storage
99
from storages.utils import setting
1010

@@ -19,4 +19,7 @@ def __init__(self):
1919
super().__init__(bucket_name=bucket, custom_domain=None, querystring_auth=True)
2020

2121
# pylint: disable=invalid-name
22-
course_import_export_storage = get_storage_class(settings.COURSE_IMPORT_EXPORT_STORAGE)()
22+
course_import_export_storage = resolve_storage_backend(
23+
storage_key="course_import_export",
24+
legacy_setting_key="COURSE_IMPORT_EXPORT_STORAGE"
25+
)

cms/djangoapps/contentstore/tests/test_import.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
2222
from xmodule.modulestore.xml_importer import import_course_from_xml
2323

24+
from common.djangoapps.util.storage import resolve_storage_backend
25+
from storages.backends.s3boto3 import S3Boto3Storage
26+
2427
TEST_DATA_CONTENTSTORE = copy.deepcopy(settings.CONTENTSTORE)
2528
TEST_DATA_CONTENTSTORE['DOC_STORE_CONFIG']['db'] = 'test_xcontent_%s' % uuid4().hex
2629

@@ -275,3 +278,81 @@ def test_video_components_present_while_import(self):
275278

276279
video = module_store.get_item(vertical.children[1])
277280
self.assertEqual(video.display_name, 'default')
281+
282+
@override_settings(
283+
COURSE_IMPORT_EXPORT_STORAGE="cms.djangoapps.contentstore.storage.ImportExportS3Storage",
284+
DEFAULT_FILE_STORAGE="django.core.files.storage.FileSystemStorage"
285+
)
286+
def test_resolve_default_storage(self):
287+
""" Ensure the default storage is invoked, even if course export storage is configured """
288+
storage = resolve_storage_backend(
289+
storage_key="default",
290+
legacy_setting_key="DEFAULT_FILE_STORAGE"
291+
)
292+
self.assertEqual(storage.__class__.__name__, "FileSystemStorage")
293+
294+
@override_settings(
295+
COURSE_IMPORT_EXPORT_STORAGE="cms.djangoapps.contentstore.storage.ImportExportS3Storage",
296+
DEFAULT_FILE_STORAGE="django.core.files.storage.FileSystemStorage",
297+
COURSE_IMPORT_EXPORT_BUCKET="bucket_name_test"
298+
)
299+
def test_resolve_happy_path_storage(self):
300+
""" Make sure that the correct course export storage is being used """
301+
storage = resolve_storage_backend(
302+
storage_key="course_import_export",
303+
legacy_setting_key="COURSE_IMPORT_EXPORT_STORAGE"
304+
)
305+
self.assertEqual(storage.__class__.__name__, "ImportExportS3Storage")
306+
self.assertEqual(storage.bucket_name, "bucket_name_test")
307+
308+
@override_settings()
309+
def test_resolve_storage_with_no_config(self):
310+
""" If no storage setup is defined, we get FileSystemStorage by default """
311+
del settings.DEFAULT_FILE_STORAGE
312+
del settings.COURSE_IMPORT_EXPORT_STORAGE
313+
del settings.COURSE_IMPORT_EXPORT_BUCKET
314+
storage = resolve_storage_backend(
315+
storage_key="course_import_export",
316+
legacy_setting_key="COURSE_IMPORT_EXPORT_STORAGE"
317+
)
318+
self.assertEqual(storage.__class__.__name__, "FileSystemStorage")
319+
320+
@override_settings(
321+
COURSE_IMPORT_EXPORT_STORAGE=None,
322+
COURSE_IMPORT_EXPORT_BUCKET="bucket_name_test",
323+
STORAGES={
324+
'course_import_export': {
325+
'BACKEND': 'cms.djangoapps.contentstore.storage.ImportExportS3Storage',
326+
'OPTIONS': {}
327+
}
328+
}
329+
)
330+
def test_resolve_storage_using_django5_settings(self):
331+
""" Simulating a Django 4 environment using Django 5 Storages configuration """
332+
storage = resolve_storage_backend(
333+
storage_key="course_import_export",
334+
legacy_setting_key="COURSE_IMPORT_EXPORT_STORAGE"
335+
)
336+
self.assertEqual(storage.__class__.__name__, "ImportExportS3Storage")
337+
self.assertEqual(storage.bucket_name, "bucket_name_test")
338+
339+
@override_settings(
340+
STORAGES={
341+
'course_import_export': {
342+
'BACKEND': 'storages.backends.s3boto3.S3Boto3Storage',
343+
'OPTIONS': {
344+
'bucket_name': 'bucket_name_test'
345+
}
346+
}
347+
}
348+
)
349+
def test_resolve_storage_using_django5_settings_with_options(self):
350+
""" Ensure we call the storage class with the correct parameters and Django 5 setup """
351+
del settings.COURSE_IMPORT_EXPORT_STORAGE
352+
del settings.COURSE_IMPORT_EXPORT_BUCKET
353+
storage = resolve_storage_backend(
354+
storage_key="course_import_export",
355+
legacy_setting_key="COURSE_IMPORT_EXPORT_STORAGE"
356+
)
357+
self.assertEqual(storage.__class__.__name__, S3Boto3Storage.__name__)
358+
self.assertEqual(storage.bucket_name, "bucket_name_test")

0 commit comments

Comments
 (0)