[Zope] ZPublisher and creating children

Pavlos Christoforou pavlos@gaaros.msrc.sunysb.edu
Wed, 3 Mar 1999 10:57:23 -0500 (EST)


On Wed, 3 Mar 1999, Cees de Groot wrote:

> etcetera yet - that could work. Just hoped somebody on the list here would
> have a canned answer - I'm a lazy guy ;-)
> 
Everybody on this list is lazy ;-)

This is my approach:
First define a Webobject class that every object needs to inherit. The
simplest could be just:

class WebObject:
	        
    def __init__(self,name):
        '''__init__(self,name)
        '''
        
        self.name=string.replace(name,' ','_')

    def _set_root(self,root_obj,parent_path):
        '''_set_root(self,root_obj,parent_path)

        sets the root object and relative path. Note: 
	one will never need to pass parent_path directly. I also need
	to have a reference to the root object so I can do stuff remotely
	similar to acquisition'''

        self.root=root_obj
        self.rel_path=parent_path+self.name+os.sep

class Container(WebObject):

    def __init__(self,name):
        WebObject.__init__(self,name)
        ##Holds contents
        self._d={}

    def __getitem__(self, key): return self._d[key]

    def items(self): return self._d.items()

    def _do_add(self,object):
        '''_do_add(self,object)

        Adds object in container. Object MUST have inherited the
        WebObject class.'''

        name=object.name
        if hasattr(self,name) or self._d.has_key(name):
            raise ValueError,'Object with same name exists in WebFolder'
        d=self._d
        object._set_root(self.root,self.rel_path)

        d[name]=object
        self._d=d

So you can create any object that has inherited the WebObject class
independently and add it to the container instance.


Pavlos