3

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.

rain2o
  • 498
  • 5
  • 20

1 Answers1

2

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)
Mathias
  • 6,777
  • 2
  • 20
  • 32
  • I tried your suggestion in the CSA. I received this error: `Unauthorized: You are not allowed to access 'setStatusOf' in this context` The content type has an `at_post_create_script` and `at_post_edit_script` function in its content type python file. I wonder if I could apply the force there? I have noticed that those functions don't get called when creating the items from `invokeFactory`, but maybe I can get it to? – rain2o Oct 02 '14 at 13:17
  • 1
    I was able to get `workflow_tool.doActionFor()` to work though. I just had to create a new workflow transition with the name of the action I wanted. – rain2o Oct 02 '14 at 13:44
  • You have to run the script with the right permission. afaik you can use a proxy role (Manager for example) to run the script. So you can avoid the permission problem (New workflow could also be a solution). You can also add this into your `at_post_create_script` script. But also there you habe to make sure to have the right permissions. – Mathias Oct 03 '14 at 06:35
  • I have the CSA running with manager proxy role, and I was testing as site admin when I received that error. As for the `at_post_create_script`, it doesn't seem to run when creating the content this way. It does when I manually "Add New", but not with the CSA creating it. Any idea why and how to fix that? – rain2o Oct 03 '14 at 10:56
  • 1
    I guess you have to call the `at_post_create_script` manually. – Mathias Oct 08 '14 at 08:43