Back to index

plone3  3.1.7
utils.py
Go to the documentation of this file.
00001 from zope.traversing.interfaces import IContainmentRoot
00002 
00003 from Acquisition import aq_parent, aq_inner
00004 
00005 
00006 def get_parent(obj):
00007     """Returns the container the object was traversed via.  This
00008     is a version of zope.traversing.api.getParent that is designed to
00009     handle acquisition as well.
00010 
00011     Returns None if the object is a containment root.
00012     Raises TypeError if the object doesn't have enough context to get the
00013     parent.
00014     """
00015     
00016     if IContainmentRoot.providedBy(obj):
00017         return None
00018     
00019     parent = getattr(obj, '__parent__', None)
00020     if parent is not None:
00021         return parent
00022 
00023     parent = aq_parent(aq_inner(obj))
00024     if parent is not None:
00025         return parent
00026 
00027     raise TypeError("Not enough context information to get parent", obj)