summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArmin Ronacher <armin.ronacher@active-4.com>2016-12-28 23:49:29 +0100
committerArmin Ronacher <armin.ronacher@active-4.com>2016-12-28 23:49:33 +0100
commit1df67cf172b5d875571a51a41c688a7db9956959 (patch)
tree9d4cecaa33e513c511fca2512939e6988112e095
parent76415429803fa389a13852b4c845047da53b0405 (diff)
downloadjinja2-1df67cf172b5d875571a51a41c688a7db9956959.tar.gz
Made join filter work with async
-rw-r--r--jinja2/asyncfilters.py10
-rw-r--r--tests/test_asyncfilters.py29
2 files changed, 37 insertions, 2 deletions
diff --git a/jinja2/asyncfilters.py b/jinja2/asyncfilters.py
index e879893..c621ae4 100644
--- a/jinja2/asyncfilters.py
+++ b/jinja2/asyncfilters.py
@@ -17,11 +17,11 @@ async def auto_to_seq(value):
def dualfilter(normal_filter, async_filter):
wrap_evalctx = False
- if getattr(normal_filter, 'environmentfilter'):
+ if getattr(normal_filter, 'environmentfilter', False):
is_async = lambda args: args[0].is_async
wrap_evalctx = False
else:
- if not getattr(normal_filter, 'evalcontextfilter'):
+ if not getattr(normal_filter, 'evalcontextfilter', False):
wrap_evalctx = True
is_async = lambda args: args[0].environment.is_async
@@ -62,7 +62,13 @@ async def do_groupby(environment, value, attribute):
await auto_to_seq(value), key=expr), expr)]
+@asyncfiltervariant(filters.do_join)
+async def do_join(eval_ctx, value, d=u'', attribute=None):
+ return filters.do_join(eval_ctx, await auto_to_seq(value), d, attribute)
+
+
ASYNC_FILTERS = {
'first': do_first,
'groupby': do_groupby,
+ 'join': do_join,
}
diff --git a/tests/test_asyncfilters.py b/tests/test_asyncfilters.py
index 1b5aad8..4be25a3 100644
--- a/tests/test_asyncfilters.py
+++ b/tests/test_asyncfilters.py
@@ -1,5 +1,6 @@
import pytest
from jinja2 import Environment
+from jinja2.utils import Markup
async def make_aiter(iter):
@@ -87,3 +88,31 @@ def test_groupby_multidot(env_async, articles):
'1971[totally not]',
''
]
+
+
+@mark_dualiter('int_items', lambda: [1, 2, 3])
+def test_join(env_async, int_items):
+ tmpl = env_async.from_string('{{ items()|join("|") }}')
+ out = tmpl.render(items=int_items)
+ assert out == '1|2|3'
+
+
+@mark_dualiter('string_items', lambda: ["<foo>", Markup("<span>foo</span>")])
+def test_join(string_items):
+ env2 = Environment(autoescape=True, enable_async=True)
+ tmpl = env2.from_string(
+ '{{ ["<foo>", "<span>foo</span>"|safe]|join }}')
+ assert tmpl.render(items=string_items) == '&lt;foo&gt;<span>foo</span>'
+
+
+def make_users():
+ class User(object):
+ def __init__(self, username):
+ self.username = username
+ return map(User, ['foo', 'bar'])
+
+
+@mark_dualiter('users', make_users)
+def test_join_attribute(env_async, users):
+ tmpl = env_async.from_string('''{{ users()|join(', ', 'username') }}''')
+ assert tmpl.render(users=users) == 'foo, bar'