Package: z3c.menu.ready2go
This package provides a “ready-to-go” menu implementation based on viewlets for
Zope 3.ContentsReady 2 go MenuGlobal MenuGlobal Menu ItemSite Menu ItemContext Menu ItemAddMenuMenu groupsEmptyMenuManagerSpecial use caseZ3C Menu directivesCHANGES2.0 (2023-02-07)1.1.0 (2018-10-09)1.0.0 (2017-04-17)1.0.0a1 (2013-03-03)0.8.0 (2010-07-12)0.7.1 (2009-12-26)0.7.0 (2009-11-30)0.6.0 (2009-02-07)0.5.1 (2009-01-04)0.5.0 (2008-04-11)Ready 2 go MenuThe z3c.menu.ready2go package provides a menu implementation which allows you
to implement menus based on content providers and viewlets.First let’s setup our defualt menu item template:>>> import os
>>> import zope.component
>>> from zope.publisher.interfaces.browser import IDefaultBrowserLayer
>>> from zope.publisher.interfaces.browser import IBrowserView
>>> from z3c.template.interfaces import IContentTemplate
>>> from z3c.template.template import TemplateFactory
>>> import z3c.menu.ready2go
>>> baseDir = os.path.split(z3c.menu.ready2go.__file__)[0]
>>> itemTemplate = os.path.join(baseDir, 'item.pt')
>>> itemTemplateFactory = TemplateFactory(itemTemplate, 'text/html')
>>> zope.component.provideAdapter(itemTemplateFactory,
...     (IBrowserView, IDefaultBrowserLayer), IContentTemplate)Global MenuLet’s create some menu and register them as viewlet manager:>>> from zope.viewlet.interfaces import IViewlet
>>> from zope.viewlet import manager
>>> from z3c.menu.ready2go import interfaces
>>> from z3c.menu.ready2go import IGlobalMenu
>>> from z3c.menu.ready2go import ISiteMenu
>>> from z3c.menu.ready2go import IContextMenu
>>> from z3c.menu.ready2go import IAddMenu
>>> from z3c.menu.ready2go.manager import MenuManagerAnd we configure our menu item as viewlet Managers. This is normaly done by theviewletManagerZCML directive:>>> GlobalMenu = manager.ViewletManager('left', IGlobalMenu,
...     bases=(MenuManager,))>>> SiteMenu = manager.ViewletManager('left', ISiteMenu,
...     bases=(MenuManager,))>>> ContextMenu = manager.ViewletManager('left', IContextMenu,
...     bases=(MenuManager,))>>> AddMenu = manager.ViewletManager('left', IAddMenu,
...     bases=(MenuManager,))Our menu managers implement IMenuManager:>>> interfaces.IMenuManager.implementedBy(GlobalMenu)
True>>> interfaces.IMenuManager.implementedBy(SiteMenu)
True>>> interfaces.IMenuManager.implementedBy(ContextMenu)
True>>> interfaces.IMenuManager.implementedBy(AddMenu)
TrueWe also need our checker adapter which can check if a menu item is available
and/or selected:>>> from z3c.menu.ready2go import checker
>>> zope.component.provideAdapter(checker.GlobalSelectedChecker)
>>> zope.component.provideAdapter(checker.SiteSelectedChecker)
>>> zope.component.provideAdapter(checker.ContextSelectedChecker)Now we have to define a site and a context:>>> import zope.interface
>>> from zope.container import contained, btree
>>> from zope.location.interfaces import IContained
>>> from zope.component.interfaces import IPossibleSite
>>> from zope.site.site import SiteManagerContainer
>>> from zope.site.site import LocalSiteManager>>> @zope.interface.implementer(IPossibleSite)
... class Site(btree.BTreeContainer, SiteManagerContainer):
...     def __init__(self):
...         super(Site, self).__init__()
...         self.setSiteManager(LocalSiteManager(self))>>> @zope.interface.implementer(IContained)
... class Content(contained.Contained):
...     pass>>> root['site'] = Site()
>>> site = root['site']Now we have to set the site object as site. This is normaly done by the
traverser but we do this here with the hooks helper because we do not really
traaverse to the site within the publisher/traverser:>>> from zope.component import hooks
>>> hooks.setSite(site)>>> site['content'] = Content()
>>> content = site['content']>>> from zope.publisher.browser import TestRequest
>>> request = TestRequest()And we need a view which knows about it’s parent:>>> @zope.interface.implementer(IBrowserView)
... class View(contained.Contained):
...
...     def __init__(self, context, request):
...         self.__parent__ = context
...         self.context = context
...         self.request = request>>> view = View(content, request)Our menus can adapt the context, request and view. See IViewletManager in
zope.viewlet for more infos about this pattern. If we render them, there is an
empty string returned. This means the menus don’t find menu items for rendering:>>> globalMenu = GlobalMenu(content, request, view)
>>> globalMenu.update()
>>> globalMenu.render()
''>>> siteMenu = SiteMenu(content, request, view)
>>> siteMenu.update()
>>> siteMenu.render()
''>>> contextMenu = ContextMenu(content, request, view)
>>> contextMenu.update()
>>> contextMenu.render()
''>>> addMenu = AddMenu(content, request, view)
>>> addMenu.update()
>>> addMenu.render()
''Global Menu ItemNow we register a context menu item for our IGlobalMenu:>>> from z3c.menu.ready2go.item import GlobalMenuItem
>>> class MyGlobalMenuItem(GlobalMenuItem):
...
...     viewName = 'root.html'Now we need a security checker for our menu item>>> from zope.security.checker import NamesChecker, defineChecker
>>> viewletChecker = NamesChecker(('update', 'render'))
>>> defineChecker(MyGlobalMenuItem, viewletChecker)And we configure our menu item for IGlobalMenu. This is normaly done by theviewletZCML directive:>>> zope.component.provideAdapter(
...     MyGlobalMenuItem,
...     (zope.interface.Interface, IDefaultBrowserLayer,
...     IBrowserView, IGlobalMenu),
...     IViewlet, name='My Global')Now let’s update the menu manager and see that this manager now contains
the menu item:>>> globalMenu.update()
>>> myGlobalMenuItem = globalMenu.viewlets[0]
>>> myGlobalMenuItem
<MyGlobalMenuItem 'My Global'>Now let’s render the global menu manager and you can see that the menu item
get rendered:>>> print(globalMenu.render())
<li>
  <a href="http://127.0.0.1/root.html"><span>My Global</span></a>
</li>Site Menu ItemNow we register a context menu item for our ISiteMenu:>>> import zope.component
>>> from zope.publisher.interfaces.browser import IDefaultBrowserLayer>>> from z3c.menu.ready2go.item import SiteMenuItem
>>> class MySiteMenuItem(SiteMenuItem):
...
...     viewName = 'site.html'Now we need a security checker for our menu item>>> from zope.security.checker import NamesChecker, defineChecker
>>> viewletChecker = NamesChecker(('update', 'render'))
>>> defineChecker(MySiteMenuItem, viewletChecker)And we configure our menu item for ISiteMenu. This is normaly done by theviewletZCML directive:>>> zope.component.provideAdapter(
...     MySiteMenuItem,
...     (zope.interface.Interface, IDefaultBrowserLayer,
...     IBrowserView, ISiteMenu),
...     IViewlet, name='My Site')Now let’s render the site menu again. You can see that we ve got a menu item
and the url points to our site:>>> siteMenu.update()
>>> print(siteMenu.render())
<li>
  <a href="http://127.0.0.1/site/site.html"><span>My Site</span></a>
</li>Context Menu ItemNow we register a context menu item for our IContextMenu:>>> import zope.component
>>> from zope.publisher.interfaces.browser import IDefaultBrowserLayer>>> from z3c.menu.ready2go.item import ContextMenuItem
>>> class MyContextMenuItem(ContextMenuItem):
...     viewName = 'context.html'
...     weight = 1Now we need a security checker for our menu item>>> from zope.security.checker import NamesChecker, defineChecker
>>> viewletChecker = NamesChecker(('update', 'render'))
>>> defineChecker(MyContextMenuItem, viewletChecker)And we configure our menu item for IContextMenu. This is normaly done by theviewletZCML directive:>>> zope.component.provideAdapter(
...     MyContextMenuItem,
...     (zope.interface.Interface, IDefaultBrowserLayer,
...     IBrowserView, IContextMenu),
...     IViewlet, name='My Context')Now let’s render the context menu again. You can see that we ve got a menu
item. Another important point here is, that the url of such ContextMemuItem
implementations point to the context of the view:>>> contextMenu.update()
>>> print(contextMenu.render())
<li>
  <a href="http://127.0.0.1/site/content/context.html"><span>My Context</span></a>
</li>Let’s set the view  __name__ tocontext.html. This will reflect that
the view offers the same name that our context menu needs to get rendered as
selected:>>> view.__name__ = 'context.html'Now try again and see if the context menu item get rendered as selected:>>> contextMenu.update()
>>> print(contextMenu.render())
<li class="selected">
  <a href="http://127.0.0.1/site/content/context.html"><span>My Context</span></a>
</li>Also, let’s check that menu item is marked selected even if we provided a viewName in
the@@context.htmlform:>>> MyContextMenuItem.viewName = '@@context.html'
>>> contextMenu.update()
>>> print(contextMenu.render())
<li class="selected">
  <a href="http://127.0.0.1/site/content/@@context.html"><span>My Context</span></a>
</li>Okay, change viewName back tocontext.htmlfor further tests:>>> MyContextMenuItem.viewName = 'context.html'Now add a second context menu item and check if we can use the cssInActive
argument which is normaly a empty string:>>> class InActiveMenuItem(ContextMenuItem):
...     viewName = 'inActive.html'
...     cssInActive = 'inActive'
...     weight = 2>>> defineChecker(InActiveMenuItem, viewletChecker)>>> zope.component.provideAdapter(
...     InActiveMenuItem,
...     (zope.interface.Interface, IDefaultBrowserLayer,
...     IBrowserView, IContextMenu),
...     IViewlet, name='In Active')Now update and render again:>>> contextMenu.update()
>>> print(contextMenu.render())
<li class="selected">
  <a href="http://127.0.0.1/site/content/context.html"><span>My Context</span></a>
</li>
<li class="inActive">
  <a href="http://127.0.0.1/site/content/inActive.html"><span>In Active</span></a>
</li>AddMenuThe add menu can be used for offering links to any kind of add forms per
context. This allows us to offer independent add form links doesn’t matter which
form framework is used. Let’s now define such a simple AddMenuItem pointing
to a add form url. Not; the add form and it’s url do not exist in thsi test.
This aslo means there is no guarantee that a form exist if a add menu item
is configured.>>> from z3c.menu.ready2go.item import AddMenuItem
>>> class MyAddMenuItem(AddMenuItem):
...
...     viewName = 'addSomething.html'Now we need a security checker for our menu item>>> from zope.security.checker import NamesChecker, defineChecker
>>> viewletChecker = NamesChecker(('update', 'render'))
>>> defineChecker(MyAddMenuItem, viewletChecker)And we configure our menu item for IAddMenu. This is normaly done by theviewletZCML directive:>>> zope.component.provideAdapter(
...     MyAddMenuItem,
...     (zope.interface.Interface, IDefaultBrowserLayer,
...     IBrowserView, IAddMenu),
...     IViewlet, name='My AddMenu')Now we can update and render our add menu:>>> addMenu.update()
>>> print(addMenu.render())
<li>
  <a href="http://127.0.0.1/site/content/addSomething.html"><span>My AddMenu</span></a>
</li>Menu groupsThe global and the site menu items are grouped menu items. This means such menu
items should get rendered as selected if a context menu item is selected. This
reflects the menu hierarchie. Let’s show how we can solve this not so simple
problem. We offer a ISelectedChecker adapter which can decide if a menu get
rendered as selected or not. This is very usefull because normaly a menu get
registered and later we add views and can not change the menu item
implementation. Let’s see how such an adapter can handle an existing menu,
context and view setup and change the selected rendering. We register a
selected checker for our site menu item:>>> zope.component.provideAdapter(checker.TrueSelectedChecker,
...     (IContained, IDefaultBrowserLayer, None, ISiteMenu, MySiteMenuItem),
...     interfaces.ISelectedChecker)Now we can render the site menu again. Note that our context is still the
sample content object.>>> siteMenu.update()
>>> print(siteMenu.render())
<li class="selected">
  <a href="http://127.0.0.1/site/site.html"><span>My Site</span></a>
</li>This reflects that the site menu is a group menu which the context menu item
of the content object is selected too.>>> contextMenu.update()
>>> print(contextMenu.render())
<li class="selected">
  <a href="http://127.0.0.1/site/content/context.html"><span>My Context</span></a>
</li>
<li class="inActive">
  <a href="http://127.0.0.1/site/content/inActive.html"><span>In Active</span></a>
</li>EmptyMenuManagerThere is a empty menu manager whihc could be used for override existing
menu managers.>>> from z3c.menu.ready2go.manager import EmptyMenuManager
>>> emptyMenu = EmptyMenuManager(None, None, None)Our empty menu manager implementsIMenuManager:>>> interfaces.IMenuManager.providedBy(emptyMenu)
TrueThis empty menu manager returns allways an empty string if we render them:>>> emptyMenu.update()
>>> emptyMenu.render()
''Special use caseWe have some special use case because of Zope’s internals. One important part
is that our menu heavy depend on context and it’s __parent__ chain to the
zope application root. This is not allways supported by Zopes default setup.
One part is the bad integrated application control part which fakes a root
object which doesn’t know about the real childs of the real root from the
ZODB e.g. application root. Now we will show you that our menu by default
render no items if we get such a fake root which messes up our menu structure.Let’s define a object which does not know about any __parent__.>>> nirvana = Content()
>>> nirvanaView = View(nirvana, request)Now we can check what’s happen to the menus if we adapt the parent less nirvana
context and update and render the menus. You can see that the global menu does
not contain any menu item. That’s because the global menu items tries to find
the root by traversing from the context to the root by the __parent__ chain
and we don’t support any parent for our nirvana object:>>> globalMenu = GlobalMenu(nirvana, request, nirvanaView)
>>> globalMenu.update()
>>> globalMenu.render()
''Also the SiteMenu doesn’t contain any menu item because of the parent less
object:>>> siteMenu = SiteMenu(nirvana, request, nirvanaView)
>>> siteMenu.update()
>>> siteMenu.render()
''>>> contextMenu = ContextMenu(nirvana, request, nirvanaView)
>>> contextMenu.update()
>>> contextMenu.render()
''>>> addMenu = AddMenu(nirvana, request, nirvanaView)
>>> addMenu.update()
>>> addMenu.render()
''Z3C Menu directivesShow how we can use the menu directive. Register the meta configuration for
the directive.First let’s setup our defualt menu item template first:>>> import os
>>> import zope.component
>>> from zope.publisher.interfaces.browser import IDefaultBrowserLayer
>>> from zope.publisher.interfaces.browser import IBrowserView
>>> from z3c.template.interfaces import IContentTemplate
>>> from z3c.template.template import TemplateFactory
>>> import z3c.menu.ready2go
>>> baseDir = os.path.split(z3c.menu.ready2go.__file__)[0]
>>> itemTemplate = os.path.join(baseDir, 'item.pt')
>>> itemTemplateFactory = TemplateFactory(itemTemplate, 'text/html')
>>> zope.component.provideAdapter(itemTemplateFactory,
...     (IBrowserView, IDefaultBrowserLayer), IContentTemplate)>>> import sys
>>> from zope.configuration import xmlconfig
>>> import z3c.menu.ready2go
>>> context = xmlconfig.file('meta.zcml', z3c.menu.ready2go)We need to register our checker adapter which can check if a menu item is
selected or not:>>> import zope.component
>>> from z3c.menu.ready2go import checker
>>> zope.component.provideAdapter(checker.ContextSelectedChecker)Let’s define a content object:>>> from z3c.menu.ready2go import testing
>>> sampleContent = testing.Sample('Sample Content')Now add the content object to our site root:>>> root['sample'] = sampleContentNow we can define our test menu manager:>>> from zope.viewlet.manager import ViewletManager
>>> from z3c.menu.ready2go import manager
>>> FirstMenu = ViewletManager('left', testing.IFirstMenu,
...     bases=(manager.MenuManager,))>>> SecondMenu = ViewletManager('left', testing.ISecondMenu,
...     bases=(manager.MenuManager,))And we need a view which knows about it’s parent:>>> from zope.publisher.browser import TestRequest
>>> request = TestRequest()
>>> firstView = testing.FirstView(sampleContent, request)
>>> testing.IFirstView.providedBy(firstView)
True>>> secondView = testing.SecondView(sampleContent, request)
>>> testing.ISecondView.providedBy(secondView)
TrueAs you can see the menu is not selected if we access the page:>>> firstMenu = FirstMenu(sampleContent, request, firstView)
>>> testing.IFirstMenu.providedBy(firstMenu)
True>>> firstMenu.update()
>>> firstMenu.render()
''>>> secondMenu = SecondMenu(sampleContent, request, secondView)
>>> testing.ISecondMenu.providedBy(secondMenu)
True>>> secondMenu.update()
>>> secondMenu.render()
''Now we need some menu items for the first menu:>>> from zope.publisher.interfaces.browser import IBrowserView
>>> from zope.publisher.interfaces.browser import IBrowserRequest
>>> from zope.viewlet.interfaces import IViewlet
>>> zope.component.provideAdapter(
...     testing.FirstMenuItem,
...     (zope.interface.Interface, IBrowserRequest,
...     IBrowserView, testing.IFirstMenu),
...     IViewlet, name='First Menu')>>> zope.component.provideAdapter(
...     testing.SecondMenuItem,
...     (zope.interface.Interface, IBrowserRequest,
...     IBrowserView, testing.IFirstMenu),
...     IViewlet, name='Second Menu')And we need some menu items for the second menu:>>> zope.component.provideAdapter(
...     testing.FirstMenuItem,
...     (zope.interface.Interface, IBrowserRequest,
...     IBrowserView, testing.ISecondMenu),
...     IViewlet, name='First Menu')>>> zope.component.provideAdapter(
...     testing.SecondMenuItem,
...     (zope.interface.Interface, IBrowserRequest,
...     IBrowserView, testing.ISecondMenu),
...     IViewlet, name='Second Menu')Now render the menu manager again and you can see that we’ve got some menu
items. but you can see that this menu items are not selected:>>> firstMenu = FirstMenu(sampleContent, request, firstView)
>>> firstMenu.update()
>>> print(firstMenu.render())
<li>
  <a><span>First Menu</span></a>
</li>
<li>
  <a><span>Second Menu</span></a>
</li>>>> secondMenu = SecondMenu(sampleContent, request, firstView)
>>> secondMenu.update()
>>> print(secondMenu.render())
<li>
  <a><span>First Menu</span></a>
</li>
<li>
  <a><span>Second Menu</span></a>
</li>Now we can register a menu selector for our page whihc renders the menu
as selected if we access the page:>>> context = xmlconfig.string("""
... <configure
...     xmlns:z3c="http://namespaces.zope.org/z3c">
...   <z3c:menuSelector
...       view=".testing.IFirstView"
...       manager=".testing.IFirstMenu"
...       menu=".testing.FirstMenuItem"
...       />
... </configure>
... """, context)After we registered a menu selector for the first view and first menu, we will
see that the first menu get rendered as selected on the first menu:>>> firstMenu = FirstMenu(sampleContent, request, firstView)
>>> firstMenu.update()
>>> print(firstMenu.render())
<li class="selected">
  <a><span>First Menu</span></a>
</li>
<li>
  <a><span>Second Menu</span></a>
</li>But not on the second menu:>>> secondMenu = SecondMenu(sampleContent, request, firstView)
>>> secondMenu.update()
>>> print(secondMenu.render())
<li>
  <a><span>First Menu</span></a>
</li>
<li>
  <a><span>Second Menu</span></a>
</li>CHANGES2.0 (2023-02-07)Add support for Python 3.8, 3.9, 3.10, 3.11.Drop support for Python 2.7, 3.5, 3.6.Drop support for deprecatedpython setup.py test.1.1.0 (2018-10-09)Add support for Python 3.7.Remove all deprecation warnings.1.0.0 (2017-04-17)Pin Python support to 2.7, 3.5, 3.6 and PyPy.1.0.0a1 (2013-03-03)Added support for Python 3.3.Changedzope.testbrowsertests toWebTest, sincezope.testbrowseris not yet ported.Replaced deprecatedzope.interface.implementsusage with equivalentzope.interface.implementerdecorator.Dropped support for Python 2.4 and 2.5.0.8.0 (2010-07-12)Replacedzope.app.pagetemplatetest dependency byzope.browserpageas the neededmetaconfigure.registerTypehas
been moved there lately without leaving a BBB import.0.7.1 (2009-12-26)Removed dependency onz3c.i18nby declaring the z3cMessageFactorylocally.Using pythondoctestmodule instead ofzope.testing.doctestunitas it deprecated now.0.7.0 (2009-11-30)Adjust dependencies and imports, to reflect changes in zope packages.0.6.0 (2009-02-07)Replacedzope.app.componentbyzope.site.Replacedzope.app.containerbyzope.container.zope.app.pagetemplateis only a test dependency.0.5.1 (2009-01-04)Add support for viewNames that start with@@. They are now
processed properly by the ViewNameSelectedChecker.Added documentation to Pypi home page.0.5.0 (2008-04-11)bugfix: fixed cssInActive usage. This was broken and ended in not
using the cssInActive CSS class argumentadded more tests, now we have 100% coveragemake ISiteMenu only available for ISite but not for IContainmentRootmake template pluggable with z3c.template patterninitial Release