quick search:
 

Add CMF content programmatically

Submitted by: runyaga
Last Edited: 2006-01-10

Category: CMF

Average rating is: 2.0 out of 5 (2 ratings)

Description:
in the CMF you can add content via invokeFactory on a PortalFolder.
if you are the zope interface and running a python script, here is
how you add a portal folder and some content inside it


Source (Text):
#create a folder
f_id = 'my_new_folder'
context.manage_addProduct['CMFCore'].manage_addContent(type='Portal Folder', id='my_new_folder')
o = getattr(context, f_id)
o.manage_addProduct['CMFDefault'].manage_addContent(type='Portal Image', id='new image1')
o.manage_addProduct['CMFDefault'].manage_addContent(type='Document', id='document no 2')

return 'fin'

Explanation:
pretty self explanatory. could probably still call invokeFactory(?)
but this is the 'traditional' way of doing it.

NOTE: Portal Content can only reside in Portal Folders


Comments:

Change access control by ksato - 2004-10-14
How can I change the access control properties of created 
contents programmatically (instead of clicking 'Security'
tab and change them manually)  ?
 
Re: Change access control by wazum - 2004-10-14
this should work:
XXXobject.manage_permission('View',('Manager','Member','Owner',),acquire=0)



Extending The Script by cer - 2004-10-14
I've been using this recipe for quite a while now, and as a result
have extended it to include editing of metadata, attributes, and
properties where they apply. I've also included the routines for
adding page templates.

Typically I'm programmatically adding content from files in the file
system so I'll use a dictionary to control the item attributes...

o = context or folder object where content being added
oi = item

Page Templates

idict = { 'id' : 'documentId'
        , 'type': 'page template'
        , 'body': *ZPT template here*
        }

o.manage_addProduct['PageTemplates'].manage_addPageTemplate(id=idict['id'])
oi = getattr(o,idict['id'])
oi.pt_edit(idict['body'],content_type)

CMF Type

idict = { 'id' : 'documentId'
        , 'type': 'Document'
        , 'title': 'This is a Document'
        , 'description': 'This is the document description'
        , 'subject': 'document, automated'
        , 'body': 'This is the body of the document which will be added'
        , 'workflow_action': 'publish'
        , 'workflow_comment': 'This item published programmatically'
        }

o.manage_addProduct['CMFDefault'].manage_addContent(type=idict['type'],id=idict['id'])
oi = getattr(o,idict['id'])
oi.editMetadata( title=idict['title']
                 , description=idict['description']
                 , subject=idict['subject']
                )
oi.edit(text_format='', file='', text=idict['body'])
review_state = oi.portal_workflow.getInfoFor(oi, 'review_state', '')
	
# validate review state, and set to published if has not been set to
# published previously
#
if not review_state == 'published':
	oi.portal_workflow.doActionFor(oi,idict['workflow_action'],
    			                   comment=idict['workflow_comment'])

Portal Folder

idict = { 'id' : 'FolderId'
        , 'type': 'Folder'
        , 'title': 'This is a Folder'
        , 'description': 'This is the folder description'
        }

o.invokeFactory(type_name=idict['type'], id=idict['id'])
oi = getattr(o,idict['id'])
oi.edit(title=idict['title'],description=idict['description'])
 
Re: Extending The Script by wedr2 - 2004-10-14
When I execute your script, the content type is 'None' so I can't view it (just edit it). Thank U
 
Re: Re: Extending The Script by jw - 2004-10-14
Cer's example worked for me with the correct CMF workflow when I replaced
 
  o.manage_addProduct['CMFDefault'].manage_addContent(type=idict['type'],id=idict['id'])

with

  o.invokeFactory(idict['type'],idict['id'])


best approach by runyaga - 2004-10-14
is to call invokeFactory() on the container in which you want to
create the object.  in CMF1.3 you say
folderishObject.invokeFactory(type_id, obj_id)

i.e. portal.invokeFactory('Folder', 'Members')


help me please by malice120 - 2004-10-14
i have errors when i click on workflow of plone document

no portal types
no workflow 


path of the folder by plone - 2006-01-10
I've got some problems with the path of the folder.
I used the script on the top in my ZMI but I wasn't able 
to create the folder in a specific path 
(i.e. absolute_url()+'/folder1/Subfolder1/...') 
but only in the path where the python script is. 

Any Ideas?
Thank UUUUU