仅作记录;我今天找到了另一个用例,我修补了Products.DCWorkflow作为概念验证:
配置.zcml
<configure
xmlns="http://namespaces.zope.org/zope"
xmlns:monkey="http://namespaces.plone.org/monkey">
<monkey:patch
description="Allow aborting workflow transitions"
class="Products.DCWorkflow.DCWorkflow.DCWorkflowDefinition"
original="doActionFor"
replacement=".patches.doActionFor"
/>
<subscriber
for="Products.DCWorkflow.interfaces.IBeforeTransitionEvent"
handler=".subscribers.validate_workflow_transition"
/>
</configure>
订阅者.py
def validate_workflow_transition(event):
if not check_something():
raise MyException
补丁.py
def doActionFor(self, ob, action, comment='', **kw):
...
# XXX: above this everything is included without any changes
# monkey patch replaces only the last line
try:
self._changeStateOf(ob, tdef, kw)
except MyException:
# do something
pass
概念验证按预期工作,但我对最终的 UI 不满意,所以我决定听从 Martijn 的建议,重新实现一切作为守卫;它将需要额外的代码来设置所涉及的所有工作流转换的保护(并在卸载时将其删除),以及一个浏览器视图和 viewlet 来显示一条消息,解释为什么转换不可用,但最后它会更干净。