Re: [Zope] Creating an instance of another class?
On Thu, 5 Feb 2004 19:39:22 +0100 Dieter Maurer <dieter@handshake.de> wrote:
J C Lawrence wrote at 2004-2-4 20:25 -0500:
What is the correct way to use the OFS to create an instance of a product within an object?
<objectManagerDestination>.manage_addProduct[<productName>]. <constructor>(<arguments>)
This is not moving as elegantly as I'd expected. Any example cases out there? -- J C Lawrence ---------(*) Satan, oscillate my metallic sonatas. claw@kanga.nu He lived as a devil, eh? http://www.kanga.nu/~claw/ Evil is a name of a foeman, as I live.
You could make it easier for yourself if you wrap it with an external method or something. It also makes it easier to do custom stuff on added objects. I sometime use a function like this:: def add_portal_type(self, parent, portal_type, id='', title='', REQUEST=None): "Adds the content. This is a factory function." # generate a general id if id == '': id = self.ZopeTime().strftime(portal_type + '_%Y%m%d_%H%M%S') # set up fast factory lookup f = parent.manage_addProduct factory_functions = { 'Folder': f['OFSP'].manage_addFolder 'PersonalPortrait': f['ots_Image'].manage_addAction_ots_Image 'Document': f['ots_Document'].manage_addAction_ots_Document 'Image': f['ots_Image'].manage_addAction_ots_Image 'File': f['ots_File'].manage_addAction_ots_File 'User': f['ots_UserBase'].manage_AddAction_ots_UserBase } # create the object factory_functions[portal_type](id) # get the newly added content obj = getattr(parent, id) obj.title = title # Optional custom stuff if portal_type = 'Document': obj.description = 'Add your description here' return obj
J C Lawrence wrote:
On Thu, 5 Feb 2004 19:39:22 +0100 Dieter Maurer <dieter@handshake.de> wrote:
J C Lawrence wrote at 2004-2-4 20:25 -0500:
What is the correct way to use the OFS to create an instance of a product within an object?
<objectManagerDestination>.manage_addProduct[<productName>]. <constructor>(<arguments>)
This is not moving as elegantly as I'd expected. Any example cases out there?
This only works on ObjectManagers. It is very common, and you can find examples on zopelabs.com. If you want to add to a property on a non-folderish object, you must do it the usual Python way, with the constructor. See the bottom of http://cvs.zope.org/CMF/CMFDefault/DiscussionTool.py?rev=1.17&content-type=t... for an example. --jcc -- "He who fights with monsters should look to it that he himself does not become a monster. And when you gaze long into an abyss the abyss also gazes into you."
participants (3)
-
J C Lawrence -
J Cameron Cooper -
Max M