[Checkins] SVN: z3c.breadcrumb/ Added z3c.breadcrumb implementation

Roger Ineichen roger at projekt01.ch
Thu May 3 07:43:23 EDT 2007


Log message for revision 75050:
  Added z3c.breadcrumb implementation

Changed:
  A   z3c.breadcrumb/README.txt
  A   z3c.breadcrumb/SETUP.cfg
  A   z3c.breadcrumb/__init__.py
  A   z3c.breadcrumb/browser.py
  A   z3c.breadcrumb/configure.zcml
  A   z3c.breadcrumb/interfaces.py
  A   z3c.breadcrumb/tests.py
  A   z3c.breadcrumb/z3c.breadcrumb-configure.zcml

-=-
Added: z3c.breadcrumb/README.txt
===================================================================
--- z3c.breadcrumb/README.txt	2007-05-03 11:38:06 UTC (rev 75049)
+++ z3c.breadcrumb/README.txt	2007-05-03 11:43:23 UTC (rev 75050)
@@ -0,0 +1,117 @@
+======
+README
+======
+
+The z3c.breadcrumb package provides base classes for breadcrumb 
+implementations. It allows you to write adapters for each content object which
+provides it's own rule for providing the breadcrumb name, url and selection.
+
+Let's do some imports we will use later.
+
+  >>> import zope.interface
+  >>> import zope.component
+  >>> from zope.publisher.interfaces.http import IHTTPRequest
+  >>> from zope.publisher.browser import TestRequest
+  >>> from zope.traversing.browser import absoluteURL
+  >>> from zope.app.container import contained
+  >>> from z3c.breadcrumb import interfaces
+  >>> from z3c.breadcrumb import browser
+
+
+IBreadcrumb
+-----------
+
+Let's define a interface and a content object.
+
+  >>> class IOffice(zope.interface.Interface):
+  ...     """Office interface."""
+
+  >>> class Office(contained.Contained):
+  ...     zope.interface.implements(IOffice)
+  ...     def __init__(self, label):
+  ...         self.label = label
+  ...         self.activeURL = True
+
+  >>> office = Office(u'Zope Foundation')
+  >>> office.__name__ = u'ZF'
+
+There is a generic breadcrumb implementation which is registered by 
+default. If we do not implement a custom IBreadcrumb the generic adapter will
+return the title or __name__ of the item. Let's register the default adapter, 
+this is normaly done in configure.zcml:
+
+  >>> zope.component.provideAdapter(browser.GenericBreadcrumb)
+
+And see what we get:
+
+  >>> request = TestRequest()
+  >>> breadcrumb = zope.component.getMultiAdapter((office, request), 
+  ...     interfaces.IBreadcrumb)
+  >>> breadcrumb.name
+  u'ZF'
+
+We can also implement a custom IBreadcrumb adapter and provide another name
+for the breadcrumb name:
+
+  >>> class BreadcrumbForOffice(object):
+  ...     zope.interface.implements(interfaces.IBreadcrumb)
+  ...     zope.component.adapts(IOffice, IHTTPRequest)
+  ... 
+  ...     def __init__(self, context, request):
+  ...         self.context = context
+  ...         self.request = request
+  ... 
+  ...     @property
+  ...     def name(self):
+  ...         return self.context.label
+  ... 
+  ...     @property
+  ...     def url(self):
+  ...         return absoluteURL(self.context, self.request)
+  ... 
+  ...     @property
+  ...     def activeURL(self):
+  ...         return self.context.activeURL
+
+Let's register the custom IBreadcrumb adapter for IOffice:
+
+  >>> zope.component.provideAdapter(BreadcrumbForOffice)
+
+And check the new breadcrumb name:
+
+  >>> breadcrumb = zope.component.getMultiAdapter((office, request), 
+  ...     interfaces.IBreadcrumb)
+  >>> breadcrumb.name
+  u'Zope Foundation'
+
+
+IBreadcrumbs
+------------
+
+There is also a IBreadcrumbs adapter which knows how to collect breadcrumb 
+informations for each item he traverses. We need to setup a little bit 
+infrastucture:
+
+  >>> root = rootFolder
+  >>> root['office'] = office
+
+Register the IBreadcrumbs adapter:
+
+  >>> zope.component.provideAdapter(browser.Breadcrumbs, 
+  ...     (zope.interface.Interface, zope.interface.Interface), 
+  ...     interfaces.IBreadcrumbs)
+
+
+Now we can collect breadcrumbs for our items. You can see the url is correct
+and the label ``Zope Foundation`` is collected by the custom IBreadcrumb 
+adapter:
+
+  >>> breadcrumb = zope.component.getMultiAdapter((office, request), 
+  ...     interfaces.IBreadcrumbs)
+  >>> list(breadcrumb.crumbs)
+  [{'url': 'http://127.0.0.1',
+    'activeURL': True,
+    'name': 'top'},
+   {'url': 'http://127.0.0.1/office',
+    'activeURL': True,
+    'name': u'Zope Foundation'}]

Added: z3c.breadcrumb/SETUP.cfg
===================================================================
--- z3c.breadcrumb/SETUP.cfg	2007-05-03 11:38:06 UTC (rev 75049)
+++ z3c.breadcrumb/SETUP.cfg	2007-05-03 11:43:23 UTC (rev 75050)
@@ -0,0 +1,3 @@
+<data-files zopeskel/etc/package-includes>
+  z3c.breadcrumb-*.zcml
+</data-files>

Added: z3c.breadcrumb/__init__.py
===================================================================
--- z3c.breadcrumb/__init__.py	2007-05-03 11:38:06 UTC (rev 75049)
+++ z3c.breadcrumb/__init__.py	2007-05-03 11:43:23 UTC (rev 75050)
@@ -0,0 +1,16 @@
+##############################################################################
+#
+# Copyright (c) 2007 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""
+$Id: __init__.py 70825 2006-10-20 01:34:05Z rogerineichen $
+"""

Added: z3c.breadcrumb/browser.py
===================================================================
--- z3c.breadcrumb/browser.py	2007-05-03 11:38:06 UTC (rev 75049)
+++ z3c.breadcrumb/browser.py	2007-05-03 11:43:23 UTC (rev 75050)
@@ -0,0 +1,94 @@
+##############################################################################
+#
+# Copyright (c) 2007 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""
+$Id: __init__.py 70825 2006-10-20 01:34:05Z rogerineichen $
+"""
+__docformat__ = 'reStructuredText'
+
+import zope.component
+import zope.interface
+import zope.location
+from zope.publisher.interfaces import NotFound
+from zope.publisher import browser
+from zope.publisher.interfaces.http import IHTTPRequest
+from zope.traversing.interfaces import IContainmentRoot
+from zope.app import zapi
+from zope.app.component.interfaces import ISite
+
+from z3c.breadcrumb import interfaces
+
+
+class Breadcrumbs(zope.location.Location):
+    """Breadcrumbs implementation using IBreadcrum adapters."""
+
+    zope.interface.implements(interfaces.IBreadcrumbs)
+    zope.component.adapts(zope.interface.Interface, IHTTPRequest)
+
+    def __init__(self, context, request):
+        self.context = context
+        self.request = request
+
+    def __getParent(self):
+        return getattr(self, '_parent', self.context)
+
+    def __setParent(self, parent):
+        self._parent = parent
+
+    __parent__ = property(__getParent, __setParent)
+
+    @property
+    def crumbs(self):
+        objects = []
+        for obj in [self.context] + list(zapi.getParents(self.context)):
+            objects.append(obj)
+            if ISite.providedBy(obj):
+                break
+        objects.reverse()
+        for object in objects:
+            info = zapi.getMultiAdapter((object, self.request),
+                                        interfaces.IBreadcrumb)
+            yield {'name': info.name, 'url': info.url, 'activeURL': info.activeURL}
+
+
+class GenericBreadcrumb(object):
+    """A generic breadcrumb adapter."""
+    zope.interface.implements(interfaces.IBreadcrumb)
+    zope.component.adapts(zope.interface.Interface, IHTTPRequest)
+
+    # See interfaces.IBreadcrumb
+    activeURL = True
+
+    def __init__(self, context, request):
+        self.context = context
+        self.request = request
+
+    @property
+    def name(self):
+        """See interfaces.IBreadcrumb"""
+        name = getattr(self.context, 'title', None)
+        if name is None:
+            name = getattr(self.context, '__name__', None)
+        if name is None and IContainmentRoot.providedBy(self.context):
+            name = 'top'
+        return name
+
+    @property
+    def url(self):
+        """See interfaces.IBreadcrumb"""
+        return zapi.absoluteURL(self.context, self.request)
+
+
+def CustomNameBreadcrumb(name):
+    return type('CustomNameBreadcrumb(%r)' %name,
+                (GenericBreadcrumb,), {'name': name})

Added: z3c.breadcrumb/configure.zcml
===================================================================
--- z3c.breadcrumb/configure.zcml	2007-05-03 11:38:06 UTC (rev 75049)
+++ z3c.breadcrumb/configure.zcml	2007-05-03 11:43:23 UTC (rev 75050)
@@ -0,0 +1,11 @@
+<configure
+    xmlns="http://namespaces.zope.org/zope">
+
+  <!-- Generic IBreadcrumb adapter -->
+  <adapter
+      factory="z3c.breadcrumb.browser.GenericBreadcrumb"
+      trusted="true"
+      permission="zope.Public"
+      />
+
+</configure>

Added: z3c.breadcrumb/interfaces.py
===================================================================
--- z3c.breadcrumb/interfaces.py	2007-05-03 11:38:06 UTC (rev 75049)
+++ z3c.breadcrumb/interfaces.py	2007-05-03 11:43:23 UTC (rev 75050)
@@ -0,0 +1,50 @@
+##############################################################################
+#
+# Copyright (c) 2007 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""
+$Id: __init__.py 70825 2006-10-20 01:34:05Z rogerineichen $
+"""
+__docformat__ = 'reStructuredText'
+
+import zope.interface
+import zope.schema
+
+
+class IBreadcrumbs(zope.interface.Interface):
+    """An object providing breadcrumbs.
+
+    This object will use the ``IBreadcrumb`` adapter to get its
+    information from each breadcrumb name.
+    """
+
+    breadcrumbs = zope.interface.Attribute('An iteratable of all breadcrumbs.')
+
+
+class IBreadcrumb(zope.interface.Interface):
+    """Provides pieces of breadcrumb information about a item."""
+
+    name = zope.schema.TextLine(
+        title=u'Name',
+        description=u'The name of the breadcrumb.',
+        required=True)
+
+    url = zope.schema.URI(
+        title=u'URL',
+        description=u'The url of the breadcrumb.',
+        required=True)
+
+    active = zope.schema.Bool(
+        title=u'Active',
+        description=u'Tells whether the breadcrumb link should active.',
+        required=True,
+        default=True)

Added: z3c.breadcrumb/tests.py
===================================================================
--- z3c.breadcrumb/tests.py	2007-05-03 11:38:06 UTC (rev 75049)
+++ z3c.breadcrumb/tests.py	2007-05-03 11:43:23 UTC (rev 75050)
@@ -0,0 +1,43 @@
+##############################################################################
+#
+# Copyright (c) 2007 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""
+$Id: __init__.py 70825 2006-10-20 01:34:05Z rogerineichen $
+"""
+__docformat__ = 'restructuredtext'
+
+import unittest
+from zope.testing import doctest
+from zope.testing.doctestunit import DocFileSuite
+from zope.app.testing import setup
+from zope.app.testing import placelesssetup
+
+
+def setUp(test):
+    site = setup.placefulSetUp(site=True)
+    test.globs['rootFolder'] = site
+
+
+def tearDown(test):
+    setup.placefulTearDown()
+
+
+def test_suite():
+    return unittest.TestSuite((
+        DocFileSuite('README.txt',
+            setUp=setUp, tearDown=tearDown,
+            optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS),
+        ))
+
+if __name__ == '__main__':
+    unittest.main(defaultTest='test_suite')

Added: z3c.breadcrumb/z3c.breadcrumb-configure.zcml
===================================================================
--- z3c.breadcrumb/z3c.breadcrumb-configure.zcml	2007-05-03 11:38:06 UTC (rev 75049)
+++ z3c.breadcrumb/z3c.breadcrumb-configure.zcml	2007-05-03 11:43:23 UTC (rev 75050)
@@ -0,0 +1,5 @@
+<configure xmlns="http://namespaces.zope.org/zope">
+
+  <include package="z3c.breadcrumb" />
+
+</configure>



More information about the Checkins mailing list