[Checkins] SVN: Products.CMFCore/branches/adapterize-wfstatus-wfhistory/Products/CMFCore/ Update workflow storage adapter branch, originally [83320]

Laurence Rowe l at lrowe.co.uk
Thu Sep 23 18:10:53 EDT 2010


Log message for revision 116781:
  Update workflow storage adapter branch, originally [83320]

Changed:
  U   Products.CMFCore/branches/adapterize-wfstatus-wfhistory/Products/CMFCore/WorkflowTool.py
  U   Products.CMFCore/branches/adapterize-wfstatus-wfhistory/Products/CMFCore/interfaces/_tools.py
  U   Products.CMFCore/branches/adapterize-wfstatus-wfhistory/Products/CMFCore/testing.py
  U   Products.CMFCore/branches/adapterize-wfstatus-wfhistory/Products/CMFCore/tests/test_WorkflowTool.py
  U   Products.CMFCore/branches/adapterize-wfstatus-wfhistory/Products/CMFCore/tool.zcml

-=-
Modified: Products.CMFCore/branches/adapterize-wfstatus-wfhistory/Products/CMFCore/WorkflowTool.py
===================================================================
--- Products.CMFCore/branches/adapterize-wfstatus-wfhistory/Products/CMFCore/WorkflowTool.py	2010-09-23 22:10:29 UTC (rev 116780)
+++ Products.CMFCore/branches/adapterize-wfstatus-wfhistory/Products/CMFCore/WorkflowTool.py	2010-09-23 22:10:53 UTC (rev 116781)
@@ -27,10 +27,18 @@
 from Persistence import PersistentMapping
 from zope.event import notify
 from zope.interface import implements
+from zope.interface import implementer
+from zope.component import adapts
+from zope.component import adapter
+from zope.component import getMultiAdapter
+from zope.component import queryMultiAdapter
 
 from Products.CMFCore.ActionProviderBase import ActionProviderBase
+from Products.CMFCore.interfaces import IContentish
 from Products.CMFCore.interfaces import IConfigurableWorkflowTool
 from Products.CMFCore.interfaces import IWorkflowDefinition
+from Products.CMFCore.interfaces import IWorkflowHistory
+from Products.CMFCore.interfaces import IWorkflowStatus
 from Products.CMFCore.interfaces import IWorkflowTool
 from Products.CMFCore.permissions import ManagePortal
 from Products.CMFCore.utils import _dtmldir
@@ -321,39 +329,26 @@
     def getHistoryOf(self, wf_id, ob):
         """ Get the history of an object for a given workflow.
         """
-        if hasattr(aq_base(ob), 'workflow_history'):
-            wfh = ob.workflow_history
-            return wfh.get(wf_id, None)
-        return ()
+        wf = self.getWorkflowById(wf_id)
+        return queryMultiAdapter((ob, wf), IWorkflowHistory, default=())
 
     security.declarePrivate('getStatusOf')
     def getStatusOf(self, wf_id, ob):
         """ Get the last element of a workflow history for a given workflow.
         """
-        wfh = self.getHistoryOf(wf_id, ob)
-        if wfh:
-            return wfh[-1]
+        wf = self.getWorkflowById(wf_id)
+        wfs = queryMultiAdapter((ob, wf), IWorkflowStatus, default=None)
+        if wfs is not None:
+            return wfs.get()
         return None
 
     security.declarePrivate('setStatusOf')
     def setStatusOf(self, wf_id, ob, status):
         """ Append a record to the workflow history of a given workflow.
         """
-        wfh = None
-        has_history = 0
-        if hasattr(aq_base(ob), 'workflow_history'):
-            history = ob.workflow_history
-            if history is not None:
-                has_history = 1
-                wfh = history.get(wf_id, None)
-                if wfh is not None:
-                    wfh = list(wfh)
-        if not wfh:
-            wfh = []
-        wfh.append(status)
-        if not has_history:
-            ob.workflow_history = PersistentMapping()
-        ob.workflow_history[wf_id] = tuple(wfh)
+        wf = self.getWorkflowById(wf_id)
+        wfs = getMultiAdapter((ob, wf), IWorkflowStatus)
+        wfs.set(status)
 
     #
     #   'IConfigurableWorkflowTool' interface methods
@@ -622,3 +617,34 @@
             ob.reindexObjectSecurity()
 
 InitializeClass(WorkflowTool)
+
+
+class DefaultWorkflowStatus(object):
+    implements(IWorkflowStatus)
+    adapts(IContentish, IWorkflowDefinition)
+
+    def __init__(self, context, workflow):
+        self.context = aq_base(context)
+        self.wf_id = workflow.getId()
+
+    def get(self):
+        history = getattr(self.context, 'workflow_history', {})
+        wfh = history.get(self.wf_id)
+        if wfh:
+            return wfh[-1]
+        return None
+
+    def set(self, status):
+        history = getattr(self.context, 'workflow_history', None)
+        if history is None:
+            history = self.context.workflow_history = PersistentMapping()
+        wfh = list(history.get(self.wf_id, ()))
+        wfh.append(status)
+        history[self.wf_id] = tuple(wfh)
+
+
+ at implementer(IWorkflowHistory)
+ at adapter(IContentish, IWorkflowDefinition)
+def default_workflow_history(context, workflow):
+        history = getattr(aq_base(context), 'workflow_history', {})
+        return history.get(workflow.getId(), ())

Modified: Products.CMFCore/branches/adapterize-wfstatus-wfhistory/Products/CMFCore/interfaces/_tools.py
===================================================================
--- Products.CMFCore/branches/adapterize-wfstatus-wfhistory/Products/CMFCore/interfaces/_tools.py	2010-09-23 22:10:29 UTC (rev 116780)
+++ Products.CMFCore/branches/adapterize-wfstatus-wfhistory/Products/CMFCore/interfaces/_tools.py	2010-09-23 22:10:53 UTC (rev 116781)
@@ -1820,6 +1820,10 @@
     """Plugin interface for workflow definitions managed by IWorkflowTool.
     """
 
+    def getId():
+        """ Return the id of the workflow definition.
+        """
+
     def getCatalogVariablesFor(ob):
         """ Return a mapping of attributes relevant to this workflow.
 
@@ -1991,6 +1995,22 @@
         """
 
 
+class IWorkflowStatus(Interface):
+
+    def get():
+        """Return the current workflow status or None
+        """
+
+    def set(status):
+        """Update the current workflow status to `status`
+        """
+
+
+class IWorkflowHistory(Interface):
+    """A sequence of workflow status dictionaries
+    """
+
+
 class ILinebreakNormalizer(Interface):
     
     """ Interface for a utility to normalize line breaks in plain text

Modified: Products.CMFCore/branches/adapterize-wfstatus-wfhistory/Products/CMFCore/testing.py
===================================================================
--- Products.CMFCore/branches/adapterize-wfstatus-wfhistory/Products/CMFCore/testing.py	2010-09-23 22:10:29 UTC (rev 116780)
+++ Products.CMFCore/branches/adapterize-wfstatus-wfhistory/Products/CMFCore/testing.py	2010-09-23 22:10:53 UTC (rev 116781)
@@ -140,6 +140,7 @@
         zcml.load_config('configure.zcml', zope.traversing)
         zcml.load_config('event.zcml', Products.Five)
         zcml.load_config('event.zcml', Products.CMFCore)
+        zcml.load_config('tool.zcml', Products.CMFCore)
         setHooks()
 
     @classmethod

Modified: Products.CMFCore/branches/adapterize-wfstatus-wfhistory/Products/CMFCore/tests/test_WorkflowTool.py
===================================================================
--- Products.CMFCore/branches/adapterize-wfstatus-wfhistory/Products/CMFCore/tests/test_WorkflowTool.py	2010-09-23 22:10:29 UTC (rev 116780)
+++ Products.CMFCore/branches/adapterize-wfstatus-wfhistory/Products/CMFCore/tests/test_WorkflowTool.py	2010-09-23 22:10:53 UTC (rev 116781)
@@ -28,6 +28,7 @@
 from Products.CMFCore.interfaces import IActionWillBeInvokedEvent
 from Products.CMFCore.interfaces import IContentish
 from Products.CMFCore.interfaces import IWorkflowDefinition
+from Products.CMFCore.testing import TraversingEventZCMLLayer
 
 
 class Dummy( SimpleItem ):
@@ -154,6 +155,8 @@
 
 class WorkflowToolTests(unittest.TestCase):
 
+    layer = TraversingEventZCMLLayer
+
     def _makeOne( self, workflow_ids=() ):
         from Products.CMFCore.WorkflowTool import WorkflowTool
 

Modified: Products.CMFCore/branches/adapterize-wfstatus-wfhistory/Products/CMFCore/tool.zcml
===================================================================
--- Products.CMFCore/branches/adapterize-wfstatus-wfhistory/Products/CMFCore/tool.zcml	2010-09-23 22:10:29 UTC (rev 116780)
+++ Products.CMFCore/branches/adapterize-wfstatus-wfhistory/Products/CMFCore/tool.zcml	2010-09-23 22:10:53 UTC (rev 116781)
@@ -41,4 +41,8 @@
       name="MemberData"
       />
 
+  <adapter factory=".WorkflowTool.DefaultWorkflowStatus" />
+
+  <adapter factory=".WorkflowTool.default_workflow_history" />
+
 </configure>



More information about the checkins mailing list