Skip to content

Commit 5a54a81

Browse files
authored
Merge pull request #105 from zachbellay/feat/customizable-storage-backend
Enabling Customization of Storage Backends
2 parents 234e93e + b3273d3 commit 5a54a81

File tree

5 files changed

+29
-5
lines changed

5 files changed

+29
-5
lines changed

README.rst

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

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

172+
Customizing File Storage Backend
173+
--------------------------------
174+
175+
Define a custom storage backend by adding the `IMPORT_EXPORT_CELERY_STORAGE` to your Django settings. For instance:
176+
177+
::
178+
179+
IMPORT_EXPORT_CELERY_STORAGE = "storages.backends.s3boto3.S3Boto3Storage"
172180

173181
Customizing email template for export job completion email
174182
----------------------------------------------------------
@@ -215,10 +223,10 @@ Before submitting a PR please run `flake8` and (in the examples directory) `pyth
215223

216224
Please note, that you need to restart celery for changes to propogate to the workers. Do this with `docker-compose down celery`, `docker-compose up celery`.
217225

218-
Comercial support
226+
Commercial support
219227
-----------------
220228

221-
Comercial support is provided by `gradesta s.r.o <https://gradesta.com/support/>`_.
229+
Commercial support is provided by `gradesta s.r.o <https://gradesta.com/support/>`_.
222230

223231
Credits
224232
-------

example/project/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,4 @@
135135
}
136136

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

import_export_celery/fields.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from django.conf import settings
2+
from django.core.files.storage import get_storage_class
3+
from django.db import models
4+
5+
6+
class ImportExportFileField(models.FileField):
7+
def __init__(self, *args, **kwargs):
8+
# If the user has specified a custom storage backend, use it.
9+
if settings.IMPORT_EXPORT_CELERY_STORAGE:
10+
storage_class = get_storage_class(settings.IMPORT_EXPORT_CELERY_STORAGE)
11+
kwargs["storage"] = storage_class()
12+
13+
super().__init__(*args, **kwargs)

import_export_celery/models/exportjob.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from django.db.models.signals import post_save
1313
from django.utils.translation import gettext_lazy as _
1414

15+
from ..fields import ImportExportFileField
1516
from ..tasks import run_export_job
1617
from ..utils import get_formats
1718

@@ -22,7 +23,7 @@ def __init__(self, *args, **kwargs):
2223
super().__init__(*args, **kwargs)
2324
self._content_type = None
2425

25-
file = models.FileField(
26+
file = ImportExportFileField(
2627
verbose_name=_("exported file"),
2728
upload_to="django-import-export-celery-export-jobs",
2829
blank=False,

import_export_celery/models/importjob.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313

1414
from import_export.formats.base_formats import DEFAULT_FORMATS
1515

16+
from ..fields import ImportExportFileField
1617
from ..tasks import run_import_job
1718

1819

1920
@with_author
2021
class ImportJob(models.Model):
21-
file = models.FileField(
22+
file = ImportExportFileField(
2223
verbose_name=_("File to be imported"),
2324
upload_to="django-import-export-celery-import-jobs",
2425
blank=False,
@@ -45,7 +46,7 @@ class ImportJob(models.Model):
4546
max_length=255,
4647
)
4748

48-
change_summary = models.FileField(
49+
change_summary = ImportExportFileField(
4950
verbose_name=_("Summary of changes made by this import"),
5051
upload_to="django-import-export-celery-import-change-summaries",
5152
blank=True,

0 commit comments

Comments
 (0)