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<
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.