groupby now supports attributes of attributes. This fixes #10
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 9adf937 · Browse files · 4 files·+40 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+40 added−2 removed
- other:+40 added−2 removed
Provenance
- Commit
9573b66- Tree
9c1b794- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
9adf937
4 files changed+40 added−2 removed
modified:jinja2/testsuite/filters.py+26 added
250 unmodified lines251 "3: 3, 4",252 ""253 ]254 255+ def test_groupby_multidot(self):256+ class Date(object):257+ def __init__(self, day, month, year):258+ self.day = day259+ self.month = month260+ self.year = year261+ class Article(object):262+ def __init__(self, title, *date):263+ self.date = Date(*date)264+ self.title = title265+ articles = [266+ Article('aha', 1, 1, 1970),267+ Article('interesting', 2, 1, 1970),268+ Article('really?', 3, 1, 1970),269+ Article('totally not', 1, 1, 1971)270+ ]271+ tmpl = env.from_string('''272+ {%- for year, list in articles|groupby('date.year') -%}273+ {{ year }}{% for x in list %}[{{ x.title }}]{% endfor %}|274+ {%- endfor %}''')275+ assert tmpl.render(articles=articles).split('|') == [276+ '1970[aha][interesting][really?]',277+ '1971[totally not]',278+ ''279+ ]280+281 def test_filtertag(self):282 tmpl = env.from_string("{% filter upper|replace('FOO', 'foo') %}"283 "foobar{% endfilter %}")284 assert tmpl.render() == 'fooBAR'33 unmodified linesmodified:jinja2/compiler.py+1 added-1 removed
11 unmodified lines12 from itertools import chain13 from copy import deepcopy14 from jinja2 import nodes15 from jinja2.nodes import EvalContext16-from jinja2.visitor import NodeVisitor, NodeTransformer16+from jinja2.visitor import NodeVisitor17 from jinja2.exceptions import TemplateAssertionError18 from jinja2.utils import Markup, concat, escape, is_python_keyword, next19 20 1632 unmodified linesmodified:jinja2/filters.py+11 added-1 removed
593 unmodified lines594 595 As you can see the item we're grouping by is stored in the `grouper`596 attribute and the `list` contains all the objects that have this grouper597 in common.598+599+ .. versionchanged:: 2.6600+ It's now possible to use dotted notation to group by the child601+ attribute of another attribute.602 """603+ if '.' in attribute:604+ def expr(item):605+ for part in attribute.split('.'):606+ item = environment.getitem(item, part)607+ return item608+ else:599- expr = lambda x: environment.getitem(x, attribute)609+ expr = lambda x: environment.getitem(x, attribute)610 return sorted(map(_GroupTuple, groupby(sorted(value, key=expr), expr)))611 612 613 class _GroupTuple(tuple):116 unmodified linesmodified:CHANGES+2 added
11 unmodified lines12 - implemented operator intercepting for sandboxed environments. This13 allows application developers to disable builtin operators for better14 security. (For instance limit the mathematical operators to actual15 integers instead of longs)16+- groupby filter now supports dotted notation for grouping by attributes17+ of attributes.18 19 Version 2.5.520 -------------21 (re-release of 2.5.4 with built documentation removed for filesize.229 unmodified lines