Ticket #6904: 6904_revision7393_better.diff

File 6904_revision7393_better.diff, 2.5 KB (added by Michael Axiak, 17 years ago)

Better patch...

  • django/template/defaultfilters.py

     
    416416    Takes a list of dicts, returns that list sorted by the property given in
    417417    the argument.
    418418    """
     419    def lower_if_string(object):
     420        # Use try/except because I would expect most objects
     421        # to be strings...
     422        try:
     423            return object.lower()
     424        except AttributeError:
     425            return object
    419426    var_resolve = Variable(arg).resolve
    420     decorated = [(var_resolve(item), item) for item in value]
     427    decorated = [(lower_if_string(var_resolve(item)), item) for item in value]
    421428    decorated.sort()
    422429    return [item[1] for item in decorated]
    423430dictsort.is_safe = False
     
    427434    Takes a list of dicts, returns that list sorted in reverse order by the
    428435    property given in the argument.
    429436    """
     437    def lower_if_string(object):
     438        # Use try/except because I would expect most objects
     439        # to be strings...
     440        try:
     441            return object.lower()
     442        except AttributeError:
     443            return object
     444           
    430445    var_resolve = Variable(arg).resolve
    431     decorated = [(var_resolve(item), item) for item in value]
     446    decorated = [(lower_if_string(var_resolve(item)), item) for item in value]
    432447    decorated.sort()
    433448    decorated.reverse()
    434449    return [item[1] for item in decorated]
  • tests/regressiontests/defaultfilters/tests.py

     
    236236...           {'name': 'Jonny B Goode', 'age': 18}], 'age')
    237237[{'age': 63, 'name': 'Ra Ra Rasputin'}, {'age': 23, 'name': 'Barbara-Ann'}, {'age': 18, 'name': 'Jonny B Goode'}]
    238238
     239>>> dictsort([{'age': 23, 'name': 'Barbara-Ann'},
     240...           {'age': 63, 'name': 'Ra Ra Rasputin'},
     241...           {'name': 'jonny B Goode', 'age': 18}], 'name')
     242[{'age': 23, 'name': 'Barbara-Ann'}, {'age': 18, 'name': 'jonny B Goode'}, {'age': 63, 'name': 'Ra Ra Rasputin'}]
     243
     244>>> dictsortreversed([{'age': 23, 'name': 'Barbara-Ann'},
     245...           {'age': 63, 'name': 'Ra Ra Rasputin'},
     246...           {'name': 'jonny B Goode', 'age': 18}], 'name')
     247[{'age': 63, 'name': 'Ra Ra Rasputin'}, {'age': 18, 'name': 'jonny B Goode'}, {'age': 23, 'name': 'Barbara-Ann'}]
     248
    239249>>> first([0,1,2])
    2402500
    241251
Back to Top