z3c.menu.ready2go 0.7.0
pip install z3c.menu.ready2go==0.7.0
Released:
A ready to go menu for Zope3
Navigation
Verified details
These details have been verified by PyPIOwner
Zope FoundationMaintainers
agroszer baijum ccomb chrisw davisagli faassen gary hannosch hathawsh menesis mgedmin nadako pcardune projekt01 rcrafton roymath srichter tlotzeUnverified details
These details have not been verified by PyPIProject links
Meta
- License: Zope Public License (ZPL 2.1)
- Author: Stephan Richter, Roger Ineichen and the Zope Community
- Tags zope3, z3c, ready, 2, go, menu
Classifiers
- Development Status
- Environment
- Framework
- Intended Audience
- License
- Natural Language
- Operating System
- Programming Language
- Topic
Project description
This package provides a ready 2 go menu implementation based on viewlets for Zope3.
Ready 2 go Menu
The 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 Menu
Let’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 MenuManager
And we configure our menu item as viewlet Managers. This is normaly done by the viewletManager ZCML 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) True
We 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.container.interfaces import IContained >>> from zope.location.interfaces import IPossibleSite >>> from zope.site.site import SiteManagerContainer >>> from zope.site.site import LocalSiteManager>>> class Site(btree.BTreeContainer, SiteManagerContainer): ... zope.interface.implements(IPossibleSite) ... def __init__(self): ... super(Site, self).__init__() ... self.setSiteManager(LocalSiteManager(self))>>> class Content(contained.Contained): ... zope.interface.implements(IContained)>>> 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.site 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:
>>> class View(contained.Contained): ... ... zope.interface.implements(IBrowserView) ... ... 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() u''>>> siteMenu = SiteMenu(content, request, view) >>> siteMenu.update() >>> siteMenu.render() u''>>> contextMenu = ContextMenu(content, request, view) >>> contextMenu.update() >>> contextMenu.render() u''>>> addMenu = AddMenu(content, request, view) >>> addMenu.update() >>> addMenu.render() u''
Global Menu Item
Now 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 the viewlet ZCML 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 u'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 Item
Now 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 the viewlet ZCML 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 Item
Now 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'
Now 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 the viewlet ZCML 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__ to context.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.html form:
>>> 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 to context.html for 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'>>> 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>
AddMenu
The 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 the viewlet ZCML 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 groups
The 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>
EmptyMenuManager
There 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 implements IMenuManager:
>>> interfaces.IMenuManager.providedBy(emptyMenu) True
This empty menu manager returns allways an empty string if we render them:
>>> emptyMenu.update() >>> emptyMenu.render() u''
Special use case
We 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() u''
Also the SiteMenu doesn’t contain any menu item because of the parent less object:
>>> siteMenu = SiteMenu(nirvana, request, nirvanaView) >>> siteMenu.update() >>> siteMenu.render() u''>>> contextMenu = ContextMenu(nirvana, request, nirvanaView) >>> contextMenu.update() >>> contextMenu.render() u''>>> addMenu = AddMenu(nirvana, request, nirvanaView) >>> addMenu.update() >>> addMenu.render() u''
Z3C Menu directives
Show 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'] = sampleContent
Now 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) True
As 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() u''>>> secondMenu = SecondMenu(sampleContent, request, secondView) >>> testing.ISecondMenu.providedBy(secondMenu) True>>> secondMenu.update() >>> secondMenu.render() u''
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>Second Menu</span></a> </li> <li> <a><span>First Menu</span></a> </li>>>> secondMenu = SecondMenu(sampleContent, request, firstView) >>> secondMenu.update() >>> print secondMenu.render() <li> <a><span>Second Menu</span></a> </li> <li> <a><span>First 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> <a><span>Second Menu</span></a> </li> <li class="selected"> <a><span>First Menu</span></a> </li>
But not on the second menu:
>>> secondMenu = SecondMenu(sampleContent, request, firstView) >>> secondMenu.update() >>> print secondMenu.render() <li> <a><span>Second Menu</span></a> </li> <li> <a><span>First Menu</span></a> </li>
CHANGES
0.7.0 (2009-11-30)
adjust dependencies and imports, reflect changes in zope packages
0.6.0 (2009-02-07)
Replaced zope.app.component by zope.site.
Replaced zope.app.container by zope.container.
zope.app.pagetemplate is 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 argument
added more tests, now we have 100% coverage
make ISiteMenu only available for ISite but not for IContainmentRoot
make template pluggable with z3c.template pattern
initial Release
Project details
Verified details
These details have been verified by PyPIOwner
Zope FoundationMaintainers
agroszer baijum ccomb chrisw davisagli faassen gary hannosch hathawsh menesis mgedmin nadako pcardune projekt01 rcrafton roymath srichter tlotzeUnverified details
These details have not been verified by PyPIProject links
Meta
- License: Zope Public License (ZPL 2.1)
- Author: Stephan Richter, Roger Ineichen and the Zope Community
- Tags zope3, z3c, ready, 2, go, menu
Classifiers
- Development Status
- Environment
- Framework
- Intended Audience
- License
- Natural Language
- Operating System
- Programming Language
- Topic
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file z3c.menu.ready2go-0.7.0.zip
.
File metadata
- Download URL: z3c.menu.ready2go-0.7.0.zip
- Upload date:
- Size: 36.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 13adf1b0c514187beac892e198fc4b0dc2b4c4312d13219e693decf15e942233 |
|
MD5 | 593eb88ec161484f702b7684788f6e69 |
|
BLAKE2b-256 | b3c5e928d39c1a935181b9f6312ee96274239e081bce74490bdf33a16b7c8a56 |