Skip to content

Instantly share code, notes, and snippets.

@btbytes
Created March 7, 2009 04:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save btbytes/75218 to your computer and use it in GitHub Desktop.
Save btbytes/75218 to your computer and use it in GitHub Desktop.
Create Plone Documents programatically from the shell/script
'''
create_plone_doc.py
Create Plone Document from debug shell.
Useful for creating content automatically in Plone. Think data import from other CMSes. etc.
'''
from wrap import wrap #see http://gist.github.com/75207
from Products.CMFCore.utils import getToolByName
import transaction
app = wrap(app, 'admin', 'passwd')
# `test` is the plone site
urltool = getToolByName(app.test, 'portal_url')
portal = urltool.getPortalObject()
#place you want to create the content, here its the root of the site
container = portal
#create a transaction
t = transaction.get()
#create an object
id = 'helloworld'
container.invokeFactory(type_name="Document", id=id)
doc = container[id]
doc.setTitle("Hello world")
doc.setText("<h3>Section 1</h3> <p>Introduction</p>")
#is the doc published? No.
container.portal_workflow.getInfoFor(doc, 'review_state', '')
wftool = container.portal_workflow
wftool.doActionFor(doc, 'publish', comment='publishing programmatically')
# set Tags
doc.setSubject(('foo', 'bar'))
# allow comments
doc.allowDiscussion(True)
#exclude from navigation as this is in top level
#and default behaviour is to create a nav tab for children at root
doc.setExcludeFromNav(True)
#Create a folder
id = 'programming'
container.invokeFactory(type_name="Folder", id=id)
fol = container['programming']
wftool.doActionFor(fol, 'publish', comment='publishing folder')
# commit changes
t.commit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment