Skip to main content

Adapters

Default Display Name Generator

The default display name generator simply takes a Dublin Core title or a __name__ and returns it, truncated if desired. It uses a helper function intended to make writing other display name generators easier, convertName.

No help is offered yet for using HTML with the IBrowserDisplayNameGenerator interface.

Given an ILocation that can be adapted to zope.dublincore.interfaces.IDCDescriptiveProperties, and that actually has a value for it, it returns the DC title; otherwise, it uses __name__.

>>> import zope.dublincore.interfaces
>>> import zope.location.interfaces
>>> from zc.displayname import interfaces, adapters
>>> from zope.interface import verify
>>> from zope import interface
>>> class Dummy(object):
...     interface.implements(zope.location.interfaces.ILocation)
...     def __init__(self, parent, name):
...         self.__parent__ = parent
...         self.__name__ = name
...
>>> d = Dummy('parent', 'a name')
>>> from zope.publisher.browser import TestRequest
>>> g = adapters.DefaultDisplayNameGenerator(d, TestRequest())
>>> verify.verifyObject(interfaces.IDisplayNameGenerator, g)
True
>>> g()
'a name'
>>> g('foo')
Traceback (most recent call last):
...
TypeError: ('maxlength must be int', 'foo')
>>> g(-1)
Traceback (most recent call last):
...
ValueError: ('maxlength must be 0 or greater', -1)
>>> g(4)
'a...'
>>> g(6)
'a name'
>>> g(2)
'??'
>>> interface.directlyProvides(
...     d, zope.dublincore.interfaces.IDCDescriptiveProperties)
>>> d.title = 'My Special Dummy'
>>> d.description = 'My interface said I had to have this'
>>> verify.verifyObject(
...     zope.dublincore.interfaces.IDCDescriptiveProperties, d)
True
>>> g()
'My Special Dummy'
>>> g(0)
''
>>> g(100)
'My Special Dummy'
>>> g(16)
'My Special Dummy'
>>> g(15)
'My Special D...'

Breadcrumbs

zc.displayname also provides an interface and some basic adapters that provide breadcrumb functionality. This breadcrumb functionality is notably different from the standard Zope 3 breadcrumbs in two ways: the breadcrumbs are not connected to the IAbsoluteURL interface, and the breadcrumbs take advantage of the display name generator.

To use the breadcrumbs, register the appropriate adapters, and get breadcrumbs for an ILocation within a hierarchy with an IContainmentRoot as a root node.

>>> from zope.traversing.interfaces import IContainmentRoot
>>> class DummyContainmentRoot(object):
...     zope.interface.implements(IContainmentRoot)
...
>>> root = DummyContainmentRoot()
>>> child = Dummy(root, 'foo')
>>> grandchild = Dummy(child, 'bar baz bing')
>>> d.__parent__ = grandchild
>>> from zope import component
>>> component.provideAdapter(adapters.Breadcrumbs)
>>> component.provideAdapter(adapters.TerminalBreadcrumbs)
>>> component.provideAdapter(adapters.DefaultDisplayNameGenerator)
>>> component.provideAdapter(adapters.SiteDisplayNameGenerator)
>>> from zope.publisher.interfaces.http import IHTTPRequest
>>> from zope.traversing.browser.interfaces import IAbsoluteURL
>>> from zope.traversing import browser
>>> component.provideAdapter(
...     browser.AbsoluteURL, adapts=(None, IHTTPRequest),
...     provides=IAbsoluteURL)
>>> component.provideAdapter(
...     browser.SiteAbsoluteURL, adapts=(IContainmentRoot, IHTTPRequest),
...     provides=IAbsoluteURL)
>>> component.provideAdapter(
...     browser.AbsoluteURL, adapts=(None, IHTTPRequest),
...     provides=interface.Interface, name='absolute_url')
>>> component.provideAdapter(
...     browser.SiteAbsoluteURL, adapts=(IContainmentRoot, IHTTPRequest),
...     provides=interface.Interface, name='absolute_url')
>>> from zope.publisher.browser import TestRequest
>>> request = TestRequest()
>>> bc = component.getMultiAdapter((d, request), interfaces.IBreadcrumbs)
>>> import pprint
>>> pprint.pprint(bc()) # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
({'name': u'[root]',
  'name_gen': <...SiteDisplayNameGenerator...>,
  'object': <DummyContainmentRoot...>,
  'url': 'http://127.0.0.1'},
 {'name': 'foo',
  'name_gen': <...DefaultDisplayNameGenerator...>,
  'object': <Dummy...
  'url': 'http://127.0.0.1/foo'},
 {'name': 'bar baz bing',
  'name_gen': <...DefaultDisplayNameGenerator...>,
  'object': <Dummy...>,
  'url': 'http://127.0.0.1/foo/bar%20baz%20bing'},
 {'name': 'My Special Dummy',
  'name_gen': <...DefaultDisplayNameGenerator...>,
  'object': <Dummy...>,
  'url': 'http://127.0.0.1/foo/bar%20baz%20bing/a%20name'})
>>> pprint.pprint(bc(6)) # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
({'name': u'[root]',
  'name_gen': <...SiteDisplayNameGenerator...>,
  'object': <DummyContainmentRoot...>,
  'url': 'http://127.0.0.1'},
 {'name': 'foo',
  'name_gen': <...DefaultDisplayNameGenerator...>,
  'object': <Dummy...
  'url': 'http://127.0.0.1/foo'},
 {'name': 'bar...',
  'name_gen': <...DefaultDisplayNameGenerator...>,
  'object': <Dummy...>,
  'url': 'http://127.0.0.1/foo/bar%20baz%20bing'},
 {'name': 'My ...',
  'name_gen': <...DefaultDisplayNameGenerator...>,
  'object': <Dummy...>,
  'url': 'http://127.0.0.1/foo/bar%20baz%20bing/a%20name'})

The package also offers a hidden breadcrumb implementation. This enables usecases that involve objects that are in the traversal path but are for which no breadcrumbs are desired. For some sites, an IAdding is such an object. Here is an example of hiding the IAdding from the breadcrumbs.

>>> import zope.app.container.interfaces
>>> component.provideAdapter(
...     adapters.HiddenBreadcrumbs,
...     adapts=(zope.app.container.interfaces.IAdding, IHTTPRequest))
>>> from zope.app.container.browser.adding import Adding
>>> adding = Adding(d, request)
>>> bc = component.getMultiAdapter(
...     (adding, request), interfaces.IBreadcrumbs)
>>> pprint.pprint(bc()) # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
({'name': u'[root]',
  'name_gen': <...SiteDisplayNameGenerator...>,
  'object': <DummyContainmentRoot...>,
  'url': 'http://127.0.0.1'},
 {'name': 'foo',
  'name_gen': <...DefaultDisplayNameGenerator...>,
  'object': <Dummy...
  'url': 'http://127.0.0.1/foo'},
 {'name': 'bar baz bing',
  'name_gen': <...DefaultDisplayNameGenerator...>,
  'object': <Dummy...>,
  'url': 'http://127.0.0.1/foo/bar%20baz%20bing'},
 {'name': 'My Special Dummy',
  'name_gen': <...DefaultDisplayNameGenerator...>,
  'object': <Dummy...>,
  'url': 'http://127.0.0.1/foo/bar%20baz%20bing/a%20name'})

For ease of use within page templates, the adapters module also offers two views that can be used to get breadcrumbs directly. They simply get the adapter and call it, either with no argument, or with the maxlength argument of 20. Register one of these, or another one with a similar pattern, for your application’s breadcrumbs view.

>>> pprint.pprint(adapters.breadcrumbs(adding, request))
... # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
({'name': u'[root]',
  'name_gen': <...SiteDisplayNameGenerator...>,
  'object': <DummyContainmentRoot...>,
  'url': 'http://127.0.0.1'},
 {'name': 'foo',
  'name_gen': <...DefaultDisplayNameGenerator...>,
  'object': <Dummy...
  'url': 'http://127.0.0.1/foo'},
 {'name': 'bar baz bing',
  'name_gen': <...DefaultDisplayNameGenerator...>,
  'object': <Dummy...>,
  'url': 'http://127.0.0.1/foo/bar%20baz%20bing'},
 {'name': 'My Special Dummy',
  'name_gen': <...DefaultDisplayNameGenerator...>,
  'object': <Dummy...>,
  'url': 'http://127.0.0.1/foo/bar%20baz%20bing/a%20name'})
>>> d.title = "My Special Dummy With a Really Long Title"
>>> pprint.pprint(adapters.breadcrumbs20char(adding, request))
... # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
({'name': u'[root]',
  'name_gen': <...SiteDisplayNameGenerator...>,
  'object': <DummyContainmentRoot...>,
  'url': 'http://127.0.0.1'},
 {'name': 'foo',
  'name_gen': <...DefaultDisplayNameGenerator...>,
  'object': <Dummy...
  'url': 'http://127.0.0.1/foo'},
 {'name': 'bar baz bing',
  'name_gen': <...DefaultDisplayNameGenerator...>,
  'object': <Dummy...>,
  'url': 'http://127.0.0.1/foo/bar%20baz%20bing'},
 {'name': 'My Special Dummy ...',
  'name_gen': <...DefaultDisplayNameGenerator...>,
  'object': <Dummy...>,
  'url': 'http://127.0.0.1/foo/bar%20baz%20bing/a%20name'})

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

zc.displayname-1.1.tar.gz (8.4 kB view details)

Uploaded Source

File details

Details for the file zc.displayname-1.1.tar.gz.

File metadata

  • Download URL: zc.displayname-1.1.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for zc.displayname-1.1.tar.gz
Algorithm Hash digest
SHA256 146cd8bacd9215d6c27a78ccfe4eef1df3b9c66e456c1e458afed83a805c33c3
MD5 68bf950b0c2e7e8d4bd890b57e034f62
BLAKE2b-256 cf3fd70c09ded3ffa3019a0d82aa3ba2bd98b77605e6e8fbce70631542f3e0a4

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.1 This release

1 file

1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page