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<
Here's another possibility, again from the same genius co-worker:
from zope.interface import implements
from transaction.interfaces import ISavepointDataManager
from transaction._transaction import AbortSavepoint
import transaction
class RedirectDataManager(object):
implements(ISavepointDataManager)
def __init__(self, request, url):
self.request = request
self.url = url
# Use the default thread transaction manager.
self.transaction_manager = transaction.manager
def tpc_begin(self, transaction):
pass
def tpc_finish(self, transaction):
self.request.response.redirect(self.url)
def tpc_abort(self, transaction):
self.request.response.redirect(self.url)
def commit(self, transaction):
pass
def abort(self, transaction):
pass
def tpc_vote(self, transaction):
pass
def sortKey(self):
return id(self)
def savepoint(self):
"""
This is just here to make it possible to enter a savepoint with this manager active.
"""
return AbortSavepoint(self, transaction.get())
def redirect_to_trial(obj, event):
request = getattr(obj, 'REQUEST', None)
if request:
trial_url = obj.__parent__.__parent__.absolute_url()
transaction.get().join(RedirectDataManager(request, trial_url))
I'm now using zcml for subscription to more easily bind it to multiple content types:
This is the solution I've ended up going with, as I find it more explicit about what's happening than doing manual checks to work out if the event I've caught is the event I really want.