Author: gwilson
Date: 2009-03-30 11:48:24 -0500 (Mon, 30 Mar 2009)
New Revision: 10194
Modified:
django/branches/releases/1.0.X/django/template/defaultfilters.py
django/branches/releases/1.0.X/tests/regressiontests/templates/filters.py
Log:
[1.0.X]: Fixed #8462 -- Made `length` and `length_is` template filters fail
silently when given a value that has undefined length and added tests for both
filters. Based on patch from marcelor, rob, and SmileyChris.
Backport of r10193 from trunk.
Modified: django/branches/releases/1.0.X/django/template/defaultfilters.py
===================================================================
--- django/branches/releases/1.0.X/django/template/defaultfilters.py
2009-03-30 16:46:27 UTC (rev 10193)
+++ django/branches/releases/1.0.X/django/template/defaultfilters.py
2009-03-30 16:48:24 UTC (rev 10194)
@@ -507,12 +507,18 @@
def length(value):
"""Returns the length of the value - useful for lists."""
- return len(value)
+ try:
+ return len(value)
+ except (ValueError, TypeError):
+ return ''
length.is_safe = True
def length_is(value, arg):
"""Returns a boolean of whether the value's length is the argument."""
- return len(value) == int(arg)
+ try:
+ return len(value) == int(arg)
+ except (ValueError, TypeError):
+ return ''
length_is.is_safe = False
def random(value):
Modified:
django/branches/releases/1.0.X/tests/regressiontests/templates/filters.py
===================================================================
--- django/branches/releases/1.0.X/tests/regressiontests/templates/filters.py
2009-03-30 16:46:27 UTC (rev 10193)
+++ django/branches/releases/1.0.X/tests/regressiontests/templates/filters.py
2009-03-30 16:48:24 UTC (rev 10194)
@@ -278,13 +278,33 @@
'escapejs01': (r'{{ a|escapejs }}', {'a': 'testing\r\njavascript
\'string" <b>escaping</b>'}, 'testing\\x0D\\x0Ajavascript \\x27string\\x22
\\x3Cb\\x3Eescaping\\x3C/b\\x3E'),
'escapejs02': (r'{% autoescape off %}{{ a|escapejs }}{% endautoescape
%}', {'a': 'testing\r\njavascript \'string" <b>escaping</b>'},
'testing\\x0D\\x0Ajavascript \\x27string\\x22 \\x3Cb\\x3Eescaping\\x3C/b\\x3E'),
+
+ # length filter.
+ 'length01': ('{{ list|length }}', {'list': ['4', None, True, {}]},
'4'),
+ 'length02': ('{{ list|length }}', {'list': []}, '0'),
+ 'length03': ('{{ string|length }}', {'string': ''}, '0'),
+ 'length04': ('{{ string|length }}', {'string': 'django'}, '6'),
+ # Invalid uses that should fail silently.
+ 'length05': ('{{ int|length }}', {'int': 7}, ''),
+ 'length06': ('{{ None|length }}', {'None': None}, ''),
+
+ # length_is filter.
+ 'length_is01': ('{% if some_list|length_is:"4" %}Four{% endif %}',
{'some_list': ['4', None, True, {}]}, 'Four'),
+ 'length_is02': ('{% if some_list|length_is:"4" %}Four{% else %}Not
Four{% endif %}', {'some_list': ['4', None, True, {}, 17]}, 'Not Four'),
+ 'length_is03': ('{% if mystring|length_is:"4" %}Four{% endif %}',
{'mystring': 'word'}, 'Four'),
+ 'length_is04': ('{% if mystring|length_is:"4" %}Four{% else %}Not
Four{% endif %}', {'mystring': 'Python'}, 'Not Four'),
+ 'length_is05': ('{% if mystring|length_is:"4" %}Four{% else %}Not
Four{% endif %}', {'mystring': ''}, 'Not Four'),
+ 'length_is06': ('{% with var|length as my_length %}{{ my_length }}{%
endwith %}', {'var': 'django'}, '6'),
# Boolean return value from length_is should not be coerced to a string
- 'lengthis01': (r'{% if "X"|length_is:0 %}Length is 0{% else %}Length
not 0{% endif %}', {}, 'Length not 0'),
- 'lengthis02': (r'{% if "X"|length_is:1 %}Length is 1{% else %}Length
not 1{% endif %}', {}, 'Length is 1'),
+ 'length_is07': (r'{% if "X"|length_is:0 %}Length is 0{% else %}Length
not 0{% endif %}', {}, 'Length not 0'),
+ 'length_is08': (r'{% if "X"|length_is:1 %}Length is 1{% else %}Length
not 1{% endif %}', {}, 'Length is 1'),
+ # Invalid uses that should fail silently.
+ 'length_is09': ('{{ var|length_is:"fish" }}', {'var': 'django'}, ''),
+ 'length_is10': ('{{ int|length_is:"1" }}', {'int': 7}, ''),
+ 'length_is11': ('{{ none|length_is:"1" }}', {'none': None}, ''),
'join01': (r'{{ a|join:", " }}', {'a': ['alpha', 'beta & me']},
'alpha, beta & me'),
'join02': (r'{% autoescape off %}{{ a|join:", " }}{% endautoescape
%}', {'a': ['alpha', 'beta & me']}, 'alpha, beta & me'),
'join03': (r'{{ a|join:" & " }}', {'a': ['alpha', 'beta & me']},
'alpha & beta & me'),
'join04': (r'{% autoescape off %}{{ a|join:" & " }}{%
endautoescape %}', {'a': ['alpha', 'beta & me']}, 'alpha & beta & me'),
}
-
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---