Function
zope.app.container.contained.uncontained

Signature

uncontained(object, container, name=None)

Documentation String

Clear the containment relationship between the object and the container.

If we run this using the testing framework, we'll use getEvents to track the events generated:

>>> from zope.component.eventtesting import getEvents
>>> from zope.lifecycleevent.interfaces import IObjectModifiedEvent
>>> from zope.app.container.interfaces import IObjectRemovedEvent

We'll start by creating a container with an item:

>>> class Item(Contained):
...     pass
>>> item = Item()
>>> container = {u'foo': item}
>>> x, event = containedEvent(item, container, u'foo')
>>> item.__parent__ is container
1
>>> item.__name__
u'foo'

Now we'll remove the item. It's parent and name are cleared:

>>> uncontained(item, container, u'foo')
>>> item.__parent__
>>> item.__name__

We now have a new removed event:

>>> len(getEvents(IObjectRemovedEvent))
1
>>> event = getEvents(IObjectRemovedEvent)[-1]
>>> event.object is item
1
>>> event.oldParent is container
1
>>> event.oldName
u'foo'
>>> event.newParent
>>> event.newName

As well as a modification event for the container:

>>> len(getEvents(IObjectModifiedEvent))
1
>>> getEvents(IObjectModifiedEvent)[-1].object is container
1

Now if we call uncontained again:

>>> uncontained(item, container, u'foo')

We won't get any new events, because __parent__ and __name__ are None:

>>> len(getEvents(IObjectRemovedEvent))
1
>>> len(getEvents(IObjectModifiedEvent))
1

But, if either the name or parent are not None and they are not the container and the old name, we'll get a modified event but not a removed event.

>>> item.__parent__, item.__name__ = container, None
>>> uncontained(item, container, u'foo')
>>> len(getEvents(IObjectRemovedEvent))
1
>>> len(getEvents(IObjectModifiedEvent))
2
>>> item.__parent__, item.__name__ = None, u'bar'
>>> uncontained(item, container, u'foo')
>>> len(getEvents(IObjectRemovedEvent))
1
>>> len(getEvents(IObjectModifiedEvent))
3