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

Add custom file storage setting: "FILE_STORAGE_BACKEND" #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ Please refer to django-ses package [documentation](https://github.com/django-ses
DJANGO_SES_PLUS_SETTINGS = {
"SEND_EMAIL": True, # True by default.
"CELERY_TASK_RETRY_KWARGS": {
'max_retries': 15,
'max_retries': 15,
'countdown': 60
}
},
"FILE_STORAGE_BACKEND": None, # Should be a subclass of django.core.files.storage.Storage.
# Default is None which uses DEFAULT_FILE_STORAGE.
}
```

Expand Down
4 changes: 2 additions & 2 deletions django_ses_plus/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from django.utils.translation import ugettext_lazy as _

from django_ses_plus.settings import DJANGO_SES_PLUS_SETTINGS
from .utils import sent_email_upload_path
from .utils import get_file_storage_backend, sent_email_upload_path


class SentEmail(models.Model):
Expand All @@ -19,7 +19,7 @@ class SentEmail(models.Model):
to_email = models.EmailField()
from_email = models.EmailField()
subject = models.TextField()
html = models.FileField(upload_to=sent_email_upload_path)
html = models.FileField(upload_to=sent_email_upload_path, storage=get_file_storage_backend)
Copy link
Contributor

@alicertel alicertel Feb 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the storage field accept callable? I though it's expecting instance of a storage or none.

Copy link
Author

@KaratasFurkan KaratasFurkan Feb 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It accepts callable, I checked the source code and also tested in one of our projects.

It calls the callable and gets an instance of subclass of django.core.files.storage.Storage.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thank you.


creation_datetime = models.DateTimeField(auto_now_add=True)
update_datetime = models.DateTimeField(auto_now=True)
Expand Down
1 change: 1 addition & 0 deletions django_ses_plus/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

DJANGO_SES_PLUS_SETTINGS.setdefault("SEND_EMAIL", True)
DJANGO_SES_PLUS_SETTINGS.setdefault("CELERY_TASK_RETRY_KWARGS", {'max_retries': 15, 'countdown': 60})
DJANGO_SES_PLUS_SETTINGS.setdefault("FILE_STORAGE_BACKEND", None)

# Get default from email from django settings.
DJANGO_SES_PLUS_SETTINGS.setdefault("DEFAULT_FROM_EMAIL", settings.DEFAULT_FROM_EMAIL)
9 changes: 9 additions & 0 deletions django_ses_plus/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
from time import strftime

from django.utils.module_loading import import_string

from .settings import DJANGO_SES_PLUS_SETTINGS


def sent_email_upload_path(sent_email, filename):
filename = filename.split(".")[0]
return strftime("emails/%Y/%m/%d/%Y-%m-%d-%H-%M-%S-{}.html".format(filename))


def get_file_storage_backend():
if DJANGO_SES_PLUS_SETTINGS["FILE_STORAGE_BACKEND"]:
return import_string(DJANGO_SES_PLUS_SETTINGS["FILE_STORAGE_BACKEND"])()