ploneploneformgen

Include "Change Note" when creating content from InvokeFactory


I am creating a content item from a PloneFormGen Form Custom Script Adapter using invokeFactory. Everything is working fine so far, however we want to start generating a comment to be included in the create action, for the history of the item. The comment itself will be generated using fields from the form and some preset text.

Is this something that would be possible from PFG?

The content type is a custom type, and it is versionable. Using Plone 4.3.2, PFG 1.7.14

EDIT

My current code:

from Products.CMFPlone.utils import normalizeString

portal_root = context.portal_url.getPortalObject()
target = portal_root['first-folder']['my-folder']
form = request.form
title = "My Title: "+form['title-1']
id = normalizeString(title)
id = id+"_"+str(DateTime().millis())

target.invokeFactory(
    "MyCustomType",
    id=id,
    title=title,
    text=form['comments'],
    relatedItems=form['uid']
    )

I have tried using keys like comments, comment, message, and even cmfeditions_version_comment within the target.invokeFactory arguments. No luck so far.


Solution

  • I'm not sure if that's possible in a custom script adapter.

    The action of you first entry is None. The history automatically shows Create if the action is None. This is implemented here (plone.app.layout.viewlets.content)

    # On a default Plone site you got the following
    >>> item.workflow_history
    {'simple_publication_workflow': ({'action': None, 'review_state': 'private', 'actor': 'admin', 'comments': '', 'time': DateTime('2014/10/02 08:08:53.659345 GMT+2')},)}
    

    Key of the the dict is the workflow id and the value is a tuple of all entries. So you can manipulate the entry like you want. But I don't know if this is possible with restricted python (custom script adapter can only use restricted python).

    But you could also add a new entry, by extending you script with:

    ...
    
    new_object = target.get(id)
    workflow_tool = getToolByName(new_object, 'portal_workflow')
    
    workflows = workflow_tool.getWorkflowsFor(new_object)
    
    if not workflows:
        return
    
    workflow_id = workflows[0].id  # Grap first workflow, if you have more, take the the one you need
    review_state = workflow_tool.getInfoFor(new_object, 'review_state', None)
    
    history_entry = {
                     'action' : action, # Your action
                     'review_state' : review_state,
                     'comments' : comment, # Your comment
                     'actor' : actor, # Probably you could get the logged in user
                     'time' : time,
                     }
    
    workflow_tool.setStatusOf(workflow_id, context, history_entry)