From 737954909febeabbabda84b933fa78066bb5c3ef Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Sun, 23 May 2010 23:07:08 +0200 Subject: [PATCH] Version 2.4.2 ------------- (bugfix release, release date to be announced) improved the sort filter (should have worked like this for a long time) by adding support for case insensitive searches. fixed a bug for getattribute constant folding. --HG-- branch : trunk --- CHANGES | 10 +++++++--- jinja2/filters.py | 21 ++++++++------------- jinja2/testsuite/filters.py | 4 ++++ 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/CHANGES b/CHANGES index 46e4d05..bbe3bd3 100644 --- a/CHANGES +++ b/CHANGES @@ -1,9 +1,13 @@ Jinja2 Changelog ================ -Version 2.5 ------------ -(codename not be selected, release date to be announced) +Version 2.4.2 +------------- +(bugfix release, release date to be announced) + +- improved the sort filter (should have worked like this for a + long time) by adding support for case insensitive searches. +- fixed a bug for getattribute constant folding. Version 2.4.1 ------------- diff --git a/jinja2/filters.py b/jinja2/filters.py index c0b8bee..073277b 100644 --- a/jinja2/filters.py +++ b/jinja2/filters.py @@ -176,10 +176,13 @@ def do_dictsort(value, case_sensitive=False, by='key'): return sorted(value.items(), key=sort_func) -def do_sort(value, case_sensitive=False): - """Sort an iterable. If the iterable is made of strings the second - parameter can be used to control the case sensitiveness of the - comparison which is disabled by default. +def do_sort(value, reverse=False, case_sensitive=False): + """Sort an iterable. Per default it sorts ascending, if you pass it + true as first argument it will reverse the sorting. + + If the iterable is made of strings the third parameter can be used to + control the case sensitiveness of the comparison which is disabled by + default. .. sourcecode:: jinja @@ -194,7 +197,7 @@ def do_sort(value, case_sensitive=False): return item else: sort_func = None - return sorted(seq, key=sort_func) + return sorted(value, key=sort_func, reverse=reverse) def do_default(value, default_value=u'', boolean=False): @@ -564,13 +567,6 @@ def do_round(value, precision=0, method='common'): return func(value) -def do_sort(value, reverse=False): - """Sort a sequence. Per default it sorts ascending, if you pass it - true as first argument it will reverse the sorting. - """ - return sorted(value, reverse=reverse) - - @environmentfilter def do_groupby(environment, value, attribute): """Group a sequence of objects by a common attribute. @@ -723,7 +719,6 @@ FILTERS = { 'sum': sum, 'abs': abs, 'round': do_round, - 'sort': do_sort, 'groupby': do_groupby, 'safe': do_mark_safe, 'xmlattr': do_xmlattr diff --git a/jinja2/testsuite/filters.py b/jinja2/testsuite/filters.py index eea52a6..ea015e5 100644 --- a/jinja2/testsuite/filters.py +++ b/jinja2/testsuite/filters.py @@ -227,6 +227,10 @@ class FilterTestCase(JinjaTestCase): tmpl = env.from_string('{{ [2, 3, 1]|sort }}|{{ [2, 3, 1]|sort(true) }}') assert tmpl.render() == '[1, 2, 3]|[3, 2, 1]' + def test_sort2(self): + tmpl = env.from_string('{{ "".join(["c", "A", "b", "D"]|sort(false, true)) }}') + assert tmpl.render() == 'AbcD' + def test_groupby(self): tmpl = env.from_string(''' {%- for grouper, list in [{'foo': 1, 'bar': 2}, -- 2.26.2