Back to index

plone3  3.1.7
workflow.py
Go to the documentation of this file.
00001 from zope.interface import Interface, implementer
00002 from zope.component import adapter
00003 from Acquisition import aq_base, aq_inner
00004 from Globals import PersistentMapping
00005 from Products.CMFCore.interfaces import IWorkflowTool
00006 from Products.CMFPlone.interfaces import IWorkflowChain
00007 
00008 @adapter(Interface, IWorkflowTool)
00009 @implementer(IWorkflowChain)
00010 def ToolWorkflowChain( context, workflow_tool ):
00011     """Looks up the workflow chain by portal type suing a mapping
00012     stored on the tool::
00013 
00014       >>> from Products.CMFPlone.tests.dummy import DummyContent, DummyWorkflowTool
00015       >>> tool = DummyWorkflowTool()
00016       >>> content = DummyContent(id='dummy', portal_type='DummyType')
00017 
00018     Either an object with a portal_type or the portal_type as a
00019     string.  If we pass in an unknown portal_type we get the default
00020     chain::
00021 
00022       >>> ToolWorkflowChain('A Type', tool)
00023       ('Default Workflow',)
00024       >>> tool.setChainForPortalTypes(('A Type',), ('Some Workflow',))
00025       >>> ToolWorkflowChain('A Type', tool)
00026       ('Some Workflow',)
00027 
00028     When we pass in a piece of content we get similar behavior::
00029 
00030       >>> ToolWorkflowChain(content, tool)
00031       ('Default Workflow',)
00032       >>> tool.setChainForPortalTypes(('DummyType',), ('Some Workflow',))
00033       >>> ToolWorkflowChain(content, tool)
00034       ('Some Workflow',)
00035 
00036    If we can't figure out a portal_type then we return an empty chain::
00037 
00038       >>> ToolWorkflowChain((), tool)
00039       ()
00040 
00041     """
00042     if isinstance(context, basestring):
00043         pt = context
00044     elif hasattr(aq_base(context), 'getPortalTypeName'):
00045         pt = context.getPortalTypeName()
00046     else:
00047        pt = None
00048     if pt is None:
00049         return ()
00050     chain = None
00051 
00052     # Unfortunately we need to rely on a private variable here
00053     cbt = workflow_tool._chains_by_type
00054 
00055     if cbt is not None:
00056         chain = cbt.get(pt, None)
00057     if chain is None:
00058         chain = workflow_tool.getDefaultChainFor(context)
00059         if chain is None:
00060             return ()
00061     return chain
00062