Skip to content

Commit

Permalink
Merge pull request #116 from JMSoler7/fix/warn_storages
Browse files Browse the repository at this point in the history
Manage custom storage class using django.core.files.storage.storages …
  • Loading branch information
timthelion authored Apr 12, 2024
2 parents 71885dd + f86afdb commit 2064e2d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
8 changes: 6 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,15 @@ To exclude or disable file formats from the admin site, configure `IMPORT_EXPORT
Customizing File Storage Backend
--------------------------------

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

::

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

Customizing Task Time Limits
----------------------------
Expand Down
6 changes: 5 additions & 1 deletion example/project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@
},
}

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

# Password validation
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators
Expand Down Expand Up @@ -135,7 +140,6 @@
}

EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
IMPORT_EXPORT_CELERY_STORAGE = "django.core.files.storage.FileSystemStorage"

# Default import time limits (in seconds)
IMPORT_EXPORT_CELERY_IMPORT_SOFT_TIME_LIMIT = 300 # 5 minutes
Expand Down
25 changes: 17 additions & 8 deletions import_export_celery/fields.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
from django.conf import settings
from django.core.files.storage import get_storage_class, storages
from django.db import models


def lazy_initialize_storage_class():
# If the user has specified a custom storage backend, use it.
if getattr(settings, "IMPORT_EXPORT_CELERY_STORAGE", None):
if settings.STORAGES.get("IMPORT_EXPORT_CELERY_STORAGE"):
try:
storage_class = get_storage_class(settings.IMPORT_EXPORT_CELERY_STORAGE)
return storage_class()
except (ImportError, TypeError):
return storages[settings.IMPORT_EXPORT_CELERY_STORAGE]

return None
# 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


class ImportExportFileField(models.FileField):
Expand Down

0 comments on commit 2064e2d

Please sign in to comment.