sort now also accepts an attribute
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent dd8afa2 · Browse files · 3 files·+29 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+29 added−1 removed
- other:+29 added−1 removed
Provenance
- Commit
b7e75b8- Tree
15cd06a- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
dd8afa2
3 files changed+29 added−1 removed
modified:jinja2/testsuite/filters.py+9 added
255 unmodified lines256 def test_sort3(self):257 tmpl = env.from_string('''{{ ['foo', 'Bar', 'blah']|sort }}''')258 assert tmpl.render() == "['Bar', 'blah', 'foo']"259 260+ def test_sort4(self):261+ class Magic(object):262+ def __init__(self, value):263+ self.value = value264+ def __unicode__(self):265+ return unicode(self.value)266+ tmpl = env.from_string('''{{ items|sort(attribute='value')|join }}''')267+ assert tmpl.render(items=map(Magic, [3, 2, 4, 1])) == '1234'268+269 def test_groupby(self):270 tmpl = env.from_string('''271 {%- for grouper, list in [{'foo': 1, 'bar': 2},272 {'foo': 2, 'bar': 3},70 unmodified linesmodified:jinja2/filters.py+19 added-1 removed
189 unmodified lines190 191 return sorted(value.items(), key=sort_func)192 193 194-def do_sort(value, reverse=False, case_sensitive=False):194+@environmentfilter195+def do_sort(environment, value, reverse=False, case_sensitive=False,196+ attribute=None):197 """Sort an iterable. Per default it sorts ascending, if you pass it198 true as first argument it will reverse the sorting.199 200 If the iterable is made of strings the third parameter can be used to4 unmodified lines205 206 {% for item in iterable|sort %}207 ...208 {% endfor %}209+210+ It is also possible to sort by an attribute (for example to sort211+ by the date of an object) by specifying the `attribute` parameter:212+213+ .. sourcecode:: jinja214+215+ {% for item in iterable|sort(attribute='date') %}216+ ...217+ {% endfor %}218+219+ .. versionchanged:: 2.6220+ The `attribute` parameter was added.221 """222 if not case_sensitive:223 def sort_func(item):224 if isinstance(item, basestring):225 item = item.lower()226 return item227 else:228 sort_func = None229+ if attribute is not None:230+ getter = make_attrgetter(environment, attribute)231+ def sort_func(item, processor=sort_func or (lambda x: x)):232+ return processor(getter(item))233 return sorted(value, key=sort_func, reverse=reverse)234 235 236 def do_default(value, default_value=u'', boolean=False):533 unmodified linesmodified:CHANGES+1 added
26 unmodified lines27 - the sum filter can now sum up values by attribute. This is a backwards28 incompatible change. The argument to the filter previously was the29 optional starting index which defaultes to zero. This now became the30 second argument to the function because it's rarely used.31+- like sum, sort now also makes it possible to order items by attribute.32 33 Version 2.5.534 -------------35 (re-release of 2.5.4 with built documentation removed for filesize.229 unmodified lines