Ticket #6904: 6904_revision7393_better.diff
File 6904_revision7393_better.diff, 2.5 KB (added by , 17 years ago) |
---|
-
django/template/defaultfilters.py
416 416 Takes a list of dicts, returns that list sorted by the property given in 417 417 the argument. 418 418 """ 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 419 426 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] 421 428 decorated.sort() 422 429 return [item[1] for item in decorated] 423 430 dictsort.is_safe = False … … 427 434 Takes a list of dicts, returns that list sorted in reverse order by the 428 435 property given in the argument. 429 436 """ 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 430 445 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] 432 447 decorated.sort() 433 448 decorated.reverse() 434 449 return [item[1] for item in decorated] -
tests/regressiontests/defaultfilters/tests.py
236 236 ... {'name': 'Jonny B Goode', 'age': 18}], 'age') 237 237 [{'age': 63, 'name': 'Ra Ra Rasputin'}, {'age': 23, 'name': 'Barbara-Ann'}, {'age': 18, 'name': 'Jonny B Goode'}] 238 238 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 239 249 >>> first([0,1,2]) 240 250 0 241 251