Skip to content

Commit

Permalink
simple implementation of Django STORAGES
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrDlouhy committed Jan 23, 2024
1 parent c674948 commit 5d1bbfb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
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

STORAGE = get_storage_class(THUMBNAIL_STORAGE)()


class Category(CategoryBase):
Expand All @@ -28,7 +36,7 @@ class Category(CategoryBase):
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

0 comments on commit 5d1bbfb

Please sign in to comment.