Skip to content

Commit

Permalink
fix: Respect custom filer and thumbnail storage settings (#1413)
Browse files Browse the repository at this point in the history
* Fix #1377

* fix bug #1412

* Fix: differentiate source_storage and thumbnail_storage

* Update tests to run on non-standard storage settings.

* Update CHANGELOG.rst

* Replace `re_path` by `path`

* Remove `re_path` from tests

* Bump version
  • Loading branch information
fsbraun authored Aug 22, 2023
1 parent 7c55f45 commit 3940e1c
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 22 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
CHANGELOG
=========

unreleased
==========
3.0.5 (2023-08-22)
==================

* Fix bug that ignored thumbnail storage custom settings in directory view
* remove Django 2.2, 3.0, and 3.1 classifiers in setup.py
* remove tests for Django < 3.2 since those versions are not supported anymore

Expand Down
2 changes: 1 addition & 1 deletion filer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
8. Publish the release and it will automatically release to pypi
"""

__version__ = '3.0.4'
__version__ = '3.0.5'
2 changes: 1 addition & 1 deletion filer/fields/multistorage_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class MultiStorageFieldFile(ThumbnailerNameMixin,
def __init__(self, instance, field, name):
"""
This is a little weird, but I couldn't find a better solution.
Thumbnailer.__init__ is called first for proper object inizialization.
Thumbnailer.__init__ is called first for proper object initialisation.
Then we override some attributes defined at runtime with properties.
We cannot simply call super().__init__ because filer Field objects
doesn't have a storage attribute.
Expand Down
8 changes: 4 additions & 4 deletions filer/templatetags/filer_admin_tags.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from math import ceil

from django.contrib.staticfiles.storage import staticfiles_storage
from django.core.files.storage import FileSystemStorage, default_storage
from django.core.files.storage import FileSystemStorage
from django.template import Library
from django.templatetags.static import static
from django.urls import reverse
Expand Down Expand Up @@ -109,7 +109,7 @@ def file_icon_context(file, detail, width, height):
'height': width, # The icon is a square
}
# Check if file exists for performance reasons (only on FileSystemStorage)
if isinstance(default_storage, FileSystemStorage) and file.file and not file.file.exists():
if file.file and isinstance(file.file.source_storage, FileSystemStorage) and not file.file.exists():
return not_available_context

if isinstance(file, BaseImage):
Expand All @@ -131,9 +131,9 @@ def file_icon_context(file, detail, width, height):
configured_name = thumbnailer.get_thumbnail_name(thumbnail_options, transparent=file._transparent)
# If the name was annotated: Thumbnail exists and we can use it
if configured_name == file.thumbnail_name:
icon_url = default_storage.url(configured_name)
icon_url = file.file.thumbnail_storage.url(configured_name)
if mime_subtype != 'svg+xml' and file.thumbnailx2_name:
context['highres_url'] = default_storage.url(file.thumbnailx2_name)
context['highres_url'] = file.file.thumbnail_storage.url(file.thumbnailx2_name)
else: # Probably does not exist, defer creation
icon_url = reverse("admin:filer_file_fileicon", args=(file.pk, width))
context['alt_text'] = file.default_alt_text
Expand Down
6 changes: 3 additions & 3 deletions filer/urls.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from django.urls import re_path
from django.urls import path

from . import settings as filer_settings
from . import views


urlpatterns = [
re_path(
filer_settings.FILER_CANONICAL_URL + r'(?P<uploaded_at>[0-9]+)/(?P<file_id>[0-9]+)/$',
path(
filer_settings.FILER_CANONICAL_URL + '<int:uploaded_at>/<int:file_id>/',
views.canonical,
name='canonical'
),
Expand Down
16 changes: 16 additions & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,22 @@
'FILE_UPLOAD_TEMP_DIR': mkdtemp(),
'TEMPLATE_DIRS': (os.path.join(BASE_DIR, 'django-filer', 'filer', 'utils', 'templates'),),
'FILER_CANONICAL_URL': 'test-path/',
'FILER_STORAGES': {
"public": {
"main": {
"ENGINE": 'django.core.files.storage.FileSystemStorage',
"OPTIONS": {
"base_url": "/media/my-preferred-base-url-for-source-files/",
}
},
"thumbnails": {
"ENGINE": 'django.core.files.storage.FileSystemStorage',
"OPTIONS": {
"base_url": "/media/my-preferred-base-url-for-thumbnails/",
}
}
}
},
'SECRET_KEY': '__secret__',
'DEFAULT_AUTO_FIELD': 'django.db.models.AutoField',
}
Expand Down
11 changes: 6 additions & 5 deletions tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ def test_filer_directory_listing_performance(self):
# 7. Selecting file and owner data
response = self.client.get(reverse('admin:filer-directory_listing-unfiled_images'))
self.assertContains(response, "test_image_0.jpg")

self.assertContains(response, "/media/my-preferred-base-url-for-source-files/")
self.assertContains(response, "/media/my-preferred-base-url-for-thumbnails/")
for thumbnail_url in thumbnail_urls:
self.assertContains(response, thumbnail_url)

Expand Down Expand Up @@ -1693,8 +1694,8 @@ def test_image_icon_with_size(self):
image.save()
context = {}
height, width, context = get_aspect_ratio_and_download_url(context=context, detail=True, file=image, height=40, width=40)
assert 'sidebar_image_ratio' in context.keys()
assert 'download_url' in context.keys()
self.assertIn('sidebar_image_ratio', context.keys())
self.assertIn('download_url', context.keys())

def test_file_icon_with_size(self):
"""
Expand All @@ -1703,5 +1704,5 @@ def test_file_icon_with_size(self):
file = File.objects.create(name='test.pdf')
context = {}
height, width, context = get_aspect_ratio_and_download_url(context=context, detail=True, file=file, height=40, width=40)
assert 'sidebar_image_ratio' not in context.keys()
assert 'download_url' in context.keys()
self.assertNotIn('sidebar_image_ratio', context.keys())
self.assertIn('download_url', context.keys())
14 changes: 8 additions & 6 deletions tests/utils/urls.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
from django.conf import settings
from django.contrib import admin
from django.urls import include, re_path
from django.urls import include, path
from django.views.static import serve


admin.autodiscover()
admin_urls = admin.site.urls

urlpatterns = [
re_path(r'^media/(?P<path>.*)$', serve,
{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
re_path(r'^admin/', admin_urls),
re_path(r'^', include('filer.server.urls')),
re_path(r'^filer/', include('filer.urls')),
path('media/my-preferred-base-url-for-source-files/<path:path>', serve,
{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
path('media/my-preferred-base-url-for-thumbnails/<path:path>', serve,
{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
path('admin/', admin_urls),
path('', include('filer.server.urls')),
path('filer/', include('filer.urls')),
]

0 comments on commit 3940e1c

Please sign in to comment.