Example #1
0
class Root(object):
    """
    The root is a container for objects but does not store any data in the database itself. It
    is the entry point for object access. Roots are only handled by the application. 

    Requires (Container, ContainerFactory, Event)
    """
    
    def __init__(self, path, app, rootDef):
        self.__name__ = str(path)
        self.__parent__ = app
        self.id = 0 
        # unique root id generated from name . negative integer.
        self.idhash = abs(hash(self.__name__))*-1
        self.path = path
        self.configuration = rootDef
        self.queryRestraints = {}, {}

        self.meta = Conf(pool_type=rootDef["id"], 
                         title=translate(rootDef["name"]), 
                         pool_state=1, 
                         pool_filename=path, 
                         pool_wfa=u"",
                         pool_change=datetime.now(tz=app.pytimezone),
                         pool_changedby=u"",
                         pool_create=datetime.now(tz=app.pytimezone),
                         pool_createdby=u"")
        self.data = Conf()
        self.files = Conf()

        # load security
        acl = SetupRuntimeAcls(rootDef.acl, self)
        if acl:
            # omit if empty. acls will be loaded from parent object
            self.__acl__ = acl

        self.Signal("init")


    # Properties -----------------------------------------------------------

    @property
    def dataroot(self):
        """ this will return itself. Used for object compatibility. """
        return self

    @property
    def app(self):
        """ returns the cms application the root is used for """
        return self.__parent__

    @property
    def db(self):
        """ returns the datapool object """
        return self.app.db

    @property
    def parent(self):
        """ this will return None. Used for object compatibility. """
        return None

    # Object Lookup -----------------------------------------------------------

    def LookupObj(self, id, **kw):
        """
        Lookup the object referenced by id *anywhere* in the tree structure. Use obj() to 
        restrain lookup to the first sublevel only. ::
        
            id = number
            **kw = version information
        
        returns the object or None
        """
        try:
            id = long(id)
        except:
            return None
        if id <= 0:
            return self
        if not id:
            return None
            #raise Exception, "NotFound"

        # proxy object
        if kw.has_key("proxyObj") and kw["proxyObj"]:
            obj = self._GetObj(id, parentObj = kw["proxyObj"], **kw)
            if not obj:
                raise ContainmentError, "Proxy object not found"
            return obj

        # load tree structure
        path = self.app.db.GetParentPath(id)
        if path is None:
            return None
            #raise Exception, "NotFound"
        
        # check and adjust root id
        if hasattr(self, "rootID"):
            if self.rootID in path:
                path = path[path.index(self.rootID)+1:]
        if hasattr(self.app, "rootID"):
            if self.app.rootID in path:
                path = path[path.index(self.app.rootID)+1:]

        # reverse lookup of object tree. loads all parent objs.
        path.append(id)
        #opt
        obj = self
        for id in path:
            if id == self.id:
                continue
            obj = obj._GetObj(id, **kw)
            if not obj:
                return None
                #raise Exception, "NotFound"
        return obj


    def ObjQueryRestraints(self, containerObj=None, parameter=None, operators=None):
        """
        The functions returns two dictionaries (parameter, operators) used to restraint
        object lookup in subtree. For example a restraint can be set to ignore all
        objects with meta.pool_state=0. All container get (GetObj, GetObjs, ...) functions 
        use query restraints internally. 
        
        See `nive.search` for parameter and operator usage.
        
        Please note: Setting the wrong values for query restraints can easily crash 
        the application. 
        
        Event:
        - loadRestraints(parameter, operators)
        
        returns parameter dict, operators dict
        """
        p, o = self.queryRestraints
        if parameter:
            parameter.update(p)
            if operators:
                operators.update(o)
            else:
                operators = o.copy()
        else:
            parameter=p.copy()
            operators=o.copy()
        self.Signal("loadRestraints", parameter=parameter, operators=operators)
        return parameter, operators


    # Values ------------------------------------------------------

    def GetID(self):
        """ returns 0. the root id is always zero. """
        return self.id

    def GetTypeID(self):
        """ returns the root type id from configuration """
        return self.configuration.id

    def GetTypeName(self):
        """ returns the root type name from configuration """
        return self.configuration.name

    def GetFieldConf(self, fldId):
        """
        Get the FieldConf for the field with id = fldId. Looks up data, file and meta
        fields.

        returns FieldConf or None
        """
        for f in self.configuration["data"]:
            if f["id"] == fldId:
                return f
        return self.app.GetMetaFld(fldId)

    def GetTitle(self):
        """ returns the root title from configuration. """
        return self.meta.get("title","")

    def GetPath(self):
        """ returns the url path name as string. """
        return self.__name__


    # Parents ----------------------------------------------------

    def IsRoot(self):
        """ returns always True. """
        return True

    def GetParents(self):
        """ returns empty list. Used for object compatibility. """
        return []

    def GetParentIDs(self):
        """ returns empty list. Used for object compatibility. """
        return []

    def GetParentTitles(self):
        """ returns empty list. Used for object compatibility. """
        return []

    def GetParentPaths(self):
        """ returns empty list. Used for object compatibility. """
        return []


    # tools ----------------------------------------------------

    def GetTool(self, name):
        """
        Load a tool in the roots' context. Only works for tools registered for roots or this root type. ::
            
            returns the tool object or None
        
        Event
        - loadToool(tool=toolObj)
        """
        t = self.app.GetTool(name, self)
        self.Signal("loadTool", tool=t)
        return t


    def Close(self):
        """
        Close the root and all contained objects. Currently only used in combination with caches.
        
        Event
        - close()
        """
        self.Signal("close")
        if ICache.providedBy(self):
            #opt
            for o in self.GetAllFromCache():
                o.Close()
        return

    # to be removed in future versions --------------------------------------------

    def root(self):
        """
        bw 0.9.12: use dataroot property instead! 
        this will return itself. Used for object compatibility. 
        """
        return self

    def GetRoot(self):
        """bw 0.9.12: to be removed. returns self. """
        return self

    def GetApp(self):
        """bw 0.9.12: to be removed. returns the cms application. """
        return self.app

    def GetParent(self):
        """bw 0.9.12: to be removed. returns None. """
        return None
Example #2
0
class Root(object):
    """
    The root is a container for objects but does not store any data in the database itself. It
    is the entry point for object access. Roots are only handled by the application. 

    Requires (Container, ContainerFactory, Event)
    """
    def __init__(self, path, app, rootDef):
        self.__name__ = str(path)
        self.__parent__ = app
        self.id = 0
        # unique root id generated from name . negative integer.
        self.idhash = abs(hash(self.__name__)) * -1
        self.path = path
        self.configuration = rootDef
        self.queryRestraints = {}, {}

        self.meta = Conf(pool_type=rootDef["id"],
                         title=translate(rootDef["name"]),
                         pool_state=1,
                         pool_filename=path,
                         pool_wfa=u"",
                         pool_change=datetime.now(),
                         pool_changedby=u"",
                         pool_create=datetime.now(),
                         pool_createdby=u"")
        self.data = Conf()
        self.files = Conf()

        # load security
        acl = SetupRuntimeAcls(rootDef.acl, self)
        if acl:
            # omit if empty. acls will be loaded from parent object
            self.__acl__ = acl

        self.Signal("init")

    # Properties -----------------------------------------------------------

    @property
    def dataroot(self):
        """ this will return itself. Used for object compatibility. """
        return self

    @property
    def app(self):
        """ returns the cms application the root is used for """
        return self.__parent__

    @property
    def db(self):
        """ returns the datapool object """
        return self.app.db

    @property
    def parent(self):
        """ this will return None. Used for object compatibility. """
        return None

    # Object Lookup -----------------------------------------------------------

    def LookupObj(self, id, **kw):
        """
        Lookup the object referenced by id *anywhere* in the tree structure. Use obj() to 
        restrain lookup to the first sublevel only. ::
        
            id = number
            **kw = version information
        
        returns the object or None
        """
        try:
            id = long(id)
        except:
            return None
        if id <= 0:
            return self
        if not id:
            return None
            #raise Exception, "NotFound"

        # proxy object
        if kw.has_key("proxyObj") and kw["proxyObj"]:
            obj = self._GetObj(id, parentObj=kw["proxyObj"], **kw)
            if not obj:
                raise ContainmentError, "Proxy object not found"
            return obj

        # load tree structure
        path = self.app.db.GetParentPath(id)
        if path is None:
            return None
            #raise Exception, "NotFound"

        # check and adjust root id
        if hasattr(self, "rootID"):
            if self.rootID in path:
                path = path[path.index(self.rootID) + 1:]
        if hasattr(self.app, "rootID"):
            if self.app.rootID in path:
                path = path[path.index(self.app.rootID) + 1:]

        # reverse lookup of object tree. loads all parent objs.
        path.append(id)
        #opt
        obj = self
        for id in path:
            if id == self.id:
                continue
            obj = obj._GetObj(id, **kw)
            if not obj:
                return None
                #raise Exception, "NotFound"
        return obj

    def ObjQueryRestraints(self,
                           containerObj=None,
                           parameter=None,
                           operators=None):
        """
        The functions returns two dictionaries (parameter, operators) used to restraint
        object lookup in subtree. For example a restraint can be set to ignore all
        objects with meta.pool_state=0. All container get (GetObj, GetObjs, ...) functions 
        use query restraints internally. 
        
        See `nive.search` for parameter and operator usage.
        
        Please note: Setting the wrong values for query restraints can easily crash 
        the application. 
        
        Event:
        - loadRestraints(parameter, operators)
        
        returns parameter dict, operators dict
        """
        p, o = self.queryRestraints
        if parameter:
            parameter.update(p)
            if operators:
                operators.update(o)
            else:
                operators = o.copy()
        else:
            parameter = p.copy()
            operators = o.copy()
        self.Signal("loadRestraints", parameter=parameter, operators=operators)
        return parameter, operators

    # Values ------------------------------------------------------

    def GetID(self):
        """ returns 0. the root id is always zero. """
        return self.id

    def GetTypeID(self):
        """ returns the root type id from configuration """
        return self.configuration.id

    def GetTypeName(self):
        """ returns the root type name from configuration """
        return self.configuration.name

    def GetFieldConf(self, fldId):
        """
        Get the FieldConf for the field with id = fldId. Looks up data, file and meta
        fields.

        returns FieldConf or None
        """
        for f in self.configuration["data"]:
            if f["id"] == fldId:
                return f
        return self.app.GetMetaFld(fldId)

    def GetTitle(self):
        """ returns the root title from configuration. """
        return self.meta.get("title", "")

    def GetPath(self):
        """ returns the url path name as string. """
        return self.__name__

    # Parents ----------------------------------------------------

    def IsRoot(self):
        """ returns always True. """
        return True

    def GetParents(self):
        """ returns empty list. Used for object compatibility. """
        return []

    def GetParentIDs(self):
        """ returns empty list. Used for object compatibility. """
        return []

    def GetParentTitles(self):
        """ returns empty list. Used for object compatibility. """
        return []

    def GetParentPaths(self):
        """ returns empty list. Used for object compatibility. """
        return []

    # tools ----------------------------------------------------

    def GetTool(self, name):
        """
        Load a tool in the roots' context. Only works for tools registered for roots or this root type. ::
            
            returns the tool object or None
        
        Event
        - loadToool(tool=toolObj)
        """
        t = self.app.GetTool(name, self)
        self.Signal("loadTool", tool=t)
        return t

    def Close(self):
        """
        Close the root and all contained objects. Currently only used in combination with caches.
        
        Event
        - close()
        """
        self.Signal("close")
        if ICache.providedBy(self):
            #opt
            for o in self.GetAllFromCache():
                o.Close()
        return

    # to be removed in future versions --------------------------------------------

    def root(self):
        """
        bw 0.9.12: use dataroot property instead! 
        this will return itself. Used for object compatibility. 
        """
        return self

    def GetRoot(self):
        """bw 0.9.12: to be removed. returns self. """
        return self

    def GetApp(self):
        """bw 0.9.12: to be removed. returns the cms application. """
        return self.app

    def GetParent(self):
        """bw 0.9.12: to be removed. returns None. """
        return None