Ticket #13828: 13828_dry_dictsort_3.diff

File 13828_dry_dictsort_3.diff, 3.4 KB (added by Łukasz Rekucki, 14 years ago)

Simplified patch.

  • django/template/defaultfilters.py

    diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py
    index 1500a05..1606dad 100644
    a b def dictsort(value, arg):  
    479479    Takes a list of dicts, returns that list sorted by the property given in
    480480    the argument.
    481481    """
    482     var_resolve = Variable(arg).resolve
    483     decorated = [(var_resolve(item), item) for item in value]
    484     decorated.sort()
    485     return [item[1] for item in decorated]
     482    return sorted(value, key=Variable(arg).resolve)
    486483dictsort.is_safe = False
    487484
    488485def dictsortreversed(value, arg):
    def dictsortreversed(value, arg):  
    490487    Takes a list of dicts, returns that list sorted in reverse order by the
    491488    property given in the argument.
    492489    """
    493     var_resolve = Variable(arg).resolve
    494     decorated = [(var_resolve(item), item) for item in value]
    495     decorated.sort()
    496     decorated.reverse()
    497     return [item[1] for item in decorated]
     490    return sorted(value, key=Variable(arg).resolve, reverse=True)
    498491dictsortreversed.is_safe = False
    499492
    500493def first(value):
  • tests/regressiontests/defaultfilters/tests.py

    diff --git a/tests/regressiontests/defaultfilters/tests.py b/tests/regressiontests/defaultfilters/tests.py
    index 27014a7..98deb8e 100644
    a b class DefaultFiltersTests(unittest.TestCase):  
    260260            u'some html with alert("You smell") disallowed  tags')
    261261
    262262    def test_dictsort(self):
    263         sorted_dicts = dictsort([{'age': 23, 'name': 'Barbara-Ann'},
    264                                  {'age': 63, 'name': 'Ra Ra Rasputin'},
    265                                  {'name': 'Jonny B Goode', 'age': 18}], 'age')
    266 
     263        dicts = [{'age': 23, 'name': 'Barbara-Ann'},
     264                 {'age': 63, 'name': 'Ra Ra Rasputin'},
     265                 {'age': 18, 'name': 'Jonny B Goode'}]
     266        sorted_dicts = dictsort(dicts, 'age')
    267267        self.assertEqual([sorted(dict.items()) for dict in sorted_dicts],
    268268            [[('age', 18), ('name', 'Jonny B Goode')],
    269269             [('age', 23), ('name', 'Barbara-Ann')],
    270270             [('age', 63), ('name', 'Ra Ra Rasputin')]])
     271        self.assertEqual(dicts, [{'age': 23, 'name': 'Barbara-Ann'},
     272                 {'age': 63, 'name': 'Ra Ra Rasputin'},
     273                 {'age': 18, 'name': 'Jonny B Goode'}])
    271274
    272275    def test_dictsortreversed(self):
    273         sorted_dicts = dictsortreversed([{'age': 23, 'name': 'Barbara-Ann'},
    274                                          {'age': 63, 'name': 'Ra Ra Rasputin'},
    275                                          {'name': 'Jonny B Goode', 'age': 18}],
    276                                         'age')
    277 
     276        dicts = [{'age': 23, 'name': 'Barbara-Ann'},
     277                 {'age': 63, 'name': 'Ra Ra Rasputin'},
     278                 {'age': 18, 'name': 'Jonny B Goode'}]
     279        sorted_dicts = dictsortreversed(dicts, 'age')
    278280        self.assertEqual([sorted(dict.items()) for dict in sorted_dicts],
    279281            [[('age', 63), ('name', 'Ra Ra Rasputin')],
    280282             [('age', 23), ('name', 'Barbara-Ann')],
    281283             [('age', 18), ('name', 'Jonny B Goode')]])
     284        self.assertEqual(dicts, [{'age': 23, 'name': 'Barbara-Ann'},
     285                 {'age': 63, 'name': 'Ra Ra Rasputin'},
     286                 {'age': 18, 'name': 'Jonny B Goode'}])
    282287
    283288    def test_first(self):
    284289        self.assertEqual(first([0,1,2]), 0)
Back to Top