reporter.py 文件源码

python
阅读 25 收藏 0 点赞 0 评论 0

项目:django-cavalry 作者: valohai 项目源码 文件源码
def inject_html(request, response, data, summary_data):
    try:
        body_closing_index = response.content.rindex(b'</body>')
    except ValueError:
        return
    content = ' | '.join(
        '%s=%s' % (key, round(value, 3) if isinstance(value, float) else value)
        for (key, value)
        in sorted(summary_data.items())
    )
    ns = 'cv_%s' % get_random_string()
    html = """<style>{style}</style><div id="{ns}">{content}</div>""".format(
        ns=ns,
        content=content,
        style=STYLE.replace('#cv', '#' + ns),
    )
    script = generate_console_script(data, with_stacks=bool(request.GET.get('_cavalry_stacks')))
    html += "<script>{script}</script>".format(script='\n'.join(script))

    response.content = (
        response.content[:body_closing_index] +
        html.encode('utf-8') +
        response.content[body_closing_index:]
    )

    if 'content-length' in response:
        response['content-length'] = len(response.content)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号