Plone: reacting to object removal

前端 未结 4 1564
小蘑菇
小蘑菇 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:23

    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.

提交回复
热议问题