Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17374 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit c5dcba4159e00ed7fc43b605946be489602ab369 1 parent e308cfc
Ramiro Morales authored January 15, 2012
12  django/template/defaultfilters.py
@@ -7,7 +7,7 @@
7 7
 from functools import wraps
8 8
 from pprint import pformat
9 9
 
10  
-from django.template.base import Variable, Library
  10
+from django.template.base import Variable, Library, VariableDoesNotExist
11 11
 from django.conf import settings
12 12
 from django.utils import formats
13 13
 from django.utils.dateformat import format, time_format
@@ -490,7 +490,10 @@ def dictsort(value, arg):
490 490
     Takes a list of dicts, returns that list sorted by the property given in
491 491
     the argument.
492 492
     """
493  
-    return sorted(value, key=Variable(arg).resolve)
  493
+    try:
  494
+        return sorted(value, key=Variable(arg).resolve)
  495
+    except (TypeError, VariableDoesNotExist):
  496
+        return u''
494 497
 
495 498
 @register.filter(is_safe=False)
496 499
 def dictsortreversed(value, arg):
@@ -498,7 +501,10 @@ def dictsortreversed(value, arg):
498 501
     Takes a list of dicts, returns that list sorted in reverse order by the
499 502
     property given in the argument.
500 503
     """
501  
-    return sorted(value, key=Variable(arg).resolve, reverse=True)
  504
+    try:
  505
+        return sorted(value, key=Variable(arg).resolve, reverse=True)
  506
+    except (TypeError, VariableDoesNotExist):
  507
+        return u''
502 508
 
503 509
 @register.filter(is_safe=False)
504 510
 def first(value):
14  tests/regressiontests/defaultfilters/tests.py
@@ -368,6 +368,13 @@ def test_dictsort(self):
368 368
              [('age', 23), ('name', 'Barbara-Ann')],
369 369
              [('age', 63), ('name', 'Ra Ra Rasputin')]])
370 370
 
  371
+        # If it gets passed a list of something else different from
  372
+        # dictionaries it should fail silently
  373
+        self.assertEqual(dictsort([1, 2, 3], 'age'), '')
  374
+        self.assertEqual(dictsort('Hello!', 'age'), '')
  375
+        self.assertEqual(dictsort({'a': 1}, 'age'), '')
  376
+        self.assertEqual(dictsort(1, 'age'), '')
  377
+
371 378
     def test_dictsortreversed(self):
372 379
         sorted_dicts = dictsortreversed([{'age': 23, 'name': 'Barbara-Ann'},
373 380
                                          {'age': 63, 'name': 'Ra Ra Rasputin'},
@@ -379,6 +386,13 @@ def test_dictsortreversed(self):
379 386
              [('age', 23), ('name', 'Barbara-Ann')],
380 387
              [('age', 18), ('name', 'Jonny B Goode')]])
381 388
 
  389
+        # If it gets passed a list of something else different from
  390
+        # dictionaries it should fail silently
  391
+        self.assertEqual(dictsortreversed([1, 2, 3], 'age'), '')
  392
+        self.assertEqual(dictsortreversed('Hello!', 'age'), '')
  393
+        self.assertEqual(dictsortreversed({'a': 1}, 'age'), '')
  394
+        self.assertEqual(dictsortreversed(1, 'age'), '')
  395
+
382 396
     def test_first(self):
383 397
         self.assertEqual(first([0,1,2]), 0)
384 398
         self.assertEqual(first(u''), u'')

0 notes on commit c5dcba4

Please sign in to comment.
Something went wrong with that request. Please try again.