Ticket #15652: 0001-Fixed-15652-dictsort-and-dictsortreversed-won-t-rais.patch

File 0001-Fixed-15652-dictsort-and-dictsortreversed-won-t-rais.patch, 3.4 KB (added by Daniel Barreto, 13 years ago)
  • django/template/defaultfilters.py

    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  
    77from functools import wraps
    88from pprint import pformat
    99
    10 from django.template.base import Variable, Library
     10from django.template.base import Variable, Library, VariableDoesNotExist
    1111from django.conf import settings
    1212from django.utils import formats
    1313from django.utils.dateformat import format, time_format
    def dictsort(value, arg):  
    484484    Takes a list of dicts, returns that list sorted by the property given in
    485485    the argument.
    486486    """
    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''
    488491
    489492@register.filter(is_safe=False)
    490493def dictsortreversed(value, arg):
    def dictsortreversed(value, arg):  
    492495    Takes a list of dicts, returns that list sorted in reverse order by the
    493496    property given in the argument.
    494497    """
    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''
    496502
    497503@register.filter(is_safe=False)
    498504def first(value):
  • tests/regressiontests/defaultfilters/tests.py

    diff --git a/tests/regressiontests/defaultfilters/tests.py b/tests/regressiontests/defaultfilters/tests.py
    index b18334d..6662f0e 100644
    a b class DefaultFiltersTests(TestCase):  
    299299             [('age', 23), ('name', 'Barbara-Ann')],
    300300             [('age', 63), ('name', 'Ra Ra Rasputin')]])
    301301
     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
    302309    def test_dictsortreversed(self):
    303310        sorted_dicts = dictsortreversed([{'age': 23, 'name': 'Barbara-Ann'},
    304311                                         {'age': 63, 'name': 'Ra Ra Rasputin'},
    class DefaultFiltersTests(TestCase):  
    310317             [('age', 23), ('name', 'Barbara-Ann')],
    311318             [('age', 18), ('name', 'Jonny B Goode')]])
    312319
     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
    313327    def test_first(self):
    314328        self.assertEqual(first([0,1,2]), 0)
    315329        self.assertEqual(first(u''), u'')
Back to Top