Skip to content

Commit

Permalink
Made dictsort and dictsort reversed template filters fail silently
Browse files Browse the repository at this point in the history
when passed list of things that aren't dictionaries.

Thanks Harris Lapiroff for the report and Daniel Barreto for the patch.

Fixes #15652.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17374 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
ramiro committed Jan 15, 2012
1 parent e308cfc commit c5dcba4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
12 changes: 9 additions & 3 deletions django/template/defaultfilters.py
Expand Up @@ -7,7 +7,7 @@
from functools import wraps from functools import wraps
from pprint import pformat from pprint import pformat


from django.template.base import Variable, Library from django.template.base import Variable, Library, VariableDoesNotExist
from django.conf import settings from django.conf import settings
from django.utils import formats from django.utils import formats
from django.utils.dateformat import format, time_format from django.utils.dateformat import format, time_format
Expand Down Expand Up @@ -490,15 +490,21 @@ def dictsort(value, arg):
Takes a list of dicts, returns that list sorted by the property given in Takes a list of dicts, returns that list sorted by the property given in
the argument. the argument.
""" """
return sorted(value, key=Variable(arg).resolve) try:
return sorted(value, key=Variable(arg).resolve)
except (TypeError, VariableDoesNotExist):
return u''


@register.filter(is_safe=False) @register.filter(is_safe=False)
def dictsortreversed(value, arg): def dictsortreversed(value, arg):
""" """
Takes a list of dicts, returns that list sorted in reverse order by the Takes a list of dicts, returns that list sorted in reverse order by the
property given in the argument. property given in the argument.
""" """
return sorted(value, key=Variable(arg).resolve, reverse=True) try:
return sorted(value, key=Variable(arg).resolve, reverse=True)
except (TypeError, VariableDoesNotExist):
return u''


@register.filter(is_safe=False) @register.filter(is_safe=False)
def first(value): def first(value):
Expand Down
14 changes: 14 additions & 0 deletions tests/regressiontests/defaultfilters/tests.py
Expand Up @@ -368,6 +368,13 @@ def test_dictsort(self):
[('age', 23), ('name', 'Barbara-Ann')], [('age', 23), ('name', 'Barbara-Ann')],
[('age', 63), ('name', 'Ra Ra Rasputin')]]) [('age', 63), ('name', 'Ra Ra Rasputin')]])


# If it gets passed a list of something else different from
# dictionaries it should fail silently
self.assertEqual(dictsort([1, 2, 3], 'age'), '')
self.assertEqual(dictsort('Hello!', 'age'), '')
self.assertEqual(dictsort({'a': 1}, 'age'), '')
self.assertEqual(dictsort(1, 'age'), '')

def test_dictsortreversed(self): def test_dictsortreversed(self):
sorted_dicts = dictsortreversed([{'age': 23, 'name': 'Barbara-Ann'}, sorted_dicts = dictsortreversed([{'age': 23, 'name': 'Barbara-Ann'},
{'age': 63, 'name': 'Ra Ra Rasputin'}, {'age': 63, 'name': 'Ra Ra Rasputin'},
Expand All @@ -379,6 +386,13 @@ def test_dictsortreversed(self):
[('age', 23), ('name', 'Barbara-Ann')], [('age', 23), ('name', 'Barbara-Ann')],
[('age', 18), ('name', 'Jonny B Goode')]]) [('age', 18), ('name', 'Jonny B Goode')]])


# If it gets passed a list of something else different from
# dictionaries it should fail silently
self.assertEqual(dictsortreversed([1, 2, 3], 'age'), '')
self.assertEqual(dictsortreversed('Hello!', 'age'), '')
self.assertEqual(dictsortreversed({'a': 1}, 'age'), '')
self.assertEqual(dictsortreversed(1, 'age'), '')

def test_first(self): def test_first(self):
self.assertEqual(first([0,1,2]), 0) self.assertEqual(first([0,1,2]), 0)
self.assertEqual(first(u''), u'') self.assertEqual(first(u''), u'')
Expand Down

0 comments on commit c5dcba4

Please sign in to comment.