Patrick Downey
Patrick Downey

Reputation: 965

Plone/Workflow- Is it possible to set the state of a workflow without needing a transition?

I have a content type (batch) that is tied to multiple instances of a different content (my_item) by an id. The my_item content type has a workflow consisting of draft, pending, and approved. There is a form that creates the batch and "approves" the my_item content type instances, and sets the my_item_instances' batch_id to the batch's batch id (set when the batch is created). The approved state is a final state, where it cannot be edited nor retracted.

I need to be able to change the state of the my_item content type instances back to draft. Since there is no transition for back to draft from the approved state when the item is being deleted (through a subscriber), I need to somehow set the state of the my_items back to "draft" without needing a transition.

There are two methods I tried:

The subscriber is IObjectWillBeRemoved

def my_item_will_be_removed(obj,event)
    my_items = catalog.searchResults('batch_id':obj.batch_id)
    for i in my_items:
        api.content.transition(obj=i,to_state='pending')

This results in an error InvalidParaemterError: Could not find workflow to set state to draft on

I also tried using:

wf_tool = api.portal.get_tool(name='portal_workflow')
wf_tool.setStatusOf('item_workflow',i,'pending')

For some reason that ends up with the my_item becoming a string.

Is it not possible? If it is possible, how can I do so?


Offtopic, but I guess a workaround I could use for now is: make a transition "retract_from_approval" that goes from the approved state to the draft state

'can_retract_from_approval' needs to be assigned to the role that can delete the "batch"

In the deletion event, iterate through the my_items, assign the 'can_retract_from_approval' permission to the role responsible for deleting the batch locally on the current iteration

my_items = catalog.searchResults('batch_id',obj.batch_id)
for m in my_items:
    mi_obj = m.getObject()
    mi_obj.manage_permission('retract_from_approval',['ARole'],obj=mi_obj)

Then use the workflow tool to do the 'retract_from_approval' transition that sends the my_item back into the draft state. And then remove the 'can_retract_from_approval' permission.

Upvotes: 1

Views: 199

Answers (1)

keul
keul

Reputation: 7819

This is a snippet of an old migration tool I used for migrate from Plone 2.5 to Plone 3 a lot of years ago.

    wtool = getToolByName(obj, 'portal_workflow')
    status = {'action': '', 
              'review_state': old_state, 
              'actor': 'admin', 
              'comments': 'Recovery state', 
              'time': DateTime() }

    wtool.setStatusOf(workflow_id, obj, status)
  1. not sure if it still works nowadays
  2. you probably need a reindexObjectSecurity

Upvotes: 4

Related Questions