## Script (Python) "ts_get_containing_space"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=obj=None
##title=Get the first TeamSpace from the acquisition tree, if exists

# climbs the acquisition tree and returns the first TeamSpace object
# found, or None if one isn't found.  can take an obj parameter, or will
# use context.  starts w/ the current object, not the parent; if a
# TeamSpace is passed in, it will be returned

if obj is None:
    obj = context

portal = context.portal_url.getPortalObject()
cmp = obj
space = None
while not cmp is portal:
    # XXX not the best check in the world
    if hasattr(cmp.aq_explicit, 'getField') and cmp.getField('space_teams'):
        space = cmp # found one
        break
    else:
        cmp = cmp.aq_inner.aq_parent

return space
