Skip to content
Myr

Made join filter work with async

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 7641542 · Browse files · 2 files+37 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

2 files+37 added−2 removed

  • tests:+29 added−0 removed
  • other:+8 added−2 removed

2 files+37 added−2 removed

modified:jinja2/asyncfilters.py+8 added-2 removed
15 unmodified lines
161718def dualfilter(normal_filter, async_filter):19 wrap_evalctx = False20 if getattr(normal_filter, 'environmentfilter'):20 if getattr(normal_filter, 'environmentfilter', False):21 is_async = lambda args: args[0].is_async22 wrap_evalctx = False23 else:24 if not getattr(normal_filter, 'evalcontextfilter'):24 if not getattr(normal_filter, 'evalcontextfilter', False):25 wrap_evalctx = True26 is_async = lambda args: args[0].environment.is_async2728 @wraps(normal_filter)
32 unmodified lines
61 for key, values in filters.groupby(sorted(62 await auto_to_seq(value), key=expr), expr)]636465@asyncfiltervariant(filters.do_join)66async def do_join(eval_ctx, value, d=u'', attribute=None):67 return filters.do_join(eval_ctx, await auto_to_seq(value), d, attribute)686970ASYNC_FILTERS = {71 'first': do_first,72 'groupby': do_groupby,73 'join': do_join,74}
modified:tests/test_asyncfilters.py+29 added
1import pytest2from jinja2 import Environment3from jinja2.utils import Markup456async def make_aiter(iter):7 for item in iter:
79 unmodified lines
87 '1970[aha][interesting][really?]',88 '1971[totally not]',89 ''90 ]919293@mark_dualiter('int_items', lambda: [1, 2, 3])94def test_join(env_async, int_items):95 tmpl = env_async.from_string('{{ items()|join("|") }}')96 out = tmpl.render(items=int_items)97 assert out == '1|2|3'9899100@mark_dualiter('string_items', lambda: ["<foo>", Markup("<span>foo</span>")])101def test_join(string_items):102 env2 = Environment(autoescape=True, enable_async=True)103 tmpl = env2.from_string(104 '{{ ["<foo>", "<span>foo</span>"|safe]|join }}')105 assert tmpl.render(items=string_items) == '&lt;foo&gt;<span>foo</span>'106107108def make_users():109 class User(object):110 def __init__(self, username):111 self.username = username112 return map(User, ['foo', 'bar'])113114115@mark_dualiter('users', make_users)116def test_join_attribute(env_async, users):117 tmpl = env_async.from_string('''{{ users()|join(', ', 'username') }}''')118 assert tmpl.render(users=users) == 'foo, bar'