gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:38:35 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com