[Zope] manage_afterAdd quirks

John Eikenberry jae-zope at kavi.com
Wed Aug 24 12:49:08 EDT 2005


Philip J?genstedt wrote:

> It seems I need one of three things:
> 
> 1. A better way to add images and a template altogether.
> 2. A hook which is only called once, when the object is first created.
> 3. A method to check if the objects exist in the newly added item
> directly, not aquired.

As Peter mentioned, use aq_base. There are 2 ways to use it. One as an
object attribute (self.aq_base) or using the aq_base() function as imported
from Acquisition. The latter method won't cause an attribute error if you
already are working with the non-acquisition object (in that case it will
simply return the object. For what you are doing I recommend one of these
methods.

To get more control over when manage_afterAdd runs some code, you can set
some flags using the copy hooks. Before running manage_afterAdd when
copying/moving/renaming ObjectManager calls the method _notifyOfCopyTo() on
the object being copied/moved/renamed. It passes one argument (op) giving
some context to the call. It is set to 1 for moves and renames, and set to
0 for copies. 

So you can use this to set a flag on your object to modify
manage_afterAdd's behaviour in these circumstances. Eg.

class Container(ObjectManager):

    def _notifyOfCopyTo(self,op):
        # use a _v_ volitile attribute here to avoid persistence
        self._v_copy_flag = op

    def manage_afterAdd(self, item, container):
        if hasattr(self,'_v_copy_flag'):
            if self._v_copy_flag == 0:
                # stuff you want done when copying
                ...
            if self._v_copy_flag == 1:
                # stuff you want done when moving/renaming
                ...
            # clear the flag
            delattr(self,'_v_copy_flag')
        else:
            # stuff you want done only on initial install
            ...
        # stuff you want done no matter what.
        .,.

Of course you can simplify this if you don't care if its been moved,
renamed or copied. Just wanted to show the different possibilities.

-- 

John Eikenberry [jae at kavi.com]
______________________________________________________________
"A society that will trade a little liberty for a little order
 will deserve neither and lose both."
                                          --B. Franklin


More information about the Zope mailing list