Updated annotations adapters
authorThierry Florac <thierry.florac@onf.fr>
Mon, 28 May 2018 14:05:04 +0200
changeset 91 bbd58ab513a7
parent 90 a189d7fac6d9
child 92 965ebed90c10
Updated annotations adapters
src/pyams_portal/page.py
src/pyams_portal/template.py
--- a/src/pyams_portal/page.py	Wed May 23 15:09:07 2018 +0200
+++ b/src/pyams_portal/page.py	Mon May 28 14:05:04 2018 +0200
@@ -19,14 +19,13 @@
 from pyams_portal.interfaces import IPortalPage, IPortalContext, IPortalTemplateConfiguration, \
     IPortalTemplate, PORTAL_PAGE_KEY, IPortalPortletsConfiguration, PORTLETS_CONFIGURATION_KEY, \
     ILocalTemplateHandler, LOCAL_TEMPLATE_NAME
-from zope.annotation.interfaces import IAnnotations
 from zope.traversing.interfaces import ITraversable
 
 # import packages
 from persistent import Persistent
 from pyams_portal.portlet import PortalPortletsConfiguration
 from pyams_portal.template import PortalTemplate
-from pyams_utils.adapter import adapter_config, ContextAdapter
+from pyams_utils.adapter import adapter_config, ContextAdapter, get_annotation_adapter
 from pyams_utils.registry import query_utility
 from pyramid.threadlocal import get_current_registry
 from zope.container.contained import Contained
@@ -139,13 +138,7 @@
 @adapter_config(context=IPortalContext, provides=IPortalPage)
 def portal_context_page_adapter(context):
     """Portal context page factory"""
-    annotations = IAnnotations(context)
-    page = annotations.get(PORTAL_PAGE_KEY)
-    if page is None:
-        page = annotations[PORTAL_PAGE_KEY] = PortalPage()
-        get_current_registry().notify(ObjectCreatedEvent(page))
-        locate(page, context)
-    return page
+    return get_annotation_adapter(context, PORTAL_PAGE_KEY, PortalPage)
 
 
 @adapter_config(name='template', context=IPortalContext, provides=ITraversable)
@@ -166,6 +159,10 @@
 @adapter_config(context=IPortalContext, provides=IPortalPortletsConfiguration)
 def portal_context_portlets_configuration_adapter(context):
     """Portal context portlets configuration adapter"""
+
+    def portlet_configuration_factory():
+        return PortalPortletsConfiguration.clone(portlets_config, context)
+
     # get page and template
     page = IPortalPage(context)
     if page.use_local_template:
@@ -176,10 +173,9 @@
     if page.use_local_template:
         context = template
     # get current configuration
-    annotations = IAnnotations(context)
-    config = annotations.get(PORTLETS_CONFIGURATION_KEY)
-    if config is None:
-        config = annotations[PORTLETS_CONFIGURATION_KEY] = PortalPortletsConfiguration.clone(portlets_config, context)
+    config = get_annotation_adapter(context, PORTLETS_CONFIGURATION_KEY,
+                                    factory=portlet_configuration_factory,
+                                    notify=False, locate=False)
     # check for missing portlets configuration
     for portlet_id, portlet_config in portlets_config.items():
         if portlet_id not in config:
--- a/src/pyams_portal/template.py	Wed May 23 15:09:07 2018 +0200
+++ b/src/pyams_portal/template.py	Mon May 28 14:05:04 2018 +0200
@@ -19,7 +19,6 @@
 from pyams_portal.interfaces import IPortalTemplateContainer, IPortalTemplateContainerConfiguration, \
     IPortalTemplate, IPortalTemplateConfiguration, IPortalPortletsConfiguration, IPortlet, IPortletConfiguration, \
     PORTLETS_CONFIGURATION_KEY, TEMPLATE_CONTAINER_CONFIGURATION_KEY, TEMPLATE_CONFIGURATION_KEY
-from zope.annotation.interfaces import IAnnotations
 from zope.lifecycleevent.interfaces import IObjectAddedEvent, IObjectRemovedEvent
 from zope.traversing.interfaces import ITraversable
 
@@ -29,7 +28,7 @@
 from persistent.mapping import PersistentMapping
 from pyams_portal.portlet import PortalPortletsConfiguration
 from pyams_portal.slot import SlotConfiguration
-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_local_registry, get_utility
 from pyams_utils.request import check_request
 from pyams_utils.vocabulary import vocabulary_config
@@ -38,9 +37,7 @@
 from zope.componentvocabulary.vocabulary import UtilityVocabulary
 from zope.container.contained import Contained
 from zope.container.folder import Folder
-from zope.copy import clone
 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
@@ -71,13 +68,7 @@
 @adapter_config(context=IPortalTemplateContainer, provides=IPortalTemplateContainerConfiguration)
 def portal_template_container_configuration_adapter(context):
     """Portal template container configuration factory"""
-    annotations = IAnnotations(context)
-    config = annotations.get(TEMPLATE_CONTAINER_CONFIGURATION_KEY)
-    if config is None:
-        config = annotations[TEMPLATE_CONTAINER_CONFIGURATION_KEY] = PortalTemplateContainerConfiguration()
-        get_current_registry().notify(ObjectCreatedEvent(config))
-        locate(config, context)
-    return config
+    return get_annotation_adapter(context, TEMPLATE_CONTAINER_CONFIGURATION_KEY, PortalTemplateContainerConfiguration)
 
 
 #
@@ -321,13 +312,7 @@
 @adapter_config(context=IPortalTemplate, provides=IPortalTemplateConfiguration)
 def portal_template_configuration_factory(context):
     """Portal template configuration adapter"""
-    annotations = IAnnotations(context)
-    config = annotations.get(TEMPLATE_CONFIGURATION_KEY)
-    if config is None:
-        config = annotations[TEMPLATE_CONFIGURATION_KEY] = PortalTemplateConfiguration()
-        get_current_registry().notify(ObjectCreatedEvent(config))
-        locate(config, context)
-    return config
+    return get_annotation_adapter(context, TEMPLATE_CONFIGURATION_KEY, PortalTemplateConfiguration)
 
 
 @adapter_config(name='portlet', context=IPortalTemplate, provides=ITraversable)
@@ -349,10 +334,4 @@
 @adapter_config(context=IPortalTemplate, provides=IPortalPortletsConfiguration)
 def portal_template_portlets_configuration_adapter(template):
     """Portal template portlets configuration adapter"""
-    annotations = IAnnotations(template)
-    config = annotations.get(PORTLETS_CONFIGURATION_KEY)
-    if config is None:
-        config = annotations[PORTLETS_CONFIGURATION_KEY] = PortalPortletsConfiguration()
-        get_current_registry().notify(ObjectCreatedEvent(config))
-        locate(config, template)
-    return config
+    return get_annotation_adapter(template, PORTLETS_CONFIGURATION_KEY, PortalPortletsConfiguration)