Back to index

plone3  3.1.7
workflow.py
Go to the documentation of this file.
00001 from OFS.SimpleItem import SimpleItem
00002 from persistent import Persistent 
00003 
00004 from zope.interface import implements, Interface
00005 from zope.component import adapts
00006 from zope.formlib import form
00007 from zope import schema
00008 
00009 from plone.contentrules.rule.interfaces import IExecutable, IRuleElementData
00010 
00011 from plone.app.contentrules.browser.formhelper import AddForm, EditForm 
00012 
00013 import transaction
00014 from Acquisition import aq_inner, aq_parent
00015 from ZODB.POSException import ConflictError
00016 from Products.CMFCore.utils import getToolByName
00017 from Products.CMFPlone import PloneMessageFactory as _
00018 
00019 from Products.CMFPlone import utils
00020 from Products.statusmessages.interfaces import IStatusMessage
00021 
00022 class IWorkflowAction(Interface):
00023     """Interface for the configurable aspects of a workflow action.
00024     
00025     This is also used to create add and edit forms, below.
00026     """
00027     
00028     transition = schema.Choice(title=_(u"Transition"),
00029                                description=_(u"Select the workflow transition to attempt"),
00030                                required=True,
00031                                vocabulary='plone.app.vocabularies.WorkflowTransitions')
00032          
00033 class WorkflowAction(SimpleItem):
00034     """The actual persistent implementation of the action element.
00035     """
00036     implements(IWorkflowAction, IRuleElementData)
00037     
00038     transition = ''
00039     element = "plone.actions.Workflow"
00040     
00041     @property
00042     def summary(self):
00043         return _(u"Execute transition ${transition}", mapping=dict(transition=self.transition))
00044     
00045 class WorkflowActionExecutor(object):
00046     """The executor for this action.
00047     """
00048     implements(IExecutable)
00049     adapts(Interface, IWorkflowAction, Interface)
00050          
00051     def __init__(self, context, element, event):
00052         self.context = context
00053         self.element = element
00054         self.event = event
00055 
00056     def __call__(self):
00057         portal_workflow = getToolByName(self.context, 'portal_workflow', None)
00058         if portal_workflow is None:
00059             return False
00060             
00061         obj = self.event.object
00062         
00063         try:
00064             portal_workflow.doActionFor(obj, self.element.transition)
00065         except ConflictError, e:
00066             raise e
00067         except Exception, e:
00068             self.error(obj, str(e))
00069             return False
00070         
00071         return True 
00072 
00073     def error(self, obj, error):
00074         request = getattr(self.context, 'REQUEST', None)
00075         if request is not None:
00076             title = utils.pretty_title_or_id(obj, obj)
00077             message = _(u"Unable to change state of ${name} as part of content rule 'workflow' action: ${error}",
00078                           mapping={'name' : title, 'error' : error})
00079             IStatusMessage(request).addStatusMessage(message, type="error")
00080         
00081 class WorkflowAddForm(AddForm):
00082     """An add form for workflow actions.
00083     """
00084     form_fields = form.FormFields(IWorkflowAction)
00085     label = _(u"Add Workflow Action")
00086     description = _(u"A workflow action triggers a workflow transition on an object.")
00087     form_name = _(u"Configure element")
00088     
00089     def create(self, data):
00090         a = WorkflowAction()
00091         form.applyChanges(a, self.form_fields, data)
00092         return a
00093 
00094 class WorkflowEditForm(EditForm):
00095     """An edit form for workflow rule actions.
00096     """
00097     form_fields = form.FormFields(IWorkflowAction)
00098     label = _(u"Edit Workflow Action")
00099     description = _(u"A workflow action triggers a workflow transition on an object.")
00100     form_name = _(u"Configure element")