python - 如何以编程方式创建像 z3c.form 那样的详细事件?

标签 python testing event-handling plone z3c.form

我有一个简单的event handler查找实际更改的内容(它是为 IObjectModifiedEvent 事件注册的),代码如下所示:

def on_change_do_something(obj, event):
    modified = False
    # check if the publication has changed
    for change in event.descriptions:
        if change.interface == IPublication:
            modified = True
            break

    if modified:
        # do something

所以我的问题是:如何以编程方式生成这些描述?我到处都在使用 plone.app.dexterity,所以 z3c.form 在使用表单时会自动执行此操作,但我想用单元测试来测试它。

最佳答案

event.description 名义上是一个 IModificationDescription 对象,它本质上是一个 IAttributes 对象列表:每个 Attributes 对象都有一个已修改的接口(interface)(例如架构)和属性(例如字段名称列表)。

最简单的解决方案是为每个更改的字段创建一个 zope.lifecycleevent.Attributes 对象,并将其作为参数传递给事件构造函数 - 示例:

# imports elided...

changelog = [
    Attributes(IFoo, 'some_fieldname_here'),
    Attributes(IMyBehaviorHere, 'some_behavior_provided_fieldname_here',
    ]
notify(ObjectModifiedEvent(context, *changelog)

关于python - 如何以编程方式创建像 z3c.form 那样的详细事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35475391/

相关文章:

python - 我可以收到有关我的 GAE 应用程序错误的电子邮件通知吗?

python - VTK:使用 vtkDelaunay2D 创建多边形和孔不起作用

testing - Jenkins 国际化(I18N)测试工具/插件?

c# - 如何判断哪个 TestCaseData 失败(在 TestCaseSource 代码中)

java - 将 UI 与事件处理程序分离

python - 在 Python 中访问 void 指针(使用 SWIG 或其他东西)

python - key 错误 : <class 'pandas._libs.tslibs.timestamps.Timestamp' > when saving dataframe to excel

Node.js Mocha Sequelize Error 连接管理器关闭后调用ConnectionManager.getConnection

javascript - 为什么这个事件处理程序使用 "e = e || event"?

asp.net - 如何创建包含图像和文本的链接?