Added subscriber for cloned object event
authorThierry Florac <thierry.florac@onf.fr>
Wed, 20 May 2015 12:44:49 +0200
changeset 6 6f4862c48019
parent 5 43c135a76ab2
child 7 3991ed2e1855
Added subscriber for cloned object event
src/pyams_workflow/content.py
--- a/src/pyams_workflow/content.py	Wed May 20 12:44:10 2015 +0200
+++ b/src/pyams_workflow/content.py	Wed May 20 12:44:49 2015 +0200
@@ -19,7 +19,7 @@
 # import interfaces
 from pyams_security.interfaces import IPrincipalInfo
 from pyams_workflow.interfaces import IWorkflowManagedContent, IWorkflowPublicationInfo, IWorkflow, IWorkflowVersions, \
-    IWorkflowPublicationSupport, IObjectClonedEvent
+    IWorkflowPublicationSupport, IObjectClonedEvent, IWorkflowState
 from zope.annotation.interfaces import IAnnotations
 
 # import packages
@@ -29,6 +29,7 @@
 from pyams_utils.request import check_request
 from pyams_utils.timezone import gmtime
 from pyams_utils.traversing import get_parent
+from pyams_workflow.versions import WorkflowHistoryItem
 from pyramid.events import subscriber
 from pyramid.threadlocal import get_current_registry
 from zope.container.contained import Contained
@@ -37,6 +38,8 @@
 from zope.location import locate
 from zope.schema.fieldproperty import FieldProperty
 
+from pyams_workflow import _
+
 
 @implementer(IWorkflowPublicationInfo)
 class WorkflowContentPublicationInfo(Persistent, Contained):
@@ -160,6 +163,21 @@
     return info
 
 
+@subscriber(IObjectClonedEvent)
+def handle_cloned_object(event):
+    """Add comment when an object is cloned"""
+    request = check_request()
+    translate = request.localizer.translate
+    source_state = IWorkflowState(event.source)
+    target_state = IWorkflowState(event.object)
+    item = WorkflowHistoryItem(date=datetime.utcnow(),
+                               source_state=source_state.state,
+                               principal=request.principal.id,
+                               comment=translate(_("Clone created from version {source}")).format(
+                                   source=source_state.version_id))
+    target_state.history.append(item)
+
+
 @subscriber(IObjectClonedEvent, context_selector=IWorkflowPublicationSupport)
 def handle_cloned_publication_support(event):
     """Reset publication info when an object is cloned"""