Skip to content

Commit 3940e1c

Browse files
authored
fix: Respect custom filer and thumbnail storage settings (#1413)
* 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
1 parent 7c55f45 commit 3940e1c

File tree

8 files changed

+42
-22
lines changed

8 files changed

+42
-22
lines changed

CHANGELOG.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
CHANGELOG
33
=========
44

5-
unreleased
6-
==========
5+
3.0.5 (2023-08-22)
6+
==================
77

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

filer/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313
8. Publish the release and it will automatically release to pypi
1414
"""
1515

16-
__version__ = '3.0.4'
16+
__version__ = '3.0.5'

filer/fields/multistorage_file.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class MultiStorageFieldFile(ThumbnailerNameMixin,
7070
def __init__(self, instance, field, name):
7171
"""
7272
This is a little weird, but I couldn't find a better solution.
73-
Thumbnailer.__init__ is called first for proper object inizialization.
73+
Thumbnailer.__init__ is called first for proper object initialisation.
7474
Then we override some attributes defined at runtime with properties.
7575
We cannot simply call super().__init__ because filer Field objects
7676
doesn't have a storage attribute.

filer/templatetags/filer_admin_tags.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from math import ceil
22

33
from django.contrib.staticfiles.storage import staticfiles_storage
4-
from django.core.files.storage import FileSystemStorage, default_storage
4+
from django.core.files.storage import FileSystemStorage
55
from django.template import Library
66
from django.templatetags.static import static
77
from django.urls import reverse
@@ -109,7 +109,7 @@ def file_icon_context(file, detail, width, height):
109109
'height': width, # The icon is a square
110110
}
111111
# Check if file exists for performance reasons (only on FileSystemStorage)
112-
if isinstance(default_storage, FileSystemStorage) and file.file and not file.file.exists():
112+
if file.file and isinstance(file.file.source_storage, FileSystemStorage) and not file.file.exists():
113113
return not_available_context
114114

115115
if isinstance(file, BaseImage):
@@ -131,9 +131,9 @@ def file_icon_context(file, detail, width, height):
131131
configured_name = thumbnailer.get_thumbnail_name(thumbnail_options, transparent=file._transparent)
132132
# If the name was annotated: Thumbnail exists and we can use it
133133
if configured_name == file.thumbnail_name:
134-
icon_url = default_storage.url(configured_name)
134+
icon_url = file.file.thumbnail_storage.url(configured_name)
135135
if mime_subtype != 'svg+xml' and file.thumbnailx2_name:
136-
context['highres_url'] = default_storage.url(file.thumbnailx2_name)
136+
context['highres_url'] = file.file.thumbnail_storage.url(file.thumbnailx2_name)
137137
else: # Probably does not exist, defer creation
138138
icon_url = reverse("admin:filer_file_fileicon", args=(file.pk, width))
139139
context['alt_text'] = file.default_alt_text

filer/urls.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
from django.urls import re_path
1+
from django.urls import path
22

33
from . import settings as filer_settings
44
from . import views
55

66

77
urlpatterns = [
8-
re_path(
9-
filer_settings.FILER_CANONICAL_URL + r'(?P<uploaded_at>[0-9]+)/(?P<file_id>[0-9]+)/$',
8+
path(
9+
filer_settings.FILER_CANONICAL_URL + '<int:uploaded_at>/<int:file_id>/',
1010
views.canonical,
1111
name='canonical'
1212
),

tests/settings.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,22 @@
5252
'FILE_UPLOAD_TEMP_DIR': mkdtemp(),
5353
'TEMPLATE_DIRS': (os.path.join(BASE_DIR, 'django-filer', 'filer', 'utils', 'templates'),),
5454
'FILER_CANONICAL_URL': 'test-path/',
55+
'FILER_STORAGES': {
56+
"public": {
57+
"main": {
58+
"ENGINE": 'django.core.files.storage.FileSystemStorage',
59+
"OPTIONS": {
60+
"base_url": "/media/my-preferred-base-url-for-source-files/",
61+
}
62+
},
63+
"thumbnails": {
64+
"ENGINE": 'django.core.files.storage.FileSystemStorage',
65+
"OPTIONS": {
66+
"base_url": "/media/my-preferred-base-url-for-thumbnails/",
67+
}
68+
}
69+
}
70+
},
5571
'SECRET_KEY': '__secret__',
5672
'DEFAULT_AUTO_FIELD': 'django.db.models.AutoField',
5773
}

tests/test_admin.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ def test_filer_directory_listing_performance(self):
124124
# 7. Selecting file and owner data
125125
response = self.client.get(reverse('admin:filer-directory_listing-unfiled_images'))
126126
self.assertContains(response, "test_image_0.jpg")
127-
127+
self.assertContains(response, "/media/my-preferred-base-url-for-source-files/")
128+
self.assertContains(response, "/media/my-preferred-base-url-for-thumbnails/")
128129
for thumbnail_url in thumbnail_urls:
129130
self.assertContains(response, thumbnail_url)
130131

@@ -1693,8 +1694,8 @@ def test_image_icon_with_size(self):
16931694
image.save()
16941695
context = {}
16951696
height, width, context = get_aspect_ratio_and_download_url(context=context, detail=True, file=image, height=40, width=40)
1696-
assert 'sidebar_image_ratio' in context.keys()
1697-
assert 'download_url' in context.keys()
1697+
self.assertIn('sidebar_image_ratio', context.keys())
1698+
self.assertIn('download_url', context.keys())
16981699

16991700
def test_file_icon_with_size(self):
17001701
"""
@@ -1703,5 +1704,5 @@ def test_file_icon_with_size(self):
17031704
file = File.objects.create(name='test.pdf')
17041705
context = {}
17051706
height, width, context = get_aspect_ratio_and_download_url(context=context, detail=True, file=file, height=40, width=40)
1706-
assert 'sidebar_image_ratio' not in context.keys()
1707-
assert 'download_url' in context.keys()
1707+
self.assertNotIn('sidebar_image_ratio', context.keys())
1708+
self.assertIn('download_url', context.keys())

tests/utils/urls.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
from django.conf import settings
22
from django.contrib import admin
3-
from django.urls import include, re_path
3+
from django.urls import include, path
44
from django.views.static import serve
55

66

77
admin.autodiscover()
88
admin_urls = admin.site.urls
99

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

0 commit comments

Comments
 (0)