Updated annotations adapters
authorThierry Florac <thierry.florac@onf.fr>
Mon, 28 May 2018 14:07:28 +0200
changeset 71 f782ba6a001b
parent 70 09800bab28db
child 72 b24cc8efb9df
Updated annotations adapters
src/pyams_workflow/content.py
src/pyams_workflow/versions.py
--- a/src/pyams_workflow/content.py	Mon May 28 12:38:57 2018 +0200
+++ b/src/pyams_workflow/content.py	Mon May 28 14:07:28 2018 +0200
@@ -22,12 +22,11 @@
 from pyams_workflow.interfaces import IWorkflowManagedContent, IWorkflowPublicationInfo, IWorkflow, IWorkflowVersions, \
     IWorkflowPublicationSupport, IObjectClonedEvent, IWorkflowState, VersionError, VERSION_DISPLAY, \
     DISPLAY_FIRST_VERSION, DISPLAY_CURRENT_VERSION
-from zope.annotation.interfaces import IAnnotations
 from zope.dublincore.interfaces import IZopeDublinCore
 
 # import packages
 from persistent import Persistent
-from pyams_utils.adapter import adapter_config
+from pyams_utils.adapter import adapter_config, get_annotation_adapter
 from pyams_utils.date import format_datetime, format_date, SH_DATE_FORMAT
 from pyams_utils.registry import get_utility
 from pyams_utils.request import check_request
@@ -36,11 +35,8 @@
 from pyams_utils.vocabulary import vocabulary_config
 from pyams_workflow.versions import WorkflowHistoryItem
 from pyramid.events import subscriber
-from pyramid.threadlocal import get_current_registry
 from zope.container.contained import Contained
 from zope.interface import implementer
-from zope.lifecycleevent import ObjectCreatedEvent
-from zope.location import locate
 from zope.schema.fieldproperty import FieldProperty
 from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
 
@@ -200,14 +196,7 @@
 @adapter_config(context=IWorkflowPublicationSupport, provides=IWorkflowPublicationInfo)
 def workflow_content_publication_info_factory(context):
     """Workflow content info factory"""
-    annotations = IAnnotations(context)
-    info = annotations.get(WORKFLOW_CONTENT_KEY)
-    if info is None:
-        info = annotations[WORKFLOW_CONTENT_KEY] = WorkflowContentPublicationInfo()
-        registry = get_current_registry()
-        registry.notify(ObjectCreatedEvent(info))
-        locate(info, context)
-    return info
+    return get_annotation_adapter(context, WORKFLOW_CONTENT_KEY, WorkflowContentPublicationInfo)
 
 
 @subscriber(IObjectClonedEvent)
--- a/src/pyams_workflow/versions.py	Mon May 28 12:38:57 2018 +0200
+++ b/src/pyams_workflow/versions.py	Mon May 28 14:07:28 2018 +0200
@@ -20,7 +20,6 @@
 from pyams_security.interfaces import IPrincipalInfo
 from pyams_workflow.interfaces import IWorkflowVersions, IWorkflowManagedContent, VersionError, IWorkflowState, \
     IWorkflowVersion, IWorkflowStateHistoryItem, IWorkflowTransitionEvent, IWorkflowVersionTransitionEvent, IWorkflow
-from zope.annotation.interfaces import IAnnotations
 from zope.location.interfaces import ISublocations
 from zope.traversing.interfaces import ITraversable
 
@@ -28,16 +27,13 @@
 from persistent import Persistent
 from persistent.list import PersistentList
 from persistent.mapping import PersistentMapping
-from pyams_utils.adapter import adapter_config, ContextAdapter
+from pyams_utils.adapter import adapter_config, ContextAdapter, get_annotation_adapter
 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 pyramid.threadlocal import get_current_registry
 from zope.container.folder import Folder
 from zope.interface import implementer, alsoProvides
-from zope.lifecycleevent import ObjectCreatedEvent
-from zope.location import locate
 from zope.schema.fieldproperty import FieldProperty
 
 
@@ -138,13 +134,7 @@
 @adapter_config(context=IWorkflowVersion, provides=IWorkflowState)
 def workflow_version_state_factory(context):
     """Workflow content version state factory"""
-    annotations = IAnnotations(context)
-    state = annotations.get(WORKFLOW_VERSION_KEY)
-    if state is None:
-        state = annotations[WORKFLOW_VERSION_KEY] = WorkflowVersionState()
-        registry = get_current_registry()
-        registry.notify(ObjectCreatedEvent(state))
-    return state
+    return get_annotation_adapter(context, WORKFLOW_VERSION_KEY, WorkflowVersionState)
 
 
 @implementer(IWorkflowVersions)
@@ -280,14 +270,8 @@
 @adapter_config(context=IWorkflowManagedContent, provides=IWorkflowVersions)
 def workflow_content_versions_factory(context):
     """Workflow versions factory"""
-    annotations = IAnnotations(context)
-    versions = annotations.get(WORKFLOW_VERSIONS_KEY)
-    if versions is None:
-        versions = annotations[WORKFLOW_VERSIONS_KEY] = WorkflowVersions()
-        registry = get_current_registry()
-        registry.notify(ObjectCreatedEvent(versions))
-        locate(versions, context, '++versions++')
-    return versions
+    return get_annotation_adapter(context, WORKFLOW_VERSIONS_KEY, WorkflowVersions,
+                                  name='++versions++')
 
 
 @adapter_config(context=IWorkflowVersion, provides=IWorkflowVersions)