Python aiohttp_jinja2.template() Examples
The following are 25
code examples of aiohttp_jinja2.template().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
aiohttp_jinja2
, or try the search function
.
Example #1
Source File: test_simple_renderer.py From aiohttp-jinja2 with Apache License 2.0 | 6 votes |
def test_render_class_based_view(aiohttp_client): class MyView(web.View): @aiohttp_jinja2.template('tmpl.jinja2') async def get(self): return {'head': 'HEAD', 'text': 'text'} template = '<html><body><h1>{{head}}</h1>{{text}}</body></html>' app = web.Application() aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({ 'tmpl.jinja2': template })) app.router.add_route('*', '/', MyView) client = await aiohttp_client(app) resp = await client.get('/') assert 200 == resp.status txt = await resp.text() assert '<html><body><h1>HEAD</h1>text</body></html>' == txt
Example #2
Source File: test_simple_renderer.py From aiohttp-jinja2 with Apache License 2.0 | 6 votes |
def test_render_can_disable_autoescape(aiohttp_client): @aiohttp_jinja2.template('tmpl.jinja2') async def func(request): return {'text': '<script>alert(1)</script>'} app = web.Application() aiohttp_jinja2.setup(app, loader=jinja2.DictLoader( {'tmpl.jinja2': '<html>{{text}}</html>'}), autoescape=False) app.router.add_route('GET', '/', func) client = await aiohttp_client(app) resp = await client.get('/') assert 200 == resp.status txt = await resp.text() assert '<html><script>alert(1)</script></html>' == txt
Example #3
Source File: test_simple_renderer.py From aiohttp-jinja2 with Apache License 2.0 | 6 votes |
def test_render_default_is_autoescaped(aiohttp_client): @aiohttp_jinja2.template('tmpl.jinja2') async def func(request): return {'text': '<script>alert(1)</script>'} app = web.Application() aiohttp_jinja2.setup(app, loader=jinja2.DictLoader( {'tmpl.jinja2': '<html>{{text}}</html>'})) app.router.add_route('GET', '/', func) client = await aiohttp_client(app) resp = await client.get('/') assert 200 == resp.status txt = await resp.text() assert '<html><script>alert(1)</script></html>' == txt
Example #4
Source File: test_simple_renderer.py From aiohttp-jinja2 with Apache License 2.0 | 6 votes |
def test_render_without_context(aiohttp_client): @aiohttp_jinja2.template('tmpl.jinja2') async def func(request): pass template = '<html><body><p>{{text}}</p></body></html>' app = web.Application() aiohttp_jinja2.setup(app, loader=jinja2.DictLoader( {'tmpl.jinja2': template})) app.router.add_route('GET', '/', func) client = await aiohttp_client(app) resp = await client.get('/') assert 200 == resp.status txt = await resp.text() assert '<html><body><p></p></body></html>' == txt
Example #5
Source File: test_simple_renderer.py From aiohttp-jinja2 with Apache License 2.0 | 6 votes |
def test_render_not_mapping(): @aiohttp_jinja2.template('tmpl.jinja2') async def func(request): return 123 app = web.Application() aiohttp_jinja2.setup(app, loader=jinja2.DictLoader( {'tmpl.jinja2': 'tmpl'})) app.router.add_route('GET', '/', func) req = make_mocked_request('GET', '/', app=app) msg = "context should be mapping, not <class 'int'>" with pytest.raises(web.HTTPInternalServerError) as ctx: await func(req) assert msg == ctx.value.text
Example #6
Source File: test_simple_renderer.py From aiohttp-jinja2 with Apache License 2.0 | 6 votes |
def test_render_template_custom_status(aiohttp_client): async def func(request): return aiohttp_jinja2.render_template( 'tmpl.jinja2', request, {'head': 'HEAD', 'text': 'text'}, status=404) template = '<html><body><h1>{{head}}</h1>{{text}}</body></html>' app = web.Application() aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({ 'tmpl.jinja2': template })) app.router.add_route('*', '/', func) client = await aiohttp_client(app) resp = await client.get('/') assert 404 == resp.status txt = await resp.text() assert '<html><body><h1>HEAD</h1>text</body></html>' == txt
Example #7
Source File: test_simple_renderer.py From aiohttp-jinja2 with Apache License 2.0 | 6 votes |
def test_render_template(aiohttp_client): async def func(request): return aiohttp_jinja2.render_template( 'tmpl.jinja2', request, {'head': 'HEAD', 'text': 'text'}) template = '<html><body><h1>{{head}}</h1>{{text}}</body></html>' app = web.Application() aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({ 'tmpl.jinja2': template })) app.router.add_route('*', '/', func) client = await aiohttp_client(app) resp = await client.get('/') assert 200 == resp.status txt = await resp.text() assert '<html><body><h1>HEAD</h1>text</body></html>' == txt
Example #8
Source File: test_simple_renderer.py From aiohttp-jinja2 with Apache License 2.0 | 6 votes |
def test_set_status(aiohttp_client): @aiohttp_jinja2.template('tmpl.jinja2', status=201) async def func(request): return {'head': 'HEAD', 'text': 'text'} template = '<html><body><h1>{{head}}</h1>{{text}}</body></html>' app = web.Application() aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({ 'tmpl.jinja2': template })) app.router.add_route('*', '/', func) client = await aiohttp_client(app) resp = await client.get('/') assert 201 == resp.status txt = await resp.text() assert '<html><body><h1>HEAD</h1>text</body></html>' == txt
Example #9
Source File: test_simple_renderer.py From aiohttp-jinja2 with Apache License 2.0 | 6 votes |
def test_render_not_initialized(): async def func(request): return aiohttp_jinja2.render_template('template', request, {}) app = web.Application() app.router.add_route('GET', '/', func) req = make_mocked_request('GET', '/', app=app) msg = "Template engine is not initialized, " \ "call aiohttp_jinja2.setup(..., app_key={}" \ ") first".format(aiohttp_jinja2.APP_KEY) with pytest.raises(web.HTTPInternalServerError) as ctx: await func(req) assert msg == ctx.value.text
Example #10
Source File: test_simple_renderer.py From aiohttp-jinja2 with Apache License 2.0 | 6 votes |
def test_convert_func_to_coroutine(aiohttp_client): @aiohttp_jinja2.template('tmpl.jinja2') async def func(request): return {'head': 'HEAD', 'text': 'text'} template = '<html><body><h1>{{head}}</h1>{{text}}</body></html>' app = web.Application() aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({ 'tmpl.jinja2': template })) app.router.add_route('*', '/', func) client = await aiohttp_client(app) resp = await client.get('/') txt = await resp.text() assert '<html><body><h1>HEAD</h1>text</body></html>' == txt
Example #11
Source File: test_simple_renderer.py From aiohttp-jinja2 with Apache License 2.0 | 6 votes |
def test_func(aiohttp_client): @aiohttp_jinja2.template('tmpl.jinja2') async def func(request): return {'head': 'HEAD', 'text': 'text'} template = '<html><body><h1>{{head}}</h1>{{text}}</body></html>' app = web.Application() aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({ 'tmpl.jinja2': template })) app.router.add_route('*', '/', func) client = await aiohttp_client(app) resp = await client.get('/') assert 200 == resp.status txt = await resp.text() assert '<html><body><h1>HEAD</h1>text</body></html>' == txt
Example #12
Source File: test_jinja_globals.py From aiohttp-jinja2 with Apache License 2.0 | 6 votes |
def test_static(aiohttp_client): @aiohttp_jinja2.template('tmpl.jinja2') async def index(request): return {} app = web.Application() aiohttp_jinja2.setup(app, loader=jinja2.DictLoader( {'tmpl.jinja2': "{{ static('whatever.js') }}"})) app['static_root_url'] = '/static' app.router.add_route('GET', '/', index) client = await aiohttp_client(app) resp = await client.get('/') assert 200 == resp.status txt = await resp.text() assert '/static/whatever.js' == txt
Example #13
Source File: test_jinja_globals.py From aiohttp-jinja2 with Apache License 2.0 | 6 votes |
def test_url_int_param(aiohttp_client): @aiohttp_jinja2.template('tmpl.jinja2') async def index(request): return {} async def other(request): return app = web.Application() aiohttp_jinja2.setup(app, loader=jinja2.DictLoader( {'tmpl.jinja2': "{{ url('other', arg=1)}}"})) app.router.add_route('GET', '/', index) app.router.add_route('GET', '/uid/{arg}', other, name='other') client = await aiohttp_client(app) resp = await client.get('/') assert 200 == resp.status txt = await resp.text() assert '/uid/1' == txt
Example #14
Source File: test_jinja_globals.py From aiohttp-jinja2 with Apache License 2.0 | 6 votes |
def test_url_with_query(aiohttp_client): @aiohttp_jinja2.template('tmpl.jinja2') async def index(request): return {} app = web.Application() aiohttp_jinja2.setup(app, loader=jinja2.DictLoader( {'tmpl.jinja2': "{{ url('index', query_={'foo': 'bar'})}}"})) app.router.add_get('/', index, name='index') client = await aiohttp_client(app) resp = await client.get('/') assert 200 == resp.status txt = await resp.text() assert '/?foo=bar' == txt
Example #15
Source File: test_jinja_filters.py From aiohttp-jinja2 with Apache License 2.0 | 6 votes |
def test_jinja_filters(aiohttp_client): @aiohttp_jinja2.template('tmpl.jinja2') async def index(request): return {} def add_2(value): return value + 2 app = web.Application() aiohttp_jinja2.setup( app, loader=jinja2.DictLoader({'tmpl.jinja2': "{{ 5|add_2 }}"}), filters={'add_2': add_2} ) app.router.add_route('GET', '/', index) client = await aiohttp_client(app) resp = await client.get('/') assert 200 == resp.status txt = await resp.text() assert '7' == txt
Example #16
Source File: views.py From aiohttp-devtools with MIT License | 5 votes |
def index(request): """ This is the view handler for the "/" url. :param request: the request object see http://aiohttp.readthedocs.io/en/stable/web_reference.html#request :return: context for the template. """ # Note: we return a dict not a response because of the @template decorator return { 'title': request.app['settings'].name, 'intro': "Success! you've setup a basic aiohttp app.", }
Example #17
Source File: views.py From bioconda-utils with MIT License | 5 votes |
def show_index(_request): """View for the Bot's home page. Renders nothing special at the moment, just the template. """ return {}
Example #18
Source File: graphiql.py From graphql-over-kafka with MIT License | 5 votes |
def get(self): # write the template to the client return {}
Example #19
Source File: test_context_processors.py From aiohttp-jinja2 with Apache License 2.0 | 5 votes |
def test_context_processors(aiohttp_client): @aiohttp_jinja2.template('tmpl.jinja2') async def func(request): return {'bar': 2} app = web.Application(middlewares=[ aiohttp_jinja2.context_processors_middleware]) aiohttp_jinja2.setup(app, loader=jinja2.DictLoader( {'tmpl.jinja2': 'foo: {{ foo }}, bar: {{ bar }}, path: {{ request.path }}'})) async def processor(request): return {'foo': 1, 'bar': 'should be overwriten'} app['aiohttp_jinja2_context_processors'] = ( aiohttp_jinja2.request_processor, processor, ) app.router.add_get('/', func) client = await aiohttp_client(app) resp = await client.get('/') assert 200 == resp.status txt = await resp.text() assert 'foo: 1, bar: 2, path: /' == txt
Example #20
Source File: test_simple_renderer.py From aiohttp-jinja2 with Apache License 2.0 | 5 votes |
def test_render_bare_funcs_deprecated(aiohttp_client): def wrapper(func): async def wrapped(request): with pytest.warns(DeprecationWarning, match='Bare functions are deprecated'): return await func(request) return wrapped @wrapper @aiohttp_jinja2.template('tmpl.jinja2') def func(request): return {'text': 'OK'} app = web.Application() aiohttp_jinja2.setup(app, loader=jinja2.DictLoader( {'tmpl.jinja2': '{{text}}'})) app.router.add_route('GET', '/', func) client = await aiohttp_client(app) resp = await client.get('/') assert 200 == resp.status txt = await resp.text() assert 'OK' == txt
Example #21
Source File: test_context_processors.py From aiohttp-jinja2 with Apache License 2.0 | 5 votes |
def test_context_not_tainted(aiohttp_client): global_context = {'version': 1} @aiohttp_jinja2.template('tmpl.jinja2') async def func(request): return global_context async def processor(request): return {'foo': 1} app = web.Application() aiohttp_jinja2.setup( app, loader=jinja2.DictLoader({'tmpl.jinja2': 'foo: {{ foo }}'}), context_processors=[processor]) app.router.add_get('/', func) client = await aiohttp_client(app) resp = await client.get('/') assert 200 == resp.status txt = await resp.text() assert 'foo: 1' == txt assert 'foo' not in global_context
Example #22
Source File: test_context_processors.py From aiohttp-jinja2 with Apache License 2.0 | 5 votes |
def test_context_processors_new_setup_style(aiohttp_client): @aiohttp_jinja2.template('tmpl.jinja2') async def func(request): return {'bar': 2} async def processor(request): return {'foo': 1, 'bar': 'should be overwriten'} app = web.Application() aiohttp_jinja2.setup( app, loader=jinja2.DictLoader( {'tmpl.jinja2': 'foo: {{ foo }}, bar: {{ bar }}, ' 'path: {{ request.path }}'}), context_processors=(aiohttp_jinja2.request_processor, processor) ) app.router.add_route('GET', '/', func) client = await aiohttp_client(app) resp = await client.get('/') assert 200 == resp.status txt = await resp.text() assert 'foo: 1, bar: 2, path: /' == txt
Example #23
Source File: test_context_processors.py From aiohttp-jinja2 with Apache License 2.0 | 5 votes |
def test_context_is_response(aiohttp_client): @aiohttp_jinja2.template('tmpl.jinja2') async def func(request): raise web.HTTPForbidden() app = web.Application() aiohttp_jinja2.setup(app, loader=jinja2.DictLoader( {'tmpl.jinja2': "template"})) app.router.add_route('GET', '/', func) client = await aiohttp_client(app) resp = await client.get('/') assert 403 == resp.status
Example #24
Source File: app.py From proxy_py with GNU General Public License v3.0 | 4 votes |
def get_response_wrapper(template_name): def decorator_wrapper(func): @functools.wraps(func) @aiohttp_jinja2.template(template_name) async def wrap(self, *args, **kwargs): good_proxies_count = await db.count( Proxy.select().where(Proxy.number_of_bad_checks == 0) ) bad_proxies_count = await db.count( Proxy.select().where( Proxy.number_of_bad_checks > 0, Proxy.number_of_bad_checks < settings.DEAD_PROXY_THRESHOLD, ) ) dead_proxies_count = await db.count( Proxy.select().where( Proxy.number_of_bad_checks >= settings.DEAD_PROXY_THRESHOLD, Proxy.number_of_bad_checks < settings.DO_NOT_CHECK_ON_N_BAD_CHECKS, ) ) not_checked_proxies_count = await db.count( Proxy.select().where( Proxy.number_of_bad_checks >= settings.DO_NOT_CHECK_ON_N_BAD_CHECKS, ) ) response = { "bad_proxies_count": bad_proxies_count, "good_proxies_count": good_proxies_count, "dead_proxies_count": dead_proxies_count, "not_checked_proxies_count": not_checked_proxies_count, } response.update(await func(self, *args, **kwargs)) return response return wrap return decorator_wrapper
Example #25
Source File: test_context_processors.py From aiohttp-jinja2 with Apache License 2.0 | 4 votes |
def test_nested_context_processors(aiohttp_client): @aiohttp_jinja2.template('tmpl.jinja2') async def func(request): return {'bar': 2} subapp = web.Application(middlewares=[ aiohttp_jinja2.context_processors_middleware]) aiohttp_jinja2.setup(subapp, loader=jinja2.DictLoader( {'tmpl.jinja2': 'foo: {{ foo }}, bar: {{ bar }}, ' 'baz: {{ baz }}, path: {{ request.path }}'})) async def subprocessor(request): return {'foo': 1, 'bar': 'should be overwriten'} subapp['aiohttp_jinja2_context_processors'] = ( aiohttp_jinja2.request_processor, subprocessor, ) subapp.router.add_get('/', func) app = web.Application(middlewares=[ aiohttp_jinja2.context_processors_middleware]) aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({})) async def processor(request): return {'baz': 5} app['aiohttp_jinja2_context_processors'] = ( aiohttp_jinja2.request_processor, processor, ) app.add_subapp('/sub/', subapp) client = await aiohttp_client(app) resp = await client.get('/sub/') assert 200 == resp.status txt = await resp.text() assert 'foo: 1, bar: 2, baz: 5, path: /sub/' == txt