Skip to content

Commit

Permalink
1.6.1dev: allow passing a generator to max_filter and min_filter,…
Browse files Browse the repository at this point in the history
… and fix a typo `mix` for `min` (closes #13773)

git-svn-id: http://trac.edgewall.org/intertrac/log:/branches/1.6-stable@17849 af82e41b-90c4-0310-8c96-b1721e28e2e2
  • Loading branch information
jomae committed Oct 3, 2024
1 parent 186e5c3 commit 22b5064
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
10 changes: 3 additions & 7 deletions trac/util/presentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def jinja2_update(jenv):
groupattr=groupattr_filter,
htmlattr=htmlattr_filter,
max=max_filter,
mix=min_filter,
min=min_filter,
trim=trim_filter,
)
jenv.tests.update(
Expand Down Expand Up @@ -138,15 +138,11 @@ def htmlattr_filter(_eval_ctx, d, autospace=True):

def max_filter(seq, default=None):
"""Returns the max value from the sequence."""
if len(seq):
return max(seq)
return default
return max(seq, default=default)

def min_filter(seq, default=None):
"""Returns the min value from the sequence."""
if len(seq):
return min(seq)
return default
return min(seq, default=default)


def trim_filter(value, what=None):
Expand Down
17 changes: 17 additions & 0 deletions trac/web/tests/chrome.py
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,23 @@ def test(domain=None):
test(domain=None)
test(domain='messages')

def test_filters_max_min(self):
template = textwrap.dedent("""\
${names|map('capitalize')|max(default='(max)')}
${names|map('capitalize')|min(default='(min)')}
${names|reject('string')|max(default='(max)')}
${names|reject('string')|min(default='(min)')}
""")
filename = 'ticket13773.html'
create_file(os.path.join(self.env.templates_dir, filename), template)

req = MockRequest(self.env)
data = {'names': ['alpha', 'beta', 'gamma']}
content = self.chrome.render_template(req, filename, data,
{'iterable': False})
self.assertEqual(['Gamma', 'Alpha', '(max)', '(min)'],
str(content, 'utf-8').splitlines())


def test_suite():
suite = unittest.TestSuite()
Expand Down

0 comments on commit 22b5064

Please sign in to comment.