Use object factory in annotations adapters
authorThierry Florac <thierry.florac@onf.fr>
Thu, 17 Jan 2019 09:51:21 +0100
changeset 95 110e3c726006
parent 94 fef42b125ff9
child 96 929abe019e0f
Use object factory in annotations adapters
src/pyams_workflow/versions.py
--- a/src/pyams_workflow/versions.py	Wed Jan 16 15:43:18 2019 +0100
+++ b/src/pyams_workflow/versions.py	Thu Jan 17 09:51:21 2019 +0100
@@ -9,32 +9,28 @@
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
 #
-
 __docformat__ = 'restructuredtext'
 
-
-# import standard library
 from datetime import datetime
 
-# import interfaces
-from pyams_security.interfaces import IPrincipalInfo
-from pyams_workflow.interfaces import IWorkflowVersions, IWorkflowManagedContent, VersionError, IWorkflowState, \
-    IWorkflowVersion, IWorkflowStateHistoryItem, IWorkflowTransitionEvent, IWorkflowVersionTransitionEvent, IWorkflow
-from zope.location.interfaces import ISublocations
-from zope.traversing.interfaces import ITraversable
-
-# import packages
 from persistent import Persistent
 from persistent.list import PersistentList
 from persistent.mapping import PersistentMapping
-from pyams_utils.adapter import adapter_config, ContextAdapter, get_annotation_adapter
+from pyramid.events import subscriber
+from zope.container.folder import Folder
+from zope.interface import alsoProvides, implementer
+from zope.location.interfaces import ISublocations
+from zope.schema.fieldproperty import FieldProperty
+from zope.traversing.interfaces import ITraversable
+
+from pyams_security.interfaces import IPrincipalInfo
+from pyams_utils.adapter import ContextAdapter, adapter_config, get_annotation_adapter
+from pyams_utils.factory import factory_config
 from pyams_utils.registry import get_utility
 from pyams_utils.request import check_request, query_request
 from pyams_utils.traversing import get_parent
-from pyramid.events import subscriber
-from zope.container.folder import Folder
-from zope.interface import implementer, alsoProvides
-from zope.schema.fieldproperty import FieldProperty
+from pyams_workflow.interfaces import IWorkflow, IWorkflowManagedContent, IWorkflowState, IWorkflowStateHistoryItem, \
+    IWorkflowTransitionEvent, IWorkflowVersion, IWorkflowVersionTransitionEvent, IWorkflowVersions, VersionError
 
 
 @implementer(IWorkflowStateHistoryItem)
@@ -55,7 +51,7 @@
             setattr(self, key, value)
 
 
-@implementer(IWorkflowState)
+@factory_config(IWorkflowState)
 class WorkflowVersionState(Persistent):
     """Workflow managed content version object"""
 
@@ -99,6 +95,15 @@
         return None
 
 
+WORKFLOW_VERSION_KEY = 'pyams_workflow.version'
+
+
+@adapter_config(context=IWorkflowVersion, provides=IWorkflowState)
+def workflow_version_state_factory(context):
+    """Workflow content version state factory"""
+    return get_annotation_adapter(context, WORKFLOW_VERSION_KEY, IWorkflowState)
+
+
 @subscriber(IWorkflowTransitionEvent)
 def handle_workflow_transition(event):
     """Handle workflow transition"""
@@ -128,16 +133,7 @@
     IWorkflowState(event.object).history.append(item)
 
 
-WORKFLOW_VERSION_KEY = 'pyams_workflow.version'
-
-
-@adapter_config(context=IWorkflowVersion, provides=IWorkflowState)
-def workflow_version_state_factory(context):
-    """Workflow content version state factory"""
-    return get_annotation_adapter(context, WORKFLOW_VERSION_KEY, WorkflowVersionState)
-
-
-@implementer(IWorkflowVersions)
+@factory_config(IWorkflowVersions)
 class WorkflowVersions(Folder):
     """Workflow versions adapter"""
 
@@ -270,7 +266,7 @@
 @adapter_config(context=IWorkflowManagedContent, provides=IWorkflowVersions)
 def workflow_content_versions_factory(context):
     """Workflow versions factory"""
-    return get_annotation_adapter(context, WORKFLOW_VERSIONS_KEY, WorkflowVersions,
+    return get_annotation_adapter(context, WORKFLOW_VERSIONS_KEY, IWorkflowVersions,
                                   name='++versions++')