src/pyams_content/component/illustration/__init__.py
changeset 558 d9c6b1d7fefa
parent 555 8e8a14452567
child 591 b694d5667d17
--- a/src/pyams_content/component/illustration/__init__.py	Fri May 25 08:05:13 2018 +0200
+++ b/src/pyams_content/component/illustration/__init__.py	Fri May 25 15:48:59 2018 +0200
@@ -21,7 +21,6 @@
 from pyams_content.features.checker.interfaces import IContentChecker, MISSING_VALUE, MISSING_LANG_VALUE
 from pyams_file.interfaces import IFileInfo, IImage, IResponsiveImage
 from pyams_i18n.interfaces import INegotiator, II18n, II18nManager
-from zope.annotation.interfaces import IAnnotations
 from zope.lifecycleevent.interfaces import IObjectAddedEvent, IObjectModifiedEvent
 from zope.location.interfaces import ISublocations
 from zope.traversing.interfaces import ITraversable
@@ -31,7 +30,7 @@
 from pyams_content.features.checker import BaseContentChecker
 from pyams_content.features.renderer import RenderedContentMixin, RenderersVocabulary
 from pyams_i18n.property import I18nFileProperty
-from pyams_utils.adapter import adapter_config, ContextAdapter
+from pyams_utils.adapter import adapter_config, ContextAdapter, get_annotation_adapter
 from pyams_utils.factory import factory_config
 from pyams_utils.registry import query_utility, get_utility
 from pyams_utils.request import check_request
@@ -42,7 +41,6 @@
 from zope.container.contained import Contained
 from zope.interface import implementer, alsoProvides
 from zope.lifecycleevent import ObjectCreatedEvent, ObjectAddedEvent
-from zope.location import locate
 from zope.schema.fieldproperty import FieldProperty
 
 from pyams_content import _
@@ -75,15 +73,13 @@
 @adapter_config(context=IIllustrationTarget, provides=IIllustration)
 def illustration_factory(context):
     """Illustration factory"""
-    annotations = IAnnotations(context)
-    illustration = annotations.get(ILLUSTRATION_KEY)
-    if illustration is None:
-        illustration = annotations[ILLUSTRATION_KEY] = Illustration()
-        registry = get_current_registry()
-        registry.notify(ObjectCreatedEvent(illustration))
-        locate(illustration, context, '++illustration++')
-        registry.notify(ObjectAddedEvent(illustration, context, '++illustration++'))
-    return illustration
+
+    def illustration_callback(illustration):
+        get_current_registry().notify(ObjectAddedEvent(illustration, context, illustration.__name__))
+
+    return get_annotation_adapter(context, ILLUSTRATION_KEY, Illustration,
+                                  name='++illustration++',
+                                  callback=illustration_callback)
 
 
 def update_illustration_properties(illustration):