You are here: Home / Documentation / How To's / Using Custom Script Adapters with PloneFormGen forms to do a lot behind the scenes

Using Custom Script Adapters with PloneFormGen forms to do a lot behind the scenes

by nguyen — published Sep 30, 2014 11:15 AM, last modified Aug 16, 2016 10:50 AM

One nice thing you can do with PloneFormGen is present the user with a simple form that does a bunch more in the back end, using custom script adapters.

In these cases we wanted to give users one form to add a story to a web site, including a headline, text, photo, etc., and have the form's custom script adapter create the necessary folder, then the news item, the page, etc., and apply keywords/tags/categories as needed.

Here are the scripts we used with the forms.

Script 1

This script creates a News Item in a particular folder.

# Available parameters:
#  fields  = HTTP request form fields as key value pairs
#  request = The current HTTP request. 
#            Access fields by request.form["myfieldname"]
#  ploneformgen = PloneFormGen object
# 
# Return value is not processed -- unless you
# return a dictionary with contents. That's regarded
# as an error and will stop processing of actions
# and return the user to the form. Error dictionaries
# should be of the form {'field_id':'Error message'}

from Products.CMFPlone.utils import normalizeString


def suggestId(parent, title):
    return normalizeString(title)


title = request.form['entry-title']
image = request.form['entry-image_file']
imageDescription = request.form['entry-image-description']
storyDescription = request.form['entry-description']
story = request.form['story']
category = request.form['entry-category']

parent = context.aq_inner.aq_parent
grandparent = parent.aq_inner.aq_parent
entries = grandparent.get('all-entries')

id = suggestId(entries, title)
while hasattr(entries, id):
  id += "_1" # make it unique

newNewsItemId = entries.invokeFactory(id=id, type_name='News Item')
newNewsItem = entries[newNewsItemId]
newNewsItem.setTitle(title)
newNewsItem.setDescription(storyDescription)
newNewsItem.setImage(image)
newNewsItem.setImageCaption(imageDescription)
newNewsItem.setText(story)
newNewsItem.setSubject(category)
newNewsItem.reindexObject()

context.plone_utils.addPortalMessage("Created a news item with ID %s" % newNewsItemId, 'info')

portal_state = ploneformgen.restrictedTraverse('@@plone_portal_state')
url = portal_state.portal_url() + '/search'
request.response.redirect('%s?Creator=%s&sort_on=created&sort_order=reverse' % (url, portal_state.member().getId()))
 

 

Script 2

This one is a bit more complicated. It creates a folder that holds a News Item and a Document (Page).

# Available parameters:
#  fields  = HTTP request form fields as key value pairs
#  request = The current HTTP request. 
#            Access fields by request.form["myfieldname"]
#  ploneformgen = PloneFormGen object
# 
# Return value is not processed -- unless you
# return a dictionary with contents. That's regarded
# as an error and will stop processing of actions
# and return the user to the form. Error dictionaries
# should be of the form {'field_id':'Error message'}

from Products.CMFPlone.utils import normalizeString

def suggestId(parent, title):
   return normalizeString(title)

title = request.form['story-title']
image = request.form['main-image_file']
imageDescription = request.form['image-description']
storyDescription = request.form['story-description']
category = request.form['category']

stories = context.stories
defaultPageContent = stories['default-page-content'].getText()

id = suggestId(stories, title)
while hasattr(stories, id):
  id += "_1" # make it unique

newStoryFolderId = stories.invokeFactory(id=id, type_name='Folder')
newStoryFolder = stories[newStoryFolderId]
newStoryFolder.setSubject(category)
newStoryFolder.setTitle(title)
newStoryFolder.reindexObject()

id = suggestId(newStoryFolder, title)
id = "%s-1" % id
newStoryPageId = newStoryFolder.invokeFactory(id=id, type_name='Document')
newStoryPage = newStoryFolder[newStoryPageId]
newStoryPage.setTitle(title)
newStoryPage.setDescription(storyDescription)
newStoryPage.setSubject(category)
newStoryPage.setText(defaultPageContent)
newStoryPage.reindexObject()

id = suggestId(newStoryFolder, title)
id = "%s-2" % id
newNewsItemId = newStoryFolder.invokeFactory(id=id, type_name='News Item')
newNewsItem = newStoryFolder[newNewsItemId]
newNewsItem.setTitle(title)
newNewsItem.setDescription(storyDescription)
newNewsItem.setImage(image)
newNewsItem.setImageCaption(imageDescription)
newNewsItem.setSubject(category)
newNewsItem.reindexObject()

id = suggestId(newStoryFolder, title)
id = "%s-3" % id
newStoryImageId = newStoryFolder.invokeFactory(id=id, type_name='Image')
newStoryImage = newStoryFolder[newStoryImageId]
newStoryImage.setTitle(title)
newStoryImage.setImage(image)
newStoryImage.setDescription(imageDescription)
newStoryImage.setSubject(category)
newStoryImage.reindexObject()
 

 

 

Navigation