Added IBreadcrumbs interface and default multi-adapter
authorThierry Florac <thierry.florac@onf.fr>
Mon, 26 Nov 2018 11:00:18 +0100
changeset 468 8c6bd0ac6424
parent 467 c571dce459a3
child 469 46e128be7e1c
Added IBreadcrumbs interface and default multi-adapter
src/pyams_skin/interfaces/viewlet.py
src/pyams_skin/viewlet/breadcrumb/__init__.py
--- a/src/pyams_skin/interfaces/viewlet.py	Tue Nov 20 09:09:31 2018 +0100
+++ b/src/pyams_skin/interfaces/viewlet.py	Mon Nov 26 11:00:18 2018 +0100
@@ -219,6 +219,12 @@
 # Breadcrumbs interfaces
 #
 
+class IBreadcrumbs(Interface):
+    """Main breadcrumbs interface"""
+
+    items = Attribute("Breadcrumbs items iterator attribute")
+
+
 class IBreadcrumbItem(Interface):
     """Breadcrumb item interface"""
 
--- a/src/pyams_skin/viewlet/breadcrumb/__init__.py	Tue Nov 20 09:09:31 2018 +0100
+++ b/src/pyams_skin/viewlet/breadcrumb/__init__.py	Mon Nov 26 11:00:18 2018 +0100
@@ -12,21 +12,19 @@
 
 __docformat__ = 'restructuredtext'
 
-
-# import standard library
+from pyramid.location import lineage
+from zope.contentprovider.interfaces import IContentProvider
+from zope.interface import implementer, Interface
+from zope.location import ILocation
+from zope.schema.fieldproperty import FieldProperty
 
-# import interfaces
-from pyams_skin.interfaces.viewlet import IBreadcrumbItem
+from pyams_skin.interfaces.viewlet import IBreadcrumbItem, IBreadcrumbs
 from pyams_skin.layer import IPyAMSLayer
-
-# import packages
 from pyams_template.template import template_config
-from pyams_utils.adapter import ContextRequestAdapter
+from pyams_utils.adapter import ContextRequestAdapter, adapter_config, ContextRequestViewAdapter
+from pyams_utils.interfaces.tales import ITALESExtension
 from pyams_utils.url import absolute_url
 from pyams_viewlet.viewlet import contentprovider_config, ViewContentProvider
-from pyramid.location import lineage
-from zope.interface import implementer, Interface
-from zope.schema.fieldproperty import FieldProperty
 
 
 @implementer(IBreadcrumbItem)
@@ -48,6 +46,26 @@
     view_name = 'admin'
 
 
+@adapter_config(context=(ILocation, IPyAMSLayer, Interface), provides=IBreadcrumbs)
+class BreadcrumbsAdapter(ContextRequestViewAdapter):
+    """Generic breadcrumbs adapter"""
+
+    def get_items(self, source):
+        registry = self.request.registry
+        for context in reversed(tuple(lineage(source))):
+            item = registry.queryMultiAdapter((context, self.request, self.view), IBreadcrumbItem)
+            if item is None:
+                item = registry.queryMultiAdapter((context, self.request), IBreadcrumbItem)
+                if item is None:
+                    item = registry.queryAdapter(context, IBreadcrumbItem)
+            if item is not None:
+                yield item
+
+    @property
+    def items(self):
+        yield from self.get_items(self.context)
+
+
 @contentprovider_config(name='pyams.breadcrumbs', layer=IPyAMSLayer, view=Interface)
 @template_config(template='breadcrumbs.pt', layer=IPyAMSLayer)
 class BreadcrumbsContentProvider(ViewContentProvider):
@@ -55,12 +73,22 @@
 
     @property
     def items(self):
-        registry = self.request.registry
-        for context in reversed(tuple(lineage(self.context))):
-            item = registry.queryMultiAdapter((context, self.request, self.view), IBreadcrumbItem)
-            if item is None:
-                item = registry.queryMultiAdapter((context, self.request), IBreadcrumbItem)
-                if item is None:
-                    item = registry.queryAdapter(context, IBreadcrumbItem)
-            if item is not None:
-                yield item
+        breadcrumbs = self.request.registry.queryMultiAdapter((self.context, self.request, self.view), IBreadcrumbs)
+        if breadcrumbs is not None:
+            yield from breadcrumbs.items
+
+
+@adapter_config(name='breadcrumbs', context=(Interface, Interface, Interface), provides=ITALESExtension)
+class BreadcrumbsTalesExtension(ContextRequestViewAdapter):
+    """tales:breadcrumbs(context) TALES extension"""
+
+    def render(self, context=None):
+        if context is None:
+            context = self.context
+        provider = self.request.registry.queryMultiAdapter((context, self.request, self.view), IContentProvider,
+                                                           name='pyams.breadcrumbs')
+        if provider is not None:
+            provider.update()
+            return provider.render()
+        else:
+            return ''