Exemplo n.º 1
0
def test_template_not_found(loop):

    app = web.Application(loop=loop)
    aiohttp_mako.setup(app, input_encoding='utf-8',
                       output_encoding='utf-8',
                       default_filters=['decode.utf8'])

    req = make_mocked_request('GET', '/', app=app)

    with pytest.raises(web.HTTPInternalServerError) as ctx:
        aiohttp_mako.render_template('template', req, {})

    assert "Template 'template' not found" == ctx.value.text
Exemplo n.º 2
0
def render_template(name: str,
                    req: web.Request,
                    context: Dict[str, Any],
                    content_type: str = "text/html") -> web.Response:
    resp = typing.cast(web.Response,
                       aiohttp_mako.render_template(name, req, context))
    resp.content_type = content_type
    return resp
Exemplo n.º 3
0
 def func(request):
     return aiohttp_mako.render_template('tplt.html', request, {
         'head': 'HEAD',
         'text': 'text'
     })
Exemplo n.º 4
0
 def func(request):
     return aiohttp_mako.render_template('template', request, {})
    def toolbar_middleware(request):

        # request['exc_history'] = exc_history
        panel_classes = settings.get('panels', [])
        global_panel_classes = settings.get('global_panels', [])
        hosts = settings.get('hosts', [])

        show_on_exc_only = settings.get('show_on_exc_only')
        intercept_redirects = settings['intercept_redirects']

        root_url = request.app.router['debugtoolbar.main'].url()
        exclude_prefixes = settings.get('exclude_prefixes')
        exclude = [root_url] + exclude_prefixes

        p = request.path
        starts_with_excluded = list(filter(None, map(p.startswith, exclude)))

        remote_host, remote_port = request.transport.get_extra_info('peername')

        last_proxy_addr = remote_host

        # TODO: rethink access policy by host
        if starts_with_excluded or not addr_in(last_proxy_addr, hosts):
            return (yield from handler(request))

        toolbar = DebugToolbar(request, panel_classes, global_panel_classes)
        _handler = handler

        # XXX
        for panel in toolbar.panels:
            _handler = panel.wrap_handler(_handler)

        try:
            response = yield from _handler(request)
        except (web.HTTPSuccessful, web.HTTPRedirection,
                web.HTTPClientError) as e:
            # TODO: fix dirty hack
            response = e

        except Exception as e:
            if intercept_exc:
                tb = get_traceback(info=sys.exc_info(),
                                   skip=1,
                                   show_hidden_frames=False,
                                   ignore_system_exceptions=True,
                                   exc=e)
                for frame in tb.frames:
                    exc_history.frames[frame.id] = frame
                exc_history.tracebacks[tb.id] = tb
                request['pdbt_tb'] = tb

                # TODO: find out how to port following to aiohttp
                # or just remove it
                # token = request.app[APP_KEY]['pdtb_token']
                # qs = {'token': token, 'tb': str(tb.id)}
                # msg = 'Exception at %s\ntraceback url: %s'
                #
                # exc_url = request.app.router['debugtoolbar.exception']\
                #     .url(query=qs)
                # assert exc_url, msg
                # exc_msg = msg % (request.path, exc_url)
                # logger.exception(exc_msg)

                # subenviron = request.environ.copy()
                # del subenviron['PATH_INFO']
                # del subenviron['QUERY_STRING']
                # subrequest = type(request).blank(exc_url, subenviron)
                # subrequest.script_name = request.script_name
                # subrequest.path_info = \
                #     subrequest.path_info[len(request.script_name):]
                #
                # response = request.invoke_subrequest(subrequest)
                body = tb.render_full(request).encode('utf-8', 'replace')
                response = web.Response(body=body, status=500)

                yield from toolbar.process_response(request, response)

                request['id'] = str((id(request)))
                toolbar.status = response.status

                request_history.put(request['id'], toolbar)
                toolbar.inject(request, response)
                return response
            else:
                # logger.exception('Exception at %s' % request.path)
                raise e
        toolbar.status = response.status
        if intercept_redirects:
            # Intercept http redirect codes and display an html page with a
            # link to the target.
            if response.status in REDIRECT_CODES and response.location:

                context = {'redirect_to': response.location,
                           'redirect_code': response.status}

                _response = aiohttp_mako.render_template(
                    'redirect.dbtmako', request, context,
                    app_key=TEMPLATE_KEY)
                response = _response

        yield from toolbar.process_response(request, response)
        request['id'] = hexlify(id(request))

        # Don't store the favicon.ico request
        # it's requested by the browser automatically
        if not "/favicon.ico" == request.path:
            request_history.put(request['id'], toolbar)

        if not show_on_exc_only and response.content_type in HTML_TYPES:
            toolbar.inject(request, response)

        return response
 def func(request):
     return aiohttp_mako.render_template(
         'tplt.html', request,
         {'head': 'HEAD', 'text': 'text'})
Exemplo n.º 7
0
 def func(request):
     return aiohttp_mako.render_template('template', request, {})