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 8b23dd5
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 21 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
27 changes: 27 additions & 0 deletions example/winners/tests/test_fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os

Check failure on line 1 in example/winners/tests/test_fields.py

View workflow job for this annotation

GitHub Actions / flake8

'os' imported but unused

Check failure on line 1 in example/winners/tests/test_fields.py

View workflow job for this annotation

GitHub Actions / flake8

'os' imported but unused
from django.test import TestCase, override_settings
from django.core.files.base import ContentFile

Check failure on line 3 in example/winners/tests/test_fields.py

View workflow job for this annotation

GitHub Actions / flake8

'django.core.files.base.ContentFile' imported but unused

Check failure on line 3 in example/winners/tests/test_fields.py

View workflow job for this annotation

GitHub Actions / flake8

'django.core.files.base.ContentFile' imported but unused

from import_export_celery.fields import lazy_initialize_storage_class


class InitializeStorageClassTests(TestCase):

@override_settings(
IMPORT_EXPORT_CELERY_STORAGE="foo"
)
def test_lazy_initialize_storage_class_old_style(self):
self.assertEquals(lazy_initialize_storage_class(), "foo")


@override_settings(

Check failure on line 17 in example/winners/tests/test_fields.py

View workflow job for this annotation

GitHub Actions / flake8

too many blank lines (2)

Check failure on line 17 in example/winners/tests/test_fields.py

View workflow job for this annotation

GitHub Actions / flake8

too many blank lines (2)
IMPORT_EXPORT_CELERY_STORAGE_ALIAS="import_export_celery",
STORAGES={
"import_export_celery": {
"BACKEND": "bar",
}
}
)
def test_lazy_initialize_storage_class_new_style(self):
self.assertEquals(lazy_initialize_storage_class(), "bar")
31 changes: 12 additions & 19 deletions import_export_celery/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,18 @@


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
try:
# From Django 4.2 and later
from django.core.files.storage import storages
storage_alias = getattr(settings, "IMPORT_EXPORT_CELERY_STORAGE_ALIAS", "default")
storage_class = storages[storage_alias]
except ImportError:
# Deprecated since Django 4.2, Removed in Django 5.1
from django.core.files.storage import get_storage_class
old_storage = getattr(settings, "IMPORT_EXPORT_CELERY_STORAGE", settings.DEFAULT_FILE_STORAGE)
storage_class = get_storage_class(old_storage)()

return storage_class


class ImportExportFileField(models.FileField):
Expand Down

0 comments on commit 8b23dd5

Please sign in to comment.