Skip to content

Commit

Permalink
add a layer to traverser components
Browse files Browse the repository at this point in the history
  • Loading branch information
goschtl committed Nov 13, 2012
1 parent 489dda8 commit 3c4dfd1
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 2 deletions.
90 changes: 90 additions & 0 deletions src/grokcore/traverser/ftests/traversal/traverser_with_layer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
"""
Apart from using the ``traverse`` method on a model, you can
also create a separate traverser component:
>>> getRootFolder()["herd"] = Herd('The Big Mammoth Herd')
>>> from zope.app.wsgi.testlayer import Browser
>>> browser = Browser()
>>> browser.handleErrors = False
>>> browser.open("http://localhost/herd/manfred")
>>> print browser.contents
<html>
<body>
<h1>Hello, Manfred!</h1>
<p>Manfred is part of The Big Mammoth Herd.</p>
</body>
</html>
>>> browser.open("http://localhost/herd/ellie")
>>> print browser.contents
<html>
<body>
<h1>Hello, Ellie!</h1>
<p>Ellie is part of The Big Mammoth Herd.</p>
</body>
</html>
Now let's call the same URI on a different Layer.
>>> browser.open("http://localhost/++skin++elephant/herd/ellie")
>>> print browser.contents
<html>
<body>
<h1>Hello, Ellie Elephant!</h1>
<p>Ellie Elephant is part of The Big Mammoth Herd.</p>
</body>
</html>
"""
import grokcore.component as grok
import grokcore.content as content
import grokcore.traverser
import grokcore.view as view


class ElephantLayer(view.IDefaultBrowserLayer):
pass

class ElephantSkin(ElephantLayer):
view.skin('elephant')


class Herd(content.Model):

def __init__(self, name):
self.name = name


class HerdTraverser(grokcore.traverser.Traverser):
grok.context(Herd)

def traverse(self, name):
return Mammoth(name)


class ElephantTraverser(grokcore.traverser.Traverser):
grok.context(Herd)
view.layer(ElephantLayer)

def traverse(self, name):
return Mammoth(name + ' Elephant')


class Mammoth(content.Model):

def __init__(self, name):
self.name = name

grok.context(Mammoth)

class Index(view.View):
pass

index = view.PageTemplate("""\
<html>
<body>
<h1>Hello, <span tal:replace="context/name/title" />!</h1>
<p><span tal:replace="context/name/title" /> is part of <span tal:replace="context/__parent__/name" />.</p>
</body>
</html>
""")
7 changes: 5 additions & 2 deletions src/grokcore/traverser/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,23 @@
"""

import martian
import grokcore.view
import grokcore.traverser
import grokcore.component

from zope.publisher.interfaces.http import IHTTPRequest
from zope.publisher.interfaces.browser import IBrowserPublisher
from zope.publisher.interfaces.browser import IDefaultBrowserLayer


class TraverserGrokker(martian.ClassGrokker):
"""Grokker for subclasses of `grok.Traverser`."""
martian.component(grokcore.traverser.Traverser)
martian.directive(grokcore.component.context)
martian.directive(grokcore.view.layer, default=IDefaultBrowserLayer)

def execute(self, factory, config, context, **kw):
adapts = (context, IHTTPRequest)
def execute(self, factory, config, context, layer, **kw):
adapts = (context, layer)
config.action(
discriminator=('adapter', adapts, IBrowserPublisher, ''),
callable=grokcore.component.provideAdapter,
Expand Down

0 comments on commit 3c4dfd1

Please sign in to comment.