Skip to content

Commit a950cd3

Browse files
authored
Fix usage of safe_filter
* Ignore coverage.xml * Added test to check if safe_filter is actually applied to the registered template filter is_portrait. * Apply safe_filter first and then register the template filter.
1 parent b5f4d95 commit a950cd3

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,4 @@ tests/thumbnail_kvstore.lock
6262
.coverage
6363
.tox
6464
htmlcov/
65+
coverage.xml

sorl/thumbnail/templatetags/thumbnail.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ def thumbnail(parser, token):
191191
return ThumbnailNode(parser, token)
192192

193193

194-
@safe_filter(error_output=False)
195194
@register.filter
195+
@safe_filter(error_output=False)
196196
def is_portrait(file_):
197197
"""
198198
A very handy filter to determine if an image is portrait or landscape.
@@ -205,8 +205,8 @@ def is_portrait(file_):
205205
return image_file.is_portrait()
206206

207207

208-
@safe_filter(error_output='auto')
209208
@register.filter
209+
@safe_filter(error_output='auto')
210210
def margin(file_, geometry_string):
211211
"""
212212
Returns the calculated margin for an image and geometry
@@ -237,8 +237,8 @@ def margin(file_, geometry_string):
237237
return ' '.join(['%dpx' % n for n in margin])
238238

239239

240-
@safe_filter(error_output='auto')
241240
@register.filter
241+
@safe_filter(error_output='auto')
242242
def background_margin(file_, geometry_string):
243243
"""
244244
Returns the calculated margin for a background image and geometry
@@ -278,13 +278,13 @@ def text_filter(regex_base, value):
278278
return value
279279

280280

281-
@safe_filter(error_output='auto')
282281
@register.filter
282+
@safe_filter(error_output='auto')
283283
def markdown_thumbnails(value):
284284
return text_filter(r'!\[(%(re_cap)s)?\][ ]?\((%(re_img)s)\)', value)
285285

286286

287-
@safe_filter(error_output='auto')
288287
@register.filter
288+
@safe_filter(error_output='auto')
289289
def html_thumbnails(value):
290290
return text_filter(r'<img(?: alt="(%(re_cap)s)?")? src="(%(re_img)s)"', value)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{% load thumbnail %}{% spaceless %}
2+
{% if source|is_portrait %}yes{% else %}no{% endif %}
3+
{% endspaceless %}

tests/thumbnail_tests/test_templatetags.py

+18
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,24 @@ def test_portrait(self):
132132
'<img src="/media/test/cache/82/62/8262858c5f95f2bd7715d7aaa3e52b11.jpg" '
133133
'width="79" height="66" class="landscape">')
134134

135+
val = render_to_string('thumbnail4.html', {
136+
'source': 'https://dummyimage.com/100x120/',
137+
'dims': 'x66',
138+
}).strip()
139+
self.assertEqual(val, '<img width="1" height="1" class="portrait">')
140+
141+
with override_settings(THUMBNAIL_DEBUG=True):
142+
with self.assertRaises(FileNotFoundError):
143+
render_to_string('thumbnail4a.html', {
144+
'source': 'broken.jpeg',
145+
}).strip()
146+
147+
with override_settings(THUMBNAIL_DEBUG=False):
148+
val = render_to_string('thumbnail4a.html', {
149+
'source': 'broken.jpeg',
150+
}).strip()
151+
self.assertEqual(val, 'no')
152+
135153
def test_empty(self):
136154
val = render_to_string('thumbnail5.html', {}).strip()
137155
self.assertEqual(val, '<p>empty</p>')

0 commit comments

Comments
 (0)