Skip to content

Commit

Permalink
Merge pull request #105 from zachbellay/feat/customizable-storage-bac…
Browse files Browse the repository at this point in the history
…kend

Enabling Customization of Storage Backends
  • Loading branch information
timthelion authored Jul 29, 2023
2 parents 234e93e + b3273d3 commit 5a54a81
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
12 changes: 10 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@ 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. For instance:

::

IMPORT_EXPORT_CELERY_STORAGE = "storages.backends.s3boto3.S3Boto3Storage"

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

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`.

Comercial support
Commercial support
-----------------

Comercial support is provided by `gradesta s.r.o <https://gradesta.com/support/>`_.
Commercial support is provided by `gradesta s.r.o <https://gradesta.com/support/>`_.

Credits
-------
Expand Down
1 change: 1 addition & 0 deletions example/project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,4 @@
}

EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
IMPORT_EXPORT_CELERY_STORAGE = "django.core.files.storage.FileSystemStorage"
13 changes: 13 additions & 0 deletions import_export_celery/fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django.conf import settings
from django.core.files.storage import get_storage_class
from django.db import models


class ImportExportFileField(models.FileField):
def __init__(self, *args, **kwargs):
# If the user has specified a custom storage backend, use it.
if settings.IMPORT_EXPORT_CELERY_STORAGE:
storage_class = get_storage_class(settings.IMPORT_EXPORT_CELERY_STORAGE)
kwargs["storage"] = storage_class()

super().__init__(*args, **kwargs)
3 changes: 2 additions & 1 deletion import_export_celery/models/exportjob.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from django.db.models.signals import post_save
from django.utils.translation import gettext_lazy as _

from ..fields import ImportExportFileField
from ..tasks import run_export_job
from ..utils import get_formats

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

file = models.FileField(
file = ImportExportFileField(
verbose_name=_("exported file"),
upload_to="django-import-export-celery-export-jobs",
blank=False,
Expand Down
5 changes: 3 additions & 2 deletions import_export_celery/models/importjob.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@

from import_export.formats.base_formats import DEFAULT_FORMATS

from ..fields import ImportExportFileField
from ..tasks import run_import_job


@with_author
class ImportJob(models.Model):
file = models.FileField(
file = ImportExportFileField(
verbose_name=_("File to be imported"),
upload_to="django-import-export-celery-import-jobs",
blank=False,
Expand All @@ -45,7 +46,7 @@ class ImportJob(models.Model):
max_length=255,
)

change_summary = models.FileField(
change_summary = ImportExportFileField(
verbose_name=_("Summary of changes made by this import"),
upload_to="django-import-export-celery-import-change-summaries",
blank=True,
Expand Down

0 comments on commit 5a54a81

Please sign in to comment.