]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Made join filter work with async
authorArmin Ronacher <armin.ronacher@active-4.com>
Wed, 28 Dec 2016 22:49:29 +0000 (23:49 +0100)
committerArmin Ronacher <armin.ronacher@active-4.com>
Wed, 28 Dec 2016 22:49:33 +0000 (23:49 +0100)
jinja2/asyncfilters.py
tests/test_asyncfilters.py

index e879893c854334b2f002fbfc924858eeace4e4fe..c621ae41f175bf548f06971870a5c59390d11352 100644 (file)
@@ -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,
 }
index 1b5aad85870f01d6f62a6b7d1d96797455a6d3f5..4be25a3a92085387b9728b8d2681bdca66fcc271 100644 (file)
@@ -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'