Exemplo n.º 1
0
def connectionOfPersistent(obj):
    """ zope2 cxn fetcher for wrapped items """
    if not (IAcquirer.providedBy(obj) or hasattr(obj, '__parent__')):
        return getattr(obj, '_p_jar', None)

    for parent in aq_iter(obj):
        conn = getattr(parent, '_p_jar', None)
        if conn is not None:
            return conn
Exemplo n.º 2
0
    def __init__(self, wrapped_obj):

        # Detect a circular containment / an infinite Loop with aq_iter
        try:
            [obj for obj in aq_iter(wrapped_obj, error=RuntimeError)]
        except RuntimeError:
            raise NotYet(wrapped_obj)

        # make sure our object is wrapped by containment only
        try:
            self.path = '/'.join(wrapped_obj.getPhysicalPath())
        except AttributeError:
            self.path = None

        # If the path ends with /, it means the object had an empty id.
        # This means it's not yet added to the container, and so we have
        # to defer.
        if self.path is not None and self.path.endswith('/'):
            raise NotYet(wrapped_obj)
        self.object = aq_base(wrapped_obj)
        connection = IConnection(wrapped_obj, None)

        if not getattr(self.object, '_p_oid', None):
            if connection is None:
                raise NotYet(wrapped_obj)
            connection.add(self.object)

        try:
            root = get_root(wrapped_obj)
        except AttributeError:
            # If the object is unwrapped we can try to use the Site from the
            # threadlocal as our acquisition context, hopefully it's not
            # something odd.
            root = get_root(getSite())
        self.root_oid = root._p_oid
        self.root_dbname = IConnection(root).db().database_name
        self.oid = self.object._p_oid
        self.dbname = connection.db().database_name