Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manage custom storage class using django.core.files.storage.storages … #116

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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