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

simple implementation of Django STORAGES #182

Merged
merged 3 commits into from
Jan 23, 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
14 changes: 11 additions & 3 deletions categories/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.core.files.images import get_image_dimensions
from django.core.files.storage import get_storage_class
from django.db import models
from django.urls import reverse
from django.utils.encoding import force_str
Expand All @@ -15,10 +14,19 @@
RELATION_MODELS,
RELATIONS,
THUMBNAIL_STORAGE,
THUMBNAIL_STORAGE_ALIAS,
THUMBNAIL_UPLOAD_PATH,
)

STORAGE = get_storage_class(THUMBNAIL_STORAGE)
# Determine storage method based on Django version
try: # Django 4.2+
from django.core.files.storage import storages

STORAGE = storages[THUMBNAIL_STORAGE_ALIAS]
except ImportError:
from django.core.files.storage import get_storage_class

Check warning on line 27 in categories/models.py

View check run for this annotation

Codecov / codecov/patch

categories/models.py#L26-L27

Added lines #L26 - L27 were not covered by tests

STORAGE = get_storage_class(THUMBNAIL_STORAGE)()

Check warning on line 29 in categories/models.py

View check run for this annotation

Codecov / codecov/patch

categories/models.py#L29

Added line #L29 was not covered by tests


class Category(CategoryBase):
Expand All @@ -28,7 +36,7 @@
upload_to=THUMBNAIL_UPLOAD_PATH,
null=True,
blank=True,
storage=STORAGE(),
storage=STORAGE,
)
thumbnail_width = models.IntegerField(blank=True, null=True)
thumbnail_height = models.IntegerField(blank=True, null=True)
Expand Down
1 change: 1 addition & 0 deletions categories/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"FK_REGISTRY": {},
"THUMBNAIL_UPLOAD_PATH": "uploads/categories/thumbnails",
"THUMBNAIL_STORAGE": settings.DEFAULT_FILE_STORAGE,
"THUMBNAIL_STORAGE_ALIAS": "default",
"JAVASCRIPT_URL": getattr(settings, "STATIC_URL", settings.MEDIA_URL) + "js/",
"SLUG_TRANSLITERATOR": "",
"REGISTER_ADMIN": True,
Expand Down
9 changes: 8 additions & 1 deletion categories/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import os

import django
from django.core.files import File
from django.core.files.uploadedfile import UploadedFile
from django.test import TestCase

from categories.models import Category
from example.test_storages import MyTestStorage, MyTestStorageAlias


class TestCategoryThumbnail(TestCase):
def test_thumbnail(self):
file_name = "test_image.jpg"

with open(os.path.join(os.path.dirname(__file__), file_name), "rb") as f:
test_image = UploadedFile(File(f), content_type="image/jpeg")
category = Category.objects.create(name="Test Category", slug="test-category", thumbnail=test_image)

self.assertEqual(category.pk, 1)
self.assertEqual(category.thumbnail_width, 640)
self.assertEqual(category.thumbnail_height, 480)

if django.VERSION >= (4, 2):
self.assertEqual(category.thumbnail.storage.__class__, MyTestStorageAlias)
else:
self.assertEqual(category.thumbnail.storage.__class__, MyTestStorage)
10 changes: 10 additions & 0 deletions doc_src/reference/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ The default settings are:
'FK_REGISTRY': {},
'THUMBNAIL_UPLOAD_PATH': 'uploads/categories/thumbnails',
'THUMBNAIL_STORAGE': settings.DEFAULT_FILE_STORAGE,
'THUMBNAIL_STORAGE_ALIAS': 'default',
'SLUG_TRANSLITERATOR': lambda x: x,
'ADMIN_FIELDSETS': {}
}
Expand Down Expand Up @@ -112,6 +113,15 @@ THUMBNAIL_STORAGE

**Description:** How to store the thumbnails. Allows for external storage engines like S3.

.. _THUMBNAIL_STORAGE:

THUMBNAIL_STORAGE_ALIAS
=======================

**Default:** ``default``

**Description:** If new STORAGES settings from Django 4.2+ is used, use storage with this alias.

.. _JAVASCRIPT_URL:

JAVASCRIPT_URL
Expand Down
16 changes: 16 additions & 0 deletions example/settings-testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@
},
]


STORAGES = {
"default": {
"BACKEND": "django.core.files.storage.FileSystemStorage",
},
"staticfiles": {
"BACKEND": "django.core.files.storage.FileSystemStorage",
},
"thumbnails": {
"BACKEND": "example.test_storages.MyTestStorageAlias",
},
}


CATEGORIES_SETTINGS = {
"ALLOW_SLUG_CHANGE": True,
"RELATION_MODELS": ["simpletext.simpletext", "flatpages.flatpage"],
Expand All @@ -115,6 +129,8 @@
{"name": "more_categories", "related_name": "more_cats"},
),
},
"THUMBNAIL_STORAGE": "example.test_storages.MyTestStorage",
"THUMBNAIL_STORAGE_ALIAS": "thumbnails",
}

TEST_RUNNER = "django.test.runner.DiscoverRunner"
1 change: 1 addition & 0 deletions example/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
}
]


CATEGORIES_SETTINGS = {
"ALLOW_SLUG_CHANGE": True,
"RELATION_MODELS": ["simpletext.simpletext", "flatpages.flatpage"],
Expand Down
10 changes: 10 additions & 0 deletions example/test_storages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Storages used for testing THUMBNAIL_STORAGE and THUMBNAIL_STORAGE_ALIAS
from django.core.files.storage import FileSystemStorage


class MyTestStorage(FileSystemStorage):
pass


class MyTestStorageAlias(FileSystemStorage):
pass
Loading