Skip to content

Commit

Permalink
fix #115 correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrDlouhy committed May 24, 2024
1 parent 244b008 commit ada03a7
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 34 deletions.
19 changes: 17 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -169,18 +169,33 @@ To exclude or disable file formats from the admin site, configure `IMPORT_EXPORT

IMPORT_EXPORT_CELERY_EXCLUDED_FORMATS = ["csv", "xls"]


Customizing File Storage Backend
--------------------------------

Define a custom storage backend by adding the `IMPORT_EXPORT_CELERY_STORAGE` to your Django settings STORAGES definition. For instance:
**If you are using the new Django 4.2 STORAGES**:

By default, `import_export_celery` uses Django `default` storage.
To use your own storage, use the the `IMPORT_EXPORT_CELERY_STORAGE_ALIAS` variable in your Django settings and adding the STORAGES definition.
For instance:

::

STORAGES = {
"IMPORT_EXPORT_CELERY_STORAGE": {
"import_export_celery": {
"BACKEND": "storages.backends.s3boto3.S3Boto3Storage",
},
}
IMPORT_EXPORT_CELERY_STORAGE_ALIAS = 'import_export_celery'

**DEPRECATED: If you are using old style storages**:

Define a custom storage backend by adding the `IMPORT_EXPORT_CELERY_STORAGE` to your Django settings. For instance:

::

IMPORT_EXPORT_CELERY_STORAGE = "storages.backends.s3boto3.S3Boto3Storage"


Customizing Task Time Limits
----------------------------
Expand Down
12 changes: 0 additions & 12 deletions example/project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,6 @@
},
}

STORAGES = {
"IMPORT_EXPORT_CELERY_STORAGE": {
"BACKEND": "django.core.files.storage.FileSystemStorage",
},
"staticfiles": {
"BACKEND": "django.core.files.storage.FileSystemStorage",
},
"default": {
"BACKEND": "django.core.files.storage.FileSystemStorage",
}
}

# Password validation
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators

Expand Down
60 changes: 60 additions & 0 deletions example/winners/tests/test_fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import django
from django.test import TestCase, override_settings
from django.conf import settings
from django.core.files.storage import FileSystemStorage
import unittest

from import_export_celery.fields import lazy_initialize_storage_class


class FooTestingStorage(FileSystemStorage):
pass


class InitializeStorageClassTests(TestCase):

def test_default(self):
self.assertIsInstance(lazy_initialize_storage_class(), FileSystemStorage)

@unittest.skipUnless(django.VERSION < (5, 1), "Test only applicable for Django versions < 5.1")
@override_settings(
IMPORT_EXPORT_CELERY_STORAGE="winners.tests.test_fields.FooTestingStorage"
)
def test_old_style(self):
del settings.IMPORT_EXPORT_CELERY_STORAGE_ALIAS
del settings.STORAGES
self.assertIsInstance(lazy_initialize_storage_class(), FooTestingStorage)

@unittest.skipUnless((4, 2) <= django.VERSION, "Test only applicable for Django 4.2 and later")
@override_settings(
IMPORT_EXPORT_CELERY_STORAGE_ALIAS="test_import_export_celery",
STORAGES={
"test_import_export_celery": {
"BACKEND": "winners.tests.test_fields.FooTestingStorage",
},
"staticfiles": {
"BACKEND": "django.core.files.storage.FileSystemStorage",
},
"default": {
"BACKEND": "django.core.files.storage.FileSystemStorage",
}
}
)
def test_new_style(self):
self.assertIsInstance(lazy_initialize_storage_class(), FooTestingStorage)

@unittest.skipUnless((4, 2) <= django.VERSION, "Test only applicable for Django 4.2 and later")
@override_settings(
STORAGES={
"staticfiles": {
"BACKEND": "django.core.files.storage.FileSystemStorage",
},
"default": {
"BACKEND": "winners.tests.test_fields.FooTestingStorage",
}
}
)
def test_default_storage(self):
""" Test that "default" storage is used when no alias is provided """
self.assertIsInstance(lazy_initialize_storage_class(), FooTestingStorage)
39 changes: 19 additions & 20 deletions import_export_celery/fields.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
from django.conf import settings
from django.db import models
from django.core.exceptions import ImproperlyConfigured

Check failure on line 2 in import_export_celery/fields.py

View workflow job for this annotation

GitHub Actions / flake8

'django.core.exceptions.ImproperlyConfigured' imported but unused

Check failure on line 2 in import_export_celery/fields.py

View workflow job for this annotation

GitHub Actions / flake8

'django.core.exceptions.ImproperlyConfigured' imported but unused


def lazy_initialize_storage_class():
# If the user has specified a custom storage backend, use it.
if settings.STORAGES.get("IMPORT_EXPORT_CELERY_STORAGE"):
try:
# From Django 4.2 and later
from django.core.files.storage import storages
from django.core.files.storage.handler import InvalidStorageError
try:
storage_class = storages['IMPORT_EXPORT_CELERY_STORAGE']
except InvalidStorageError:
from django.utils.module_loading import import_string
storage_class = settings.DEFAULT_FILE_STORAGE
storage_class = import_string(storage_class)()
except ImportError:
# Deprecated since Django 4.2, Removed in Django 5.1
from django.core.files.storage import get_storage_class
storage_class = get_storage_class(
settings.STORAGES.get("IMPORT_EXPORT_CELERY_STORAGE")["BACKEND"]
)()
return storage_class
from django.conf import settings
try:
from django.core.files.storage import storages
storages_defined = True
except ImportError:
storages_defined = False

if not hasattr(settings, 'IMPORT_EXPORT_CELERY_STORAGE') and storages_defined:
# Use new style storages if defined
storage_alias = getattr(settings, "IMPORT_EXPORT_CELERY_STORAGE_ALIAS", "default")
storage_class = storages[storage_alias]
else:
# Use old style storages if defined
from django.core.files.storage import get_storage_class
storage_class = get_storage_class(getattr(settings, "IMPORT_EXPORT_CELERY_STORAGE", "django.core.files.storage.FileSystemStorage"))
return storage_class()

return storage_class


class ImportExportFileField(models.FileField):
Expand Down

0 comments on commit ada03a7

Please sign in to comment.