Anonymous avatar Anonymous committed e0b63c4

Made dictsort and dictsort reversed template filters fail silently
when passed list of things that aren't dictionaries.

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

Fixes #15652.

Comments (0)

Files changed (2)

django/template/defaultfilters.py

 from functools import wraps
 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.utils import formats
 from django.utils.dateformat import format, time_format
     Takes a list of dicts, returns that list sorted by the property given in
     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)
 def dictsortreversed(value, arg):
     Takes a list of dicts, returns that list sorted in reverse order by the
     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)
 def first(value):

tests/regressiontests/defaultfilters/tests.py

              [('age', 23), ('name', 'Barbara-Ann')],
              [('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):
         sorted_dicts = dictsortreversed([{'age': 23, 'name': 'Barbara-Ann'},
                                          {'age': 63, 'name': 'Ra Ra Rasputin'},
              [('age', 23), ('name', 'Barbara-Ann')],
              [('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):
         self.assertEqual(first([0,1,2]), 0)
         self.assertEqual(first(u''), u'')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.