You are here: Home / Documentation / How To's / A simple "question and answer" discussion forum using PloneFormGen, a mailer, a custom script adapter, and built-in workflow

A simple "question and answer" discussion forum using PloneFormGen, a mailer, a custom script adapter, and built-in workflow

by nguyen — published Sep 30, 2014 02:35 PM, last modified Aug 16, 2016 10:49 AM
a case study

A campus unit wanted to create a Q&A format discussion forum that would let anonymous users ask a question that would be answered by forum editors.

They started with a very simple PloneFormGen form that contained just one text field, into which the user types their question.

A mailer adapter sends an email to forum editors letting them know a question has been submitted.

A custom script adapter creates a Plone page in a specific folder (in this case, it's the folder containing the PloneFormGen form), and leaves it in the private state.  A forum editor edits the page to include an answer to the question, then publishes the page.

The custom script adapter is given Manager proxy so it can create Plone pages even for non-logged in users.  

The script is as follows.

# 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[:20])

question = request.form['comments']

parent = context.aq_inner.aq_parent
grandparent = parent.aq_inner.aq_parent

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

newItemId = grandparent.invokeFactory(id=id, type_name='Document')
newItem = grandparent[newItemId]
newItem.setTitle(question[:20])
newItem.setText(question)
newItem.reindexObject()

context.plone_utils.addPortalMessage("Question has been created (ID %s); it will be reviewed soon." % newItemId, 'info')
 

 

Navigation