src/pyams_content/shared/common/__init__.py
changeset 1028 3a608029647e
parent 951 9395a088cd46
child 1060 29b1aaf9e080
--- a/src/pyams_content/shared/common/__init__.py	Fri Oct 19 17:19:36 2018 +0200
+++ b/src/pyams_content/shared/common/__init__.py	Mon Oct 22 17:10:16 2018 +0200
@@ -12,10 +12,6 @@
 
 __docformat__ = 'restructuredtext'
 
-
-# import standard library
-
-# import interfaces
 from hypatia.interfaces import ICatalog
 from persistent import Persistent
 from pyramid.events import subscriber
@@ -28,35 +24,37 @@
 from zope.intid.interfaces import IIntIds
 from zope.lifecycleevent.interfaces import IObjectModifiedEvent
 from zope.schema.fieldproperty import FieldProperty
-from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
+from zope.schema.vocabulary import SimpleTerm, SimpleVocabulary
 
-from pyams_content import _
-# import packages
 from pyams_content.features.checker import BaseContentChecker
-from pyams_content.features.checker.interfaces import IContentChecker, MISSING_VALUE, MISSING_LANG_VALUE, ERROR_VALUE
+from pyams_content.features.checker.interfaces import ERROR_VALUE, IContentChecker, MISSING_LANG_VALUE, MISSING_VALUE
 from pyams_content.features.review.interfaces import IReviewComments
-from pyams_content.interfaces import IBaseContentInfo, OWNER_ROLE, MANAGER_ROLE, CONTRIBUTOR_ROLE, READER_ROLE, \
-    GUEST_ROLE
-from pyams_content.shared.common.interfaces import IWfSharedContent, IWfSharedContentRoles, ISharedContent, \
-    IBaseSharedTool, ISharedSite, IWfSharedContentFactory, CONTENT_TYPES_VOCABULARY
+from pyams_content.interfaces import CONTRIBUTOR_ROLE, GUEST_ROLE, IBaseContentInfo, MANAGER_ROLE, OWNER_ROLE, \
+    READER_ROLE
+from pyams_content.shared.common.interfaces import CONTENT_TYPES_VOCABULARY, IBaseSharedTool, ISharedContent, \
+    ISharedSite, IWfSharedContent, IWfSharedContentFactory, IWfSharedContentRoles
 from pyams_i18n.content import I18nManagerMixin
-from pyams_i18n.interfaces import II18nManager, II18n
+from pyams_i18n.interfaces import II18n, II18nManager
 from pyams_portal.interfaces import DESIGNER_ROLE
 from pyams_security.interfaces import IDefaultProtectionPolicy
 from pyams_security.property import RolePrincipalsFieldProperty
 from pyams_security.security import ProtectedObject
 from pyams_security.utility import get_principal
-from pyams_sequence.interfaces import ISequentialIdTarget, ISequentialIdInfo
-from pyams_utils.adapter import adapter_config, ContextAdapter
+from pyams_sequence.interfaces import ISequentialIdInfo, ISequentialIdTarget
+from pyams_utils.adapter import ContextAdapter, adapter_config
 from pyams_utils.date import format_datetime
 from pyams_utils.interfaces import VIEW_PERMISSION
 from pyams_utils.property import classproperty, classproperty_support
-from pyams_utils.registry import query_utility, get_utilities_for
-from pyams_utils.request import query_request, check_request
+from pyams_utils.registry import get_utilities_for, query_utility
+from pyams_utils.request import check_request, query_request
 from pyams_utils.timezone import tztime
 from pyams_utils.traversing import get_parent
 from pyams_utils.vocabulary import vocabulary_config
-from pyams_workflow.interfaces import IWorkflowPublicationSupport, IWorkflow, IObjectClonedEvent, IWorkflowVersions
+from pyams_utils.zodb import volatile_property
+from pyams_workflow.interfaces import IObjectClonedEvent, IWorkflow, IWorkflowPublicationSupport, \
+    IWorkflowTransitionEvent, IWorkflowVersions
+
+from pyams_content import _
 
 
 @vocabulary_config(name='PyAMS shared sites')
@@ -294,6 +292,13 @@
     def workflow_name(self):
         return get_parent(self, IBaseSharedTool).shared_content_workflow
 
+    @volatile_property
+    def published_version(self):
+        workflow = IWorkflow(self)
+        versions = IWorkflowVersions(self).get_versions(workflow.published_states, sort=True)
+        if versions:
+            return versions[-1]
+
 
 @adapter_config(context=ISharedContent, provides=IBaseContentInfo)
 class SharedContentInfoAdapter(ContextAdapter):
@@ -313,3 +318,11 @@
     """Shared content workflow adapter"""
     parent = get_parent(context, IBaseSharedTool)
     return query_utility(IWorkflow, name=parent.shared_content_workflow)
+
+
+@subscriber(IWorkflowTransitionEvent)
+def handle_workflow_event(event):
+    """Reste target on workflow transition"""
+    content = get_parent(event.object, ISharedContent)
+    if content is not None:
+        del content.published_version