quick search:
 

x Random Objects in CMF

Submitted by: runyaga
Last Edited: 2001-09-04

Category: CMF

Average rating is: 0.0 out of 5 (0 ratings)

Description:
only works with Python2.1! i needed to create some test data in a folder in CMF. here is a quick script that generates 50 random objects in a folder. demonstrates how to create objects in CMF.

Source (Text):
import random
from DateTime import DateTime

target=getattr(context, 'anotehrTest')
types=[objTypeInfo.getId() for objTypeInfo in context.portal_url.getPortalObject().allowedContentTypes()]

for x in range(50):
    random_id = str(DateTime().millis())+ str(random.randint(0,100000))
    random_type = types[random.randint(0, len(types)-1)]
    target.invokeFactory(type_name=random_type, id=random_id)
    o = getattr(target, random_id)
    o.setTitle(random_id +'s title')

return 'fin'

Explanation:
the list comprehension (types= expression) is only available in
Python2.1 and higher. target is the name of the folder which you want to put the objects in.
if you wanted it to be in context/folder/subfolder/subfolder1 you would
change it to: target = getattr(context.folder.subfolder, 'subfolder1') or
simply target=context.folder.subfolder.subfolder1 if you wanted to create a
100 random objets change for range(50) to range(100)


Comments:

No Comments