Updated annotations adapters
authorThierry Florac <thierry.florac@onf.fr>
Mon, 28 May 2018 14:06:21 +0200
changeset 328 277aea3b91ec
parent 327 237378166902
child 329 fa838ff79f03
Updated annotations adapters
src/pyams_skin/configuration.py
src/pyams_skin/extension.py
--- a/src/pyams_skin/configuration.py	Fri May 25 16:14:59 2018 +0200
+++ b/src/pyams_skin/configuration.py	Mon May 28 14:06:21 2018 +0200
@@ -21,22 +21,19 @@
     IBackOfficeConfiguration, SKIN_BACK_CONFIGURATION_KEY
 from pyams_utils.interfaces.site import IConfigurationManager, IConfigurationFactory, IBackOfficeConfigurationFactory
 from pyams_utils.interfaces.tales import ITALESExtension
-from zope.annotation.interfaces import IAnnotations
 from zope.traversing.interfaces import ITraversable
 
 # import packages
 from persistent import Persistent
 from pyams_file.property import FileProperty
 from pyams_file.views.file import FileView
-from pyams_utils.adapter import ContextRequestViewAdapter, ContextAdapter, adapter_config
-from pyams_utils.request import check_request
+from pyams_utils.adapter import ContextRequestViewAdapter, ContextAdapter, adapter_config, get_annotation_adapter
+from pyams_utils.registry import get_global_registry
 from pyams_utils.traversing import get_parent
 from pyramid.httpexceptions import HTTPNotFound
-from pyramid.threadlocal import get_current_registry
 from pyramid.view import view_config
 from zope.container.contained import Contained
 from zope.interface import implementer, Interface
-from zope.location.location import locate
 from zope.schema.fieldproperty import FieldProperty
 
 
@@ -58,7 +55,7 @@
 
     @staticmethod
     def get_title_prefix(request):
-        adapter = get_current_registry().queryMultiAdapter((request.context, request), IContextTitlePrefix)
+        adapter = get_global_registry().queryMultiAdapter((request.context, request), IContextTitlePrefix)
         if adapter is not None:
             return adapter.prefix
 
@@ -66,14 +63,13 @@
 @adapter_config(context=IConfigurationManager, provides=IConfiguration)
 def configuration_factory(context):
     """Configuration factory"""
-    annotations = IAnnotations(context)
-    configuration = annotations.get(SKIN_CONFIGURATION_KEY)
-    if configuration is None:
-        registry = check_request().registry
-        factory = registry.queryAdapter(context, IConfigurationFactory, default=Configuration)
-        configuration = annotations[SKIN_CONFIGURATION_KEY] = factory()
-        locate(configuration, context, '++configuration++')
-    return configuration
+
+    def get_configuration_factory():
+        registry = get_global_registry()
+        return registry.queryAdapter(context, IConfigurationFactory, default=Configuration)
+
+    return get_annotation_adapter(context, SKIN_CONFIGURATION_KEY, factory=lambda: get_configuration_factory(),
+                                  name='++configuration++')
 
 
 @adapter_config(name='configuration', context=(Interface, Interface, Interface), provides=ITALESExtension)
@@ -126,7 +122,7 @@
 
     @staticmethod
     def get_title_prefix(request):
-        adapter = get_current_registry().queryMultiAdapter((request.context, request), IContextTitlePrefix)
+        adapter = request.registry.queryMultiAdapter((request.context, request), IContextTitlePrefix)
         if adapter is not None:
             return adapter.prefix
 
@@ -134,14 +130,13 @@
 @adapter_config(context=IConfigurationManager, provides=IBackOfficeConfiguration)
 def back_office_configuration_factory(context):
     """Back-office configuration factory"""
-    annotations = IAnnotations(context)
-    configuration = annotations.get(SKIN_BACK_CONFIGURATION_KEY)
-    if configuration is None:
-        registry = check_request().registry
-        factory = registry.queryAdapter(context, IBackOfficeConfigurationFactory, default=BackOfficeConfiguration)
-        configuration = annotations[SKIN_BACK_CONFIGURATION_KEY] = factory()
-        locate(configuration, context, '++back-configuration++')
-    return configuration
+
+    def get_configuration_factory():
+        registry = get_global_registry()
+        return registry.queryAdapter(context, IBackOfficeConfigurationFactory, default=BackOfficeConfiguration)
+
+    return get_annotation_adapter(context, SKIN_BACK_CONFIGURATION_KEY, factory=lambda: get_configuration_factory(),
+                                  name='++back-configuration++')
 
 
 @adapter_config(name='back_configuration', context=(Interface, Interface, Interface), provides=ITALESExtension)
--- a/src/pyams_skin/extension.py	Fri May 25 16:14:59 2018 +0200
+++ b/src/pyams_skin/extension.py	Mon May 28 14:06:21 2018 +0200
@@ -20,11 +20,10 @@
     IGoogleAnalyticsInfo, GOOGLE_ANALYTICS_INFO_KEY, \
     IUserReportInfo, USER_REPORT_INFO_KEY, IGoogleTagManagerInfo, GOOGLE_TAGS_INFO_KEY
 from pyams_utils.interfaces.site import ISiteRoot
-from zope.annotation.interfaces import IAnnotations
 
 # import packages
 from persistent import Persistent
-from pyams_utils.adapter import adapter_config
+from pyams_utils.adapter import adapter_config, get_annotation_adapter
 from zope.interface import implementer
 from zope.schema.fieldproperty import FieldProperty
 
@@ -41,11 +40,8 @@
 @adapter_config(context=ISiteRoot, provides=IGoogleTagManagerInfo)
 def google_tag_manager_info_factory(context):
     """Google Tag Manager settings factory"""
-    annotations = IAnnotations(context)
-    info = annotations.get(GOOGLE_TAGS_INFO_KEY)
-    if info is None:
-        info = annotations[GOOGLE_TAGS_INFO_KEY] = GoogleTagManagerInfo()
-    return info
+    return get_annotation_adapter(context, GOOGLE_TAGS_INFO_KEY, GoogleTagManagerInfo,
+                                  notify=False, locate=False)
 
 
 @implementer(IGoogleAnalyticsInfo)
@@ -61,11 +57,8 @@
 @adapter_config(context=ISiteRoot, provides=IGoogleAnalyticsInfo)
 def google_analytics_info_factory(context):
     """Google Analytics settings factory"""
-    annotations = IAnnotations(context)
-    info = annotations.get(GOOGLE_ANALYTICS_INFO_KEY)
-    if info is None:
-        info = annotations[GOOGLE_ANALYTICS_INFO_KEY] = GoogleAnalyticsInfo()
-    return info
+    return get_annotation_adapter(context, GOOGLE_ANALYTICS_INFO_KEY, GoogleAnalyticsInfo,
+                                  notify=False, locate=False)
 
 
 @implementer(IUserReportInfo)
@@ -80,8 +73,5 @@
 @adapter_config(context=ISiteRoot, provides=IUserReportInfo)
 def user_report_info_factory(context):
     """User report settings factory"""
-    annotations = IAnnotations(context)
-    info = annotations.get(USER_REPORT_INFO_KEY)
-    if info is None:
-        info = annotations[USER_REPORT_INFO_KEY] = UserReportInfo()
-    return info
+    return get_annotation_adapter(context, USER_REPORT_INFO_KEY, UserReportInfo,
+                                  notify=False, locate=False)