From 96b412643e5cc5321636212350d47eb96a4aab42 Mon Sep 17 00:00:00 2001
From: Daniel Barreto <daniel@gia.usb.ve>
Date: Sat, 12 Nov 2011 19:34:54 -0430
Subject: [PATCH] Fixed #15652 -- dictsort and dictsortreversed won't raise TemplateSyntaxError when not provided with a list of dictionaries.
---
django/template/defaultfilters.py | 12 +++++++++---
tests/regressiontests/defaultfilters/tests.py | 14 ++++++++++++++
2 files changed, 23 insertions(+), 3 deletions(-)
diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py
index 7484c7b..b27fe08 100644
a
|
b
|
from decimal import Decimal, InvalidOperation, ROUND_HALF_UP
|
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 |
… |
… |
def dictsort(value, arg):
|
484 | 484 | Takes a list of dicts, returns that list sorted by the property given in |
485 | 485 | the argument. |
486 | 486 | """ |
487 | | return sorted(value, key=Variable(arg).resolve) |
| 487 | try: |
| 488 | return sorted(value, key=Variable(arg).resolve) |
| 489 | except (TypeError, VariableDoesNotExist): |
| 490 | return u'' |
488 | 491 | |
489 | 492 | @register.filter(is_safe=False) |
490 | 493 | def dictsortreversed(value, arg): |
… |
… |
def dictsortreversed(value, arg):
|
492 | 495 | Takes a list of dicts, returns that list sorted in reverse order by the |
493 | 496 | property given in the argument. |
494 | 497 | """ |
495 | | return sorted(value, key=Variable(arg).resolve, reverse=True) |
| 498 | try: |
| 499 | return sorted(value, key=Variable(arg).resolve, reverse=True) |
| 500 | except (TypeError, VariableDoesNotExist): |
| 501 | return u'' |
496 | 502 | |
497 | 503 | @register.filter(is_safe=False) |
498 | 504 | def first(value): |
diff --git a/tests/regressiontests/defaultfilters/tests.py b/tests/regressiontests/defaultfilters/tests.py
index b18334d..6662f0e 100644
a
|
b
|
class DefaultFiltersTests(TestCase):
|
299 | 299 | [('age', 23), ('name', 'Barbara-Ann')], |
300 | 300 | [('age', 63), ('name', 'Ra Ra Rasputin')]]) |
301 | 301 | |
| 302 | # Check a list of something else different of dictionary will |
| 303 | # fail silently |
| 304 | self.assertEqual(dictsort([1, 2, 3], 'age'), '') |
| 305 | self.assertEqual(dictsort('Hello!', 'age'), '') |
| 306 | self.assertEqual(dictsort({'a': 1}, 'age'), '') |
| 307 | self.assertEqual(dictsort(1, 'age'), '') |
| 308 | |
302 | 309 | def test_dictsortreversed(self): |
303 | 310 | sorted_dicts = dictsortreversed([{'age': 23, 'name': 'Barbara-Ann'}, |
304 | 311 | {'age': 63, 'name': 'Ra Ra Rasputin'}, |
… |
… |
class DefaultFiltersTests(TestCase):
|
310 | 317 | [('age', 23), ('name', 'Barbara-Ann')], |
311 | 318 | [('age', 18), ('name', 'Jonny B Goode')]]) |
312 | 319 | |
| 320 | # Check a list of something else different of dictionary will |
| 321 | # fail silently |
| 322 | self.assertEqual(dictsort([1, 2, 3], 'age'), '') |
| 323 | self.assertEqual(dictsort('Hello!', 'age'), '') |
| 324 | self.assertEqual(dictsort({'a': 1}, 'age'), '') |
| 325 | self.assertEqual(dictsort(1, 'age'), '') |
| 326 | |
313 | 327 | def test_first(self): |
314 | 328 | self.assertEqual(first([0,1,2]), 0) |
315 | 329 | self.assertEqual(first(u''), u'') |