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

Fix usage of safe_filter #723

Merged
merged 3 commits into from
Apr 4, 2023
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,4 @@ tests/thumbnail_kvstore.lock
.coverage
.tox
htmlcov/
coverage.xml
10 changes: 5 additions & 5 deletions sorl/thumbnail/templatetags/thumbnail.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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'<img(?: alt="(%(re_cap)s)?")? src="(%(re_img)s)"', value)
3 changes: 3 additions & 0 deletions tests/thumbnail_tests/templates/thumbnail4a.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% load thumbnail %}{% spaceless %}
{% if source|is_portrait %}yes{% else %}no{% endif %}
{% endspaceless %}
18 changes: 18 additions & 0 deletions tests/thumbnail_tests/test_templatetags.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,24 @@ def test_portrait(self):
'<img src="/media/test/cache/82/62/8262858c5f95f2bd7715d7aaa3e52b11.jpg" '
'width="79" height="66" class="landscape">')

val = render_to_string('thumbnail4.html', {
'source': 'https://dummyimage.com/100x120/',
'dims': 'x66',
}).strip()
self.assertEqual(val, '<img width="1" height="1" class="portrait">')

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, '<p>empty</p>')
Expand Down