[Zope] Creating an instance of another class?

Max M maxm at mxm.dk
Fri Feb 6 03:48:21 EST 2004


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




More information about the Zope mailing list