RE: [Zope] ZPublisher and creating children
Cees wrote:
The Container, IMHO, "knows" too much about the Child in this case, and it's hard to modify the attribute set of the Child class. Am I doing something wrong? Is there a design pattern here I'm not aware of?
Actually this example shows the beauty of Zope/Bobo programming -- the code looks completely like normal Python code, no CGI hackery involved. Anyway, this is really a Python question, IMO. You don't the arguments to the Child's constructor to be hardwired in the Parent, right? You could take a route where pass a dictionary, e.g. REQUEST, into the Child's constructor and let it figure out for itself what it needs. This feels less OO and doesn't let Zope/Bobo help you with type conversions and raising exceptions for missing arguments. --Paul
Paul Everitt wrote:
Cees wrote:
The Container, IMHO, "knows" too much about the Child in this case, and it's hard to modify the attribute set of the Child class. Am I doing something wrong? Is there a design pattern here I'm not aware of?
Actually this example shows the beauty of Zope/Bobo programming -- the code looks completely like normal Python code, no CGI hackery involved.
Anyway, this is really a Python question, IMO. You don't the arguments to the Child's constructor to be hardwired in the Parent, right?
Well, the underlying issue is the stateless-ness of the whole thing. In normal python, you'd create an empty child, call the child's editing form so the user can enter initial data, and only after the user's commit add the child to the container: def addChild(self): newChild = Child() if newChild.editForm() == true: container.append(newChild) in short. I've attempted this in the ZPublisher environment, but to no avail - either it didn't work at all or it got very messy (with the Child needing to know about the Parent, etcetera). I must say, I was under a bit of time pressure last night when looking for alternatives (wanted to have a beta release on my website before breakfast), I haven't got around playing with hidden form variables etcetera yet - that could work. Just hoped somebody on the list here would have a canned answer - I'm a lazy guy ;-)
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
participants (3)
-
Cees de Groot -
Paul Everitt -
Pavlos Christoforou