[Zope3-Users] Re: NotYet error when creating content during container creation

Tom Dossis td at yoma.com.au
Mon Feb 26 20:42:20 EST 2007


> 
> Yes, this is what I'd recommend. A handler for an ObjectAddedEvent will
> always see the object already as part of the object hierarchy (which is,
> after all, what ObjectAddedEvent is about).
> 

I found myself doing this quite often.
I use a decorator to make the code read nicer..


class MyContainer(Container):

    @callafteradd
    def setup(self)
        self['subobject'] = SomeObject()
        self['subobject2'] = AnotherObject()
        ...


def callafteradd(method):
    """Decorator which invokes a method after an instance
    has been added to a container.
    """
    caller_locals = sys._getframe(1).f_locals
    if caller_locals.get('__callafteradd__') is None:
        caller_locals['__callafteradd__'] = ()
    caller_locals['__callafteradd__'] += (method,)
    return method


@component.adapter(IObjectAddedEvent)
def ObjectAddedHandler(event):
    callafteradd_methods = getattr(ob, '__callafteradd__', ())
    for f in callafteradd_methods:
        f(ob)



More information about the Zope3-users mailing list