[Checkins] SVN: Products.CMFCore/branches/adapterize-wfstatus-wfhistory/Products/CMFCore/ Adapterize workflow status and workflow history lookup.

Laurence Rowe l at lrowe.co.uk
Wed Jan 30 13:50:12 EST 2008


Log message for revision 83320:
  Adapterize workflow status and workflow history lookup.
  * No test coverage, but I have verified it works with Plone 3.0
  * see http://plone.org/products/plone/roadmap/221 for motivation, but I have made changes as suggested by yuppie on cmf-dev

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/tool.zcml

-=-
Modified: Products.CMFCore/branches/adapterize-wfstatus-wfhistory/Products/CMFCore/WorkflowTool.py
===================================================================
--- Products.CMFCore/branches/adapterize-wfstatus-wfhistory/Products/CMFCore/WorkflowTool.py	2008-01-30 18:46:29 UTC (rev 83319)
+++ Products.CMFCore/branches/adapterize-wfstatus-wfhistory/Products/CMFCore/WorkflowTool.py	2008-01-30 18:50:11 UTC (rev 83320)
@@ -28,11 +28,19 @@
 from OFS.ObjectManager import IFAwareObjectManager
 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 ActionProviderBase import ActionProviderBase
 from interfaces import IConfigurableWorkflowTool
 from interfaces import IWorkflowDefinition
 from interfaces import IWorkflowTool
+from interfaces import IWorkflowHistory
+from interfaces import IWorkflowStatus
+from interfaces import IContentish
 from interfaces.portal_workflow import portal_workflow as z2IWorkflowTool
 from permissions import ManagePortal
 from utils import _dtmldir
@@ -322,39 +330,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
@@ -642,3 +637,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	2008-01-30 18:46:29 UTC (rev 83319)
+++ Products.CMFCore/branches/adapterize-wfstatus-wfhistory/Products/CMFCore/interfaces/_tools.py	2008-01-30 18:50:11 UTC (rev 83320)
@@ -1800,6 +1800,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.
 
@@ -1969,3 +1973,20 @@
 
         o Permission:  Private (Python only)
         """
+
+
+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
+    """
+    
\ No newline at end of file

Modified: Products.CMFCore/branches/adapterize-wfstatus-wfhistory/Products/CMFCore/tool.zcml
===================================================================
--- Products.CMFCore/branches/adapterize-wfstatus-wfhistory/Products/CMFCore/tool.zcml	2008-01-30 18:46:29 UTC (rev 83319)
+++ Products.CMFCore/branches/adapterize-wfstatus-wfhistory/Products/CMFCore/tool.zcml	2008-01-30 18:50:11 UTC (rev 83320)
@@ -34,4 +34,8 @@
       global="False"
       />
 
+  <adapter factory=".WorkflowTool.DefaultWorkflowStatus" />
+
+  <adapter factory=".WorkflowTool.default_workflow_history" />
+
 </configure>



More information about the Checkins mailing list