Added "display_context" property to request
authorThierry Florac <thierry.florac@onf.fr>
Thu, 18 Oct 2018 14:12:11 +0200
changeset 242 209f64c845d3
parent 241 29615ca72ab7
child 243 94a457eeaeaa
Added "display_context" property to request
src/pyams_utils/include.py
src/pyams_utils/url.py
--- a/src/pyams_utils/include.py	Fri Oct 05 15:47:00 2018 +0200
+++ b/src/pyams_utils/include.py	Thu Oct 18 14:12:11 2018 +0200
@@ -26,6 +26,7 @@
 from pyams_utils.request import RequestSelector, get_annotations, get_debug
 from pyams_utils.site import site_factory
 from pyams_utils.tales import ExtensionExpr
+from pyams_utils.url import get_display_context
 from pyams_utils.traversing import NamespaceTraverser
 
 
@@ -44,6 +45,7 @@
     # add request annotations
     config.add_request_method(get_annotations, 'annotations', reify=True)
     config.add_request_method(get_debug, 'debug', reify=True)
+    config.add_request_method(get_display_context, 'display_context', property=True)
 
     # add traverser handling namespaces via "++ns++(options)" URLs
     config.add_traverser(NamespaceTraverser)
--- a/src/pyams_utils/url.py	Fri Oct 05 15:47:00 2018 +0200
+++ b/src/pyams_utils/url.py	Thu Oct 18 14:12:11 2018 +0200
@@ -15,16 +15,14 @@
 
 # import standard library
 
-# import interfaces
-from pyams_utils.interfaces.tales import ITALESExtension
-from pyams_utils.interfaces.url import ICanonicalURL, IRelativeURL, DISPLAY_CONTEXT
+from pyramid.encode import url_quote, urlencode
+from pyramid.url import QUERY_SAFE, resource_url
+from zope.interface import Interface
 
-# import packages
-from pyams_utils.adapter import ContextRequestViewAdapter, adapter_config, ContextRequestAdapter
+from pyams_utils.adapter import ContextRequestAdapter, ContextRequestViewAdapter, adapter_config
+from pyams_utils.interfaces.tales import ITALESExtension
+from pyams_utils.interfaces.url import DISPLAY_CONTEXT, ICanonicalURL, IRelativeURL
 from pyams_utils.unicode import translate_string
-from pyramid.encode import url_quote, urlencode
-from pyramid.url import resource_url, QUERY_SAFE
-from zope.interface import Interface
 
 
 def generate_url(title):
@@ -39,6 +37,14 @@
 
 
 #
+# Request display context
+#
+
+def get_display_context(request):
+    return request.annotations.get(DISPLAY_CONTEXT, request.context)
+
+
+#
 # Absolute URLs management
 #