From 1f4a54438bcaa5465acf422e15dd732146900f42 Mon Sep 17 00:00:00 2001 From: Steffen Jasper Date: Tue, 7 Feb 2023 17:32:50 +0100 Subject: [PATCH 1/3] Ignore coverage.xml --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index fbd77fdc7..151972cc0 100644 --- a/.gitignore +++ b/.gitignore @@ -62,3 +62,4 @@ tests/thumbnail_kvstore.lock .coverage .tox htmlcov/ +coverage.xml From 22604926433fd8a7f8ca2d3f1560cb4b09e4f056 Mon Sep 17 00:00:00 2001 From: Steffen Jasper Date: Tue, 7 Feb 2023 17:54:58 +0100 Subject: [PATCH 2/3] Added test to check if safe_filter is actually applied to the registered template filter is_portrait. --- .../thumbnail_tests/templates/thumbnail4a.html | 3 +++ tests/thumbnail_tests/test_templatetags.py | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 tests/thumbnail_tests/templates/thumbnail4a.html diff --git a/tests/thumbnail_tests/templates/thumbnail4a.html b/tests/thumbnail_tests/templates/thumbnail4a.html new file mode 100644 index 000000000..e5a63d345 --- /dev/null +++ b/tests/thumbnail_tests/templates/thumbnail4a.html @@ -0,0 +1,3 @@ +{% load thumbnail %}{% spaceless %} +{% if source|is_portrait %}yes{% else %}no{% endif %} +{% endspaceless %} diff --git a/tests/thumbnail_tests/test_templatetags.py b/tests/thumbnail_tests/test_templatetags.py index 248f2b9e9..90ed5ebc0 100644 --- a/tests/thumbnail_tests/test_templatetags.py +++ b/tests/thumbnail_tests/test_templatetags.py @@ -132,6 +132,24 @@ def test_portrait(self): '') + val = render_to_string('thumbnail4.html', { + 'source': 'https://dummyimage.com/100x120/', + 'dims': 'x66', + }).strip() + self.assertEqual(val, '') + + with override_settings(THUMBNAIL_DEBUG=True): + with self.assertRaises(FileNotFoundError): + render_to_string('thumbnail4a.html', { + 'source': 'broken.jpeg', + }).strip() + + with override_settings(THUMBNAIL_DEBUG=False): + val = render_to_string('thumbnail4a.html', { + 'source': 'broken.jpeg', + }).strip() + self.assertEqual(val, 'no') + def test_empty(self): val = render_to_string('thumbnail5.html', {}).strip() self.assertEqual(val, '

empty

') From bef11b0d5f7fb185ce898da2990edb3cfa213d67 Mon Sep 17 00:00:00 2001 From: Steffen Jasper Date: Tue, 7 Feb 2023 17:56:28 +0100 Subject: [PATCH 3/3] Apply safe_filter first and then register the template filter. --- sorl/thumbnail/templatetags/thumbnail.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sorl/thumbnail/templatetags/thumbnail.py b/sorl/thumbnail/templatetags/thumbnail.py index d0dd296ff..f97e0eb0d 100644 --- a/sorl/thumbnail/templatetags/thumbnail.py +++ b/sorl/thumbnail/templatetags/thumbnail.py @@ -191,8 +191,8 @@ def thumbnail(parser, token): return ThumbnailNode(parser, token) -@safe_filter(error_output=False) @register.filter +@safe_filter(error_output=False) def is_portrait(file_): """ A very handy filter to determine if an image is portrait or landscape. @@ -205,8 +205,8 @@ def is_portrait(file_): return image_file.is_portrait() -@safe_filter(error_output='auto') @register.filter +@safe_filter(error_output='auto') def margin(file_, geometry_string): """ Returns the calculated margin for an image and geometry @@ -237,8 +237,8 @@ def margin(file_, geometry_string): return ' '.join(['%dpx' % n for n in margin]) -@safe_filter(error_output='auto') @register.filter +@safe_filter(error_output='auto') def background_margin(file_, geometry_string): """ Returns the calculated margin for a background image and geometry @@ -278,13 +278,13 @@ def text_filter(regex_base, value): return value -@safe_filter(error_output='auto') @register.filter +@safe_filter(error_output='auto') def markdown_thumbnails(value): return text_filter(r'!\[(%(re_cap)s)?\][ ]?\((%(re_img)s)\)', value) -@safe_filter(error_output='auto') @register.filter +@safe_filter(error_output='auto') def html_thumbnails(value): return text_filter(r'