Skip to content

Commit 7a9ec54

Browse files
committed
fix #115 correctly
1 parent c554263 commit 7a9ec54

File tree

4 files changed

+95
-34
lines changed

4 files changed

+95
-34
lines changed

README.rst

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,18 +169,33 @@ To exclude or disable file formats from the admin site, configure `IMPORT_EXPORT
169169

170170
IMPORT_EXPORT_CELERY_EXCLUDED_FORMATS = ["csv", "xls"]
171171

172+
172173
Customizing File Storage Backend
173174
--------------------------------
174175

175-
Define a custom storage backend by adding the `IMPORT_EXPORT_CELERY_STORAGE` to your Django settings STORAGES definition. For instance:
176+
**If you are using the new Django 4.2 STORAGES**:
177+
178+
By default, `import_export_celery` uses Django `default` storage.
179+
To use your own storage, use the the `IMPORT_EXPORT_CELERY_STORAGE_ALIAS` variable in your Django settings and adding the STORAGES definition.
180+
For instance:
176181

177182
::
178183

179184
STORAGES = {
180-
"IMPORT_EXPORT_CELERY_STORAGE": {
185+
"import_export_celery": {
181186
"BACKEND": "storages.backends.s3boto3.S3Boto3Storage",
182187
},
183188
}
189+
IMPORT_EXPORT_CELERY_STORAGE_ALIAS = 'import_export_celery'
190+
191+
**DEPRECATED: If you are using old style storages**:
192+
193+
Define a custom storage backend by adding the `IMPORT_EXPORT_CELERY_STORAGE` to your Django settings. For instance:
194+
195+
::
196+
197+
IMPORT_EXPORT_CELERY_STORAGE = "storages.backends.s3boto3.S3Boto3Storage"
198+
184199

185200
Customizing Task Time Limits
186201
----------------------------

example/project/settings.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,6 @@
9898
},
9999
}
100100

101-
STORAGES = {
102-
"IMPORT_EXPORT_CELERY_STORAGE": {
103-
"BACKEND": "django.core.files.storage.FileSystemStorage",
104-
},
105-
"staticfiles": {
106-
"BACKEND": "django.core.files.storage.FileSystemStorage",
107-
},
108-
"default": {
109-
"BACKEND": "django.core.files.storage.FileSystemStorage",
110-
}
111-
}
112-
113101
# Password validation
114102
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators
115103

example/winners/tests/test_fields.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import django
2+
from django.test import TestCase, override_settings
3+
from django.conf import settings
4+
from django.core.files.storage import FileSystemStorage
5+
import unittest
6+
7+
from import_export_celery.fields import lazy_initialize_storage_class
8+
9+
10+
class FooTestingStorage(FileSystemStorage):
11+
pass
12+
13+
14+
class InitializeStorageClassTests(TestCase):
15+
16+
def test_default(self):
17+
self.assertIsInstance(lazy_initialize_storage_class(), FileSystemStorage)
18+
19+
@unittest.skipUnless(django.VERSION < (5, 1), "Test only applicable for Django versions < 5.1")
20+
@override_settings(
21+
IMPORT_EXPORT_CELERY_STORAGE="winners.tests.test_fields.FooTestingStorage"
22+
)
23+
def test_old_style(self):
24+
del settings.IMPORT_EXPORT_CELERY_STORAGE_ALIAS
25+
del settings.STORAGES
26+
self.assertIsInstance(lazy_initialize_storage_class(), FooTestingStorage)
27+
28+
@unittest.skipUnless((4, 2) <= django.VERSION, "Test only applicable for Django 4.2 and later")
29+
@override_settings(
30+
IMPORT_EXPORT_CELERY_STORAGE_ALIAS="test_import_export_celery",
31+
STORAGES={
32+
"test_import_export_celery": {
33+
"BACKEND": "winners.tests.test_fields.FooTestingStorage",
34+
},
35+
"staticfiles": {
36+
"BACKEND": "django.core.files.storage.FileSystemStorage",
37+
},
38+
"default": {
39+
"BACKEND": "django.core.files.storage.FileSystemStorage",
40+
}
41+
}
42+
43+
)
44+
def test_new_style(self):
45+
self.assertIsInstance(lazy_initialize_storage_class(), FooTestingStorage)
46+
47+
@unittest.skipUnless((4, 2) <= django.VERSION, "Test only applicable for Django 4.2 and later")
48+
@override_settings(
49+
STORAGES={
50+
"staticfiles": {
51+
"BACKEND": "django.core.files.storage.FileSystemStorage",
52+
},
53+
"default": {
54+
"BACKEND": "winners.tests.test_fields.FooTestingStorage",
55+
}
56+
}
57+
)
58+
def test_default_storage(self):
59+
""" Test that "default" storage is used when no alias is provided """
60+
self.assertIsInstance(lazy_initialize_storage_class(), FooTestingStorage)

import_export_celery/fields.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
1-
from django.conf import settings
21
from django.db import models
32

43

54
def lazy_initialize_storage_class():
6-
# If the user has specified a custom storage backend, use it.
7-
if settings.STORAGES.get("IMPORT_EXPORT_CELERY_STORAGE"):
8-
try:
9-
# From Django 4.2 and later
10-
from django.core.files.storage import storages
11-
from django.core.files.storage.handler import InvalidStorageError
12-
try:
13-
storage_class = storages['IMPORT_EXPORT_CELERY_STORAGE']
14-
except InvalidStorageError:
15-
from django.utils.module_loading import import_string
16-
storage_class = settings.DEFAULT_FILE_STORAGE
17-
storage_class = import_string(storage_class)()
18-
except ImportError:
19-
# Deprecated since Django 4.2, Removed in Django 5.1
20-
from django.core.files.storage import get_storage_class
21-
storage_class = get_storage_class(
22-
settings.STORAGES.get("IMPORT_EXPORT_CELERY_STORAGE")["BACKEND"]
23-
)()
24-
return storage_class
5+
from django.conf import settings
6+
try:
7+
from django.core.files.storage import storages
8+
storages_defined = True
9+
except ImportError:
10+
storages_defined = False
11+
12+
if not hasattr(settings, 'IMPORT_EXPORT_CELERY_STORAGE') and storages_defined:
13+
# Use new style storages if defined
14+
storage_alias = getattr(settings, "IMPORT_EXPORT_CELERY_STORAGE_ALIAS", "default")
15+
storage_class = storages[storage_alias]
16+
else:
17+
# Use old style storages if defined
18+
from django.core.files.storage import get_storage_class
19+
storage_class = get_storage_class(getattr(settings, "IMPORT_EXPORT_CELERY_STORAGE", "django.core.files.storage.FileSystemStorage"))
20+
return storage_class()
21+
22+
return storage_class
2523

2624

2725
class ImportExportFileField(models.FileField):

0 commit comments

Comments
 (0)