mirror of
https://github.com/django/django.git
synced 2025-10-26 07:06:08 +00:00
Fixed #25670 -- Allowed dictsort to sort a list of lists.
Thanks Tim Graham for the review.
This commit is contained in:
committed by
Tim Graham
parent
cdbd8745f6
commit
e81d1c995c
@@ -5,6 +5,7 @@ import random as random_module
|
||||
import re
|
||||
from decimal import ROUND_HALF_UP, Context, Decimal, InvalidOperation
|
||||
from functools import wraps
|
||||
from operator import itemgetter
|
||||
from pprint import pformat
|
||||
|
||||
from django.utils import formats, six
|
||||
@@ -510,6 +511,32 @@ def striptags(value):
|
||||
# LISTS #
|
||||
###################
|
||||
|
||||
def _property_resolver(arg):
|
||||
"""
|
||||
When arg is convertible to float, behave like operator.itemgetter(arg)
|
||||
Otherwise, behave like Variable(arg).resolve
|
||||
|
||||
>>> _property_resolver(1)('abc')
|
||||
'b'
|
||||
>>> _property_resolver('1')('abc')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
TypeError: string indices must be integers
|
||||
>>> class Foo:
|
||||
... a = 42
|
||||
... b = 3.14
|
||||
... c = 'Hey!'
|
||||
>>> _property_resolver('b')(Foo())
|
||||
3.14
|
||||
"""
|
||||
try:
|
||||
float(arg)
|
||||
except ValueError:
|
||||
return Variable(arg).resolve
|
||||
else:
|
||||
return itemgetter(arg)
|
||||
|
||||
|
||||
@register.filter(is_safe=False)
|
||||
def dictsort(value, arg):
|
||||
"""
|
||||
@@ -517,7 +544,7 @@ def dictsort(value, arg):
|
||||
the argument.
|
||||
"""
|
||||
try:
|
||||
return sorted(value, key=Variable(arg).resolve)
|
||||
return sorted(value, key=_property_resolver(arg))
|
||||
except (TypeError, VariableDoesNotExist):
|
||||
return ''
|
||||
|
||||
@@ -529,7 +556,7 @@ def dictsortreversed(value, arg):
|
||||
property given in the argument.
|
||||
"""
|
||||
try:
|
||||
return sorted(value, key=Variable(arg).resolve, reverse=True)
|
||||
return sorted(value, key=_property_resolver(arg), reverse=True)
|
||||
except (TypeError, VariableDoesNotExist):
|
||||
return ''
|
||||
|
||||
|
||||
@@ -813,7 +813,7 @@ TECHNICAL_500_TEMPLATE = ("""
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for var in frame.vars|dictsort:"0" %}
|
||||
{% for var in frame.vars|dictsort:0 %}
|
||||
<tr>
|
||||
<td>{{ var.0|force_escape }}</td>
|
||||
<td class="code"><pre>{{ var.1 }}</pre></td>
|
||||
@@ -995,7 +995,7 @@ Exception Value: {{ exception_value|force_escape }}
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for var in request.META.items|dictsort:"0" %}
|
||||
{% for var in request.META.items|dictsort:0 %}
|
||||
<tr>
|
||||
<td>{{ var.0 }}</td>
|
||||
<td class="code"><pre>{{ var.1|pprint }}</pre></td>
|
||||
@@ -1017,7 +1017,7 @@ Exception Value: {{ exception_value|force_escape }}
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for var in settings.items|dictsort:"0" %}
|
||||
{% for var in settings.items|dictsort:0 %}
|
||||
<tr>
|
||||
<td>{{ var.0 }}</td>
|
||||
<td class="code"><pre>{{ var.1|pprint }}</pre></td>
|
||||
@@ -1107,12 +1107,12 @@ FILES:{% for k, v in request.FILES.items %}
|
||||
COOKIES:{% for k, v in request.COOKIES.items %}
|
||||
{{ k }} = {{ v|stringformat:"r" }}{% empty %} No cookie data{% endfor %}
|
||||
|
||||
META:{% for k, v in request.META.items|dictsort:"0" %}
|
||||
META:{% for k, v in request.META.items|dictsort:0 %}
|
||||
{{ k }} = {{ v|stringformat:"r" }}{% endfor %}
|
||||
{% else %}Request data not supplied
|
||||
{% endif %}
|
||||
Settings:
|
||||
Using settings module {{ settings.SETTINGS_MODULE }}{% for k, v in settings.items|dictsort:"0" %}
|
||||
Using settings module {{ settings.SETTINGS_MODULE }}{% for k, v in settings.items|dictsort:0 %}
|
||||
{{ k }} = {{ v|stringformat:"r" }}{% endfor %}
|
||||
|
||||
{% if not is_email %}
|
||||
|
||||
Reference in New Issue
Block a user