Plone: reacting to object removal

前端 未结 4 1461
小蘑菇
小蘑菇 2021-01-06 06:56

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<

4条回答
  •  一生所求
    2021-01-06 07:17

    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.

提交回复
热议问题