[Zope-dev] How to create a product with all_meta_types and the

Max M maxm at mxm.dk
Fri Mar 12 04:34:19 EST 2004


Garito wrote:

> Hi Chris!
> 
> Want I want exactly is to create a product's object by his name
> programmatically not by the construction form
> Is this posible?


You cannot know the name of the constructor function, unless you read 
the source.

The constructors added to Zope during the product initialisation can be 
both forms and actual function methods. There is no way of knowing 
which. And there can be more than two.

So you would need to write a factory function based on the info you can 
get from the source. Ie. in an external method. Something like::


def portal_factory(self, parent, portal_type, id='', title='',):

     "Adds the content. A factory function."

     # generate a general id
     if id == '':
         id = self.ZopeTime().strftime(portal_type + '_%Y%m%d_%H%M%S')

     zope_factory = parent.manage_addProduct

     # add the content
     if 'Document' == portal_type:
         zope_factory['Document'].manage_addDocumentAction(id)

     elif 'Some Type' == portal_type:
         zope_factory['Some Type'].manage_addSomeTypeAction(id)

     else:
         raise 'Error adding portal_type'

     # get the newly added content
     obj = getattr(parent, id)
     # Add the title
     obj.title = title

     return obj


-- 

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science



More information about the Zope-Dev mailing list