Added 'principal' parameter in several workflow interfaces
authorThierry Florac <thierry.florac@onf.fr>
Mon, 11 Sep 2017 14:28:08 +0200
changeset 37 2b8636f18d39
parent 36 a84e933260c6
child 38 ecaf0ac4b04e
Added 'principal' parameter in several workflow interfaces
src/pyams_workflow/workflow.py
--- a/src/pyams_workflow/workflow.py	Mon Sep 11 14:27:28 2017 +0200
+++ b/src/pyams_workflow/workflow.py	Mon Sep 11 14:28:08 2017 +0200
@@ -23,11 +23,11 @@
 # import packages
 from pyams_utils.adapter import adapter_config
 from pyams_utils.registry import get_utility
-from pyams_utils.request import check_request
+from pyams_utils.request import check_request, query_request
 from pyams_utils.traversing import get_parent
 from pyams_utils.vocabulary import vocabulary_config
 from pyramid.httpexceptions import HTTPUnauthorized
-from pyramid.threadlocal import get_current_registry
+from zope.component.globalregistry import getGlobalSiteManager
 from zope.componentvocabulary.vocabulary import UtilityVocabulary
 from zope.interface import implementer
 from zope.lifecycleevent import ObjectModifiedEvent
@@ -87,7 +87,8 @@
                  manager_states=None,
                  published_states=None,
                  waiting_states=None,
-                 retired_states=None):
+                 retired_states=None,
+                 auto_retired_state=None):
         self.refresh(transitions)
         self.states = states
         self.initial_state = initial_state
@@ -98,6 +99,7 @@
         self.published_states = set(published_states) if published_states else set()
         self.waiting_states = set(waiting_states) if waiting_states else set()
         self.retired_states = set(retired_states) if retired_states else set()
+        self.auto_retired_state = auto_retired_state
 
     def _register(self, transition):
         transitions = self._sources.setdefault(transition.source, {})
@@ -151,7 +153,7 @@
     def name(self):
         return self.parent.workflow_name
 
-    def fire_transition(self, transition_id, comment=None, side_effect=None, check_security=True):
+    def fire_transition(self, transition_id, comment=None, side_effect=None, check_security=True, principal=None):
         versions = IWorkflowVersions(self.parent)
         state = IWorkflowState(self.context)
         # this raises InvalidTransitionError if id is invalid for current state
@@ -165,30 +167,32 @@
         if not transition.condition(self, self.context):
             raise ConditionFailedError()
         # perform action, return any result as new version
+        if principal is None:
+            request = query_request()
+            if request is not None:
+                principal = request.principal
         result = transition.action(self, self.context)
         if result is not None:
-            # clear result history
-            IWorkflowState(result).history.clear()
             # stamp it with version
-            versions.add_version(result, transition.destination)
+            versions.add_version(result, transition.destination, principal)
             # execute any side effect:
             if side_effect is not None:
                 side_effect(result)
-            event = WorkflowVersionTransitionEvent(result, self.wf, self.context,
+            event = WorkflowVersionTransitionEvent(result, self.wf, principal, self.context,
                                                    transition.source,
                                                    transition.destination,
                                                    transition, comment)
         else:
-            versions.set_state(state.version_id, transition.destination)
+            versions.set_state(state.version_id, transition.destination, principal)
             # execute any side effect
             if side_effect is not None:
                 side_effect(self.context)
-            event = WorkflowTransitionEvent(self.context, self.wf,
+            event = WorkflowTransitionEvent(self.context, self.wf, principal,
                                             transition.source,
                                             transition.destination,
                                             transition, comment)
         # change state of context or new object
-        registry = get_current_registry()
+        registry = getGlobalSiteManager()
         registry.notify(event)
         # send modified event for original or new object
         if result is None:
@@ -197,18 +201,19 @@
             registry.notify(ObjectModifiedEvent(result))
         return result
 
-    def fire_transition_toward(self, state, comment=None, side_effect=None, check_security=True):
+    def fire_transition_toward(self, state, comment=None, side_effect=None, check_security=True, principal=None):
+        current_state = IWorkflowState(self.context)
         transition_ids = self.get_fireable_transition_ids_toward(state, check_security)
         if not transition_ids:
-            raise NoTransitionAvailableError(self.state(self.context).get_state(), state)
+            raise NoTransitionAvailableError(current_state.state, state)
         if len(transition_ids) != 1:
-            raise AmbiguousTransitionError(self.state(self.context).get_state(), state)
-        return self.fire_transition(transition_ids[0], comment, side_effect, check_security)
+            raise AmbiguousTransitionError(current_state.state, state)
+        return self.fire_transition(transition_ids[0], comment, side_effect, check_security, principal)
 
-    def fire_transition_for_versions(self, state, transition_id, comment=None):
+    def fire_transition_for_versions(self, state, transition_id, comment=None, principal=None):
         versions = IWorkflowVersions(self.parent)
         for version in versions.get_versions(state):
-            IWorkflowInfo(version).fire_transition(transition_id, comment)
+            IWorkflowInfo(version).fire_transition(transition_id, comment, principal=principal)
 
     def fire_automatic(self):
         for transition_id in self.get_automatic_transition_ids():