Adding Folders with Contents with FormGen

by Espen Moe-Nilssen last modified Dec 14, 2009 04:01 PM
Adding content: Folders and contents in the folders with Plone FormGen

This is a simple script that adds Plone large Folder with documents in it. I am reusing the same variables so if you need to add more subfolders it can be done by just duplicating lines.

Please read this first: Creating Content from PFG

 

Screenshots

 
PloneFormGen_emn02
 

Screenshot of how it could look for the plone user. 

This is a slighly different setup than the script below......

The name of the folder where the content get added is "blocks", which means that on this site, the target line is this:

target = context.blocks.

 
PloneFormGen_emn01
 

The name of the (large plone) folder "SW1" now contains 200 Wafers.

On this site, the line  that adds content was changed from:

target.invokeFactory("Document", id=uid, title=uid')

to

target.invokeFactory("Wafer", id=uid, title=uid')

 

 

The Script

___________________________________

# Find our target folder from the context. The ID of our target folder is "myfolder"
#In other word, you need to have a folder called "myfolder" in your site (at http://yoursite/myfolder)

target = context.myfolder

# The request object has an dictionary attribute named form that contains
#the submitted form content, keyed by field name
#In other words, it is possible to use the fields from the form in this script

form = request.form

#The scirpt uses the field "foldername" for id and title.
#Be aware that if this folder already exists you will get an error.
#Other fields can be used like this: form['name_of_field']
#Change "Large Plone Folder" to "Folder" if you want that instead

uid = form['foldername']
target.invokeFactory("Large Plone Folder", id=uid , title=uid, )

# Find our new object in the target folder
obj = target[uid]

# Force it to be reindexed with the new content
obj.reindexObject()

 

#Now lets add some documents in the large folder.
#The numbers of Document that will be created is taken from "howmany" field

toadd = int(form['howmany'])
target = obj

for theid in range(1, toadd+1):

    # The %03d is for making 1 into "001" and 11 into "011" so the documents sort OK.
    uid='%03d' % theid

    # We use the "invokeFactory" method of the target folder to create a content object
    target.invokeFactory("Document", id=uid, title=uid')

    # Find our new object in the target folder
    obj = target[uid]

    # Force it to be reindexed with the new content
    obj.reindexObject()

# We use the "invokeFactory" method of the target folder to create
#another document with name and id = "info"
target.invokeFactory("Document", id="info", title="Info")

# Find our new object in the target folder
obj = target['info']

# Force it to be reindexed with the new content
obj.reindexObject()