Plone:对删除对象做出反应 [英] Plone: reacting to object removal

查看:87
本文介绍了Plone:对删除对象做出反应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在删除其中的一个项目后重定向到该容器的父项.为此,我尝试了订阅zope.lifecycleevent's IObjectRemovedEvent:

I'm wanting to redirect to a container's parent after deleting an item within it. To this end, I've tried subscribing to zope.lifecycleevent's IObjectRemovedEvent:

@grok.subscribe(ISite, IObjectRemovedEvent)
def redirect_to_trial_on_delete(obj, event):
    request = getattr(obj, 'REQUEST', None)
    if request:
        trial_url = obj.aq_parent.aq_parent.absolute_url()
        request.response.redirect(trial_url)

删除是通过单击container/id/delete_confirmation触发的,但是这会触发比我预期更多的事件.我已订阅的函数被调用两次:一次是在单击链接时,一次是在确认删除时,再一次.更令人困惑的是,如果我取消删除操作,它也会被 调用.我希望仅在知道从容器中删除一个对象的情况下引发该事件.

Deletion is triggered by clicking on container/id/delete_confirmation however this fires more events than I was expecting. My subscribed function is called twice: once when I click on the link, then again when I confirm the deletion. Even more confusing, it's also called if I cancel the deletion. I was expecting the event to only be raised if an object was, y'know, removed from a container.

所有三种情况下,事件对象都是相同的,并且对oldName,oldParent等具有相同的属性值.

In all three cases the event object is the same, with the same property values for oldName, oldParent etc.

如何区分要求删除项目,取消该请求和实际删除项目?

How can I distinguish between asking to delete an item, cancelling that request, and actually deleting an item?

更新:因此似乎调用了初始事件,因为已从容器中删除了该对象以检查链接的完整性,这时会发生回滚.

Update: so it seems the initial event is called because the object is removed from the container in order to check link integrity, at which point there's a rollback.

推荐答案

同事提出了一个可行的解决方案:

A co-worker came up with a working solution:

import transaction

def redirect_to_trial(trans, obj=None, parent=None):
    if obj.id not in parent:
        request = getattr(obj, 'REQUEST', None)
        if request:
            trial_url = obj.__parent__.__parent__.absolute_url()
            request.response.redirect(trial_url)

@grok.subscribe(ISite, IObjectRemovedEvent)
def on_site_delete(obj, event):
    kwargs = dict(
        obj = obj,
        parent = event.oldParent,
    )
    transaction.get().addAfterCommitHook(redirect_to_trial, kws=kwargs)

这将在提交之后进行检查,以确保在执行重定向之前确实已删除了对象.

This checks after the commit to ensure the object actually has been removed, before performing the redirection.

不过,对这是否是一种合适的方法进行了一些确认.

Some confirmation of whether this is a suitable approach would be appreciated, though.

这篇关于Plone:对删除对象做出反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆