我有一个简单的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/