def mako_call_template_hook(*name, **kwargs): from indico.web.flask.templating import call_template_hook kwargs = { k: unicode(v, 'utf-8') if isinstance(v, str) else v for k, v in kwargs.iteritems() } result = call_template_hook(*name, **kwargs) return result.encode('utf-8')
def test_template_hooks_yielding(): def _make_tpl_hook(name='', yielding=False): if yielding: def _hook(): yield f'test{name}@{current_plugin.name}' if current_plugin else f'test{name}' yield f'test{name}@{current_plugin.name}' if current_plugin else f'test{name}' return _hook else: return lambda: f'test{name}@{current_plugin.name}' if current_plugin else f'test{name}' # single receiver with _register_template_hook_cleanup('test-hook', _make_tpl_hook(yielding=True)): assert call_template_hook('test-hook') == 'test\ntest' # single receiver - plugin with _register_template_hook_cleanup('test-hook', _make_tpl_hook(yielding=True), plugin=_DummyPlugin()): assert call_template_hook('test-hook') == 'test@dummy\ntest@dummy' # multiple receivers with ( _register_template_hook_cleanup('test-hook', _make_tpl_hook(1)), _register_template_hook_cleanup('test-hook', _make_tpl_hook(2, yielding=True)), _register_template_hook_cleanup('test-hook', _make_tpl_hook(3, yielding=True)), ): assert call_template_hook( 'test-hook') == 'test1\ntest2\ntest2\ntest3\ntest3' assert call_template_hook('test-hook', as_list=True) == [ 'test1', 'test2', 'test2', 'test3', 'test3' ] # multiple receivers - plugin with ( _register_template_hook_cleanup('test-hook', _make_tpl_hook(1), plugin=_DummyPlugin()), _register_template_hook_cleanup('test-hook', _make_tpl_hook(2, yielding=True), plugin=_DummyPlugin()), _register_template_hook_cleanup('test-hook', _make_tpl_hook(3)), _register_template_hook_cleanup('test-hook', _make_tpl_hook(4, yielding=True)), ): assert call_template_hook( 'test-hook' ) == 'test1@dummy\ntest2@dummy\ntest2@dummy\ntest3\ntest4\ntest4' assert call_template_hook('test-hook', as_list=True) == [ 'test1@dummy', 'test2@dummy', 'test2@dummy', 'test3', 'test4', 'test4' ]
def test_template_hooks(): def _make_tpl_hook(name=''): return lambda: f'test{name}@{current_plugin.name}' if current_plugin else f'test{name}' # single receiver with _register_template_hook_cleanup('test-hook', _make_tpl_hook()): assert call_template_hook('test-hook') == 'test' # single receiver - plugin with _register_template_hook_cleanup('test-hook', _make_tpl_hook(), plugin=_DummyPlugin()): assert call_template_hook('test-hook') == 'test@dummy' # multiple receivers with ( _register_template_hook_cleanup('test-hook', _make_tpl_hook(1)), _register_template_hook_cleanup('test-hook', _make_tpl_hook(2)), ): assert call_template_hook('test-hook') == 'test1\ntest2' assert call_template_hook('test-hook', as_list=True) == ['test1', 'test2'] # multiple receivers - plugin with ( _register_template_hook_cleanup('test-hook', _make_tpl_hook(1), plugin=_DummyPlugin()), _register_template_hook_cleanup('test-hook', _make_tpl_hook(2), plugin=_DummyPlugin()), _register_template_hook_cleanup('test-hook', _make_tpl_hook(3)), ): assert call_template_hook( 'test-hook') == 'test1@dummy\ntest2@dummy\ntest3' assert call_template_hook('test-hook', as_list=True) == [ 'test1@dummy', 'test2@dummy', 'test3' ] # custom priority with ( _register_template_hook_cleanup('test-hook', _make_tpl_hook(1), plugin=_DummyPlugin(), priority=60), _register_template_hook_cleanup('test-hook', _make_tpl_hook(2)), _register_template_hook_cleanup('test-hook', _make_tpl_hook(3), priority=30), ): assert call_template_hook('test-hook') == 'test3\ntest2\ntest1@dummy'
def test_template_hooks_markup(): def _make_tpl_hook(name=''): return lambda: (f'&test{name}@{current_plugin.name}' if current_plugin else f'&test{name}') with ( _register_template_hook_cleanup('test-hook', _make_tpl_hook(1)), _register_template_hook_cleanup('test-hook', _make_tpl_hook(2), markup=False), _register_template_hook_cleanup('test-hook', _make_tpl_hook(3), plugin=_DummyPlugin()), _register_template_hook_cleanup('test-hook', _make_tpl_hook(4), plugin=_DummyPlugin(), markup=False), ): rv = call_template_hook('test-hook') assert isinstance(rv, Markup) assert rv == ('&test1\n&test2\n&test3@dummy\n&test4@dummy')
def mako_call_template_hook(*name, **kwargs): from indico.web.flask.templating import call_template_hook kwargs = {k: unicode(v, "utf-8") if isinstance(v, str) else v for k, v in kwargs.iteritems()} result = call_template_hook(*name, **kwargs) return result.encode("utf-8")
def test_template_hooks_snippets(): def _make_tpl_hook(name='', yielding=False, snippet=None): if yielding: def _hook(): text = f'&test{name}@{current_plugin.name}' if current_plugin else f'&test{name}' if snippet: yield TemplateSnippet(text, **snippet) yield TemplateSnippet(text, **snippet) else: yield text yield text return _hook elif snippet: return lambda: TemplateSnippet( f'&test{name}@{current_plugin.name}' if current_plugin else f'&test{name}', **snippet) else: return lambda: f'&test{name}@{current_plugin.name}' if current_plugin else f'&test{name}' with ( _register_template_hook_cleanup('test-hook', _make_tpl_hook(1)), _register_template_hook_cleanup( 'test-hook', _make_tpl_hook(2, snippet={ 'markup': False, 'priority': 60 })), _register_template_hook_cleanup( 'test-hook', _make_tpl_hook(3, snippet={ 'markup': False, 'priority': 30 }, yielding=True)), _register_template_hook_cleanup('test-hook', _make_tpl_hook( 4, snippet={'priority': 70}), plugin=_DummyPlugin()), _register_template_hook_cleanup('test-hook', _make_tpl_hook( 5, snippet={'priority': 10}, yielding=True), plugin=_DummyPlugin()), ): assert call_template_hook('test-hook') == '\n'.join([ '&test5@dummy', '&test5@dummy', '&test3', '&test3', '&test1', '&test2', '&test4@dummy', ]) assert call_template_hook('test-hook', as_list=True) == [ Markup('&test5@dummy'), Markup('&test5@dummy'), '&test3', '&test3', Markup('&test1'), '&test2', Markup('&test4@dummy'), ]