在时间线中插入django数据库查询
timeline_django的Python项目详细描述
Timeline_django将操作插入到时间线中 (http://pypi.python.org/pypi/timeline/)用于django db查询和其他事件 在Django。这对于获得阻塞的整体视图非常有用 涉及单个django请求的工作和标注。
通常的模式是与python oops一起使用这个 已经为django应用程序配置了基于wsgi的oops报告。
wsgi
假设在wsgi下运行(因为mod_python已被弃用,而dev和 1.4及以上版本中的测试服务器是基于wsgi的。某些程序集可能需要 在非wsgi环境中运行。
配置timeline django需要三个步骤。
wsgi中间件
把这个放到你的wsgi应用程序中,在django应用程序定义之后的某个地方 但在应用oops_wsgi包装之前:
from timeline_django.wsgi import make_app as timeline_django_make_app from timeline.wsgi import make_app as timeline_make_app application = timeline_django_make_app(application) application = timeline_make_app(application)
第一个包装器将wsgi环境公开给在没有 请求上下文-例如django钩子,它是捕捉orm事件的地方。
第二个包装器将时间线对象注入到wsgi环境中。
连接到django事件
把它放在它只运行一次的任何地方(例如,在您的wsgi应用程序中 定义):
from timeline_django import setup setup.setup_for_requests()
修订
最后,您需要确保可能泄漏安全性的查询内容 或者修改个人信息:这可以防止会话劫持和 权限提升攻击,使非管理人员(如 开发人员查看您的时间线数据:
import oops_timeline import timeline_django.filters oops_timeline.install_hooks(oops_config) timeline_django.filters.install_hooks(oops_config)
oops_timeline钩子将时间线从wsgi环境复制到oops 报告,第二个安装django特定的编校过滤器 在复制的时间线上操作-排序很重要。电流 Timeline提供的过滤器是:
* ``session`` table to prevent session hijacking * ``user`` table to prevent password disclosure
如果您的django站点使用其他敏感表(例如 认证模块)您也应该安排对它们进行过滤。见 timeline_django.filters例如代码。
如果您没有在python oops中使用timeline,则需要安排 您正在使用的任何时间线捕获/查看系统的修订。
非wsgi环境
如果在其他环境中运行,则需要执行以下步骤 你自己。首先,您需要选择一个点来创建一个新的时间轴对象。 这需要与您希望的时间线的开始相对应 抓捕。可能在脚本的开头,也可能是对 其他一些事件。
一旦您创建了时间线,您需要将其存储在它可以存储的地方 需要时访问。可能在变量中,也可能在 如果有多个线程分别处理 时间表。一旦存储了timline,就需要一个函数 会归还的。该函数将是您的时间线工厂。应该是 没有参数并返回时间线对象,如果没有 调用时适用于时间线。
一旦你有了这个方法,你就可以为你的请求调用timeline django的设置。 方法:
from timeline_django.setup import setup_for_requests setup_for_requests(timeline_factory=timeline_factory)
其中,timeline工厂是您在上面创建的函数。这将建立 当你的时间线中有一个动作需要记录的钩子 数据库查询,或支持的其他django事件之一。
Copyright (c) 2012, Canonical Ltd
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, version 3 only.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. GNU Lesser General Public License version 3 (see the file LICENSE).