[Checkins] SVN: grok/branches/jspaans-traversableattrs/src/grok/
Make tests for traversable attributes work
Jasper Spaans
jspaans at thehealthagency.com
Fri May 2 05:22:36 EDT 2008
Log message for revision 86054:
Make tests for traversable attributes work
Changed:
U grok/branches/jspaans-traversableattrs/src/grok/components.py
U grok/branches/jspaans-traversableattrs/src/grok/ftests/traversal/traversableattr.py
-=-
Modified: grok/branches/jspaans-traversableattrs/src/grok/components.py
===================================================================
--- grok/branches/jspaans-traversableattrs/src/grok/components.py 2008-05-02 09:19:59 UTC (rev 86053)
+++ grok/branches/jspaans-traversableattrs/src/grok/components.py 2008-05-02 09:22:35 UTC (rev 86054)
@@ -442,7 +442,6 @@
subob = getattr(self.context, traversable_dict[name])
if callable(subob):
subob = subob()
- print 'XXX', subob, dir(subob), subob.__parent__
return util.safely_locate_maybe(subob, self.context, name)
# XXX Special logic here to deal with containers. It would be
Modified: grok/branches/jspaans-traversableattrs/src/grok/ftests/traversal/traversableattr.py
===================================================================
--- grok/branches/jspaans-traversableattrs/src/grok/ftests/traversal/traversableattr.py 2008-05-02 09:19:59 UTC (rev 86053)
+++ grok/branches/jspaans-traversableattrs/src/grok/ftests/traversal/traversableattr.py 2008-05-02 09:22:35 UTC (rev 86054)
@@ -1,51 +1,67 @@
"""
-Models can determine how they want to be traversed by
-implementing a 'traverse' method:
+Models can expose attributes using the grok.traversable directive.
- >>> getRootFolder()["traversefoo"] = TraFoo('foo')
+ >>> getRootFolder()["traversefoo"] = Foo('foo')
>>> from zope.testbrowser.testing import Browser
>>> browser = Browser()
>>> browser.handleErrors = False
+
+As always, we can access a model with a view::
>>> browser.open("http://localhost/traversefoo/")
>>> print browser.contents
- <html>
- <body>foo</body>
- </html>
+ foo
+
+'foo' is an exposed attribute, so it should be accessible::
>>> browser.open("http://localhost/traversefoo/foo")
>>> print browser.contents
foo
+
+'bar' is an exposed method, and should also be accessible::
>>> browser.open("http://localhost/traversefoo/bar")
>>> print browser.contents
- <html>
- <body>bar</body>
- </html>
+ bar
+'bar' is also exposed under the name 'namedbar', and can also be accessed::
+ >>> browser.open("http://localhost/traversefoo/namedbar")
+ >>> print browser.contents
+ bar
+Finally, attributes which are not exposed, should not be visible:
+ >>> browser.open("http://localhost/traversefoo/z")
+ Traceback (most recent call last):
+ ...
+ NotFound: ...
+
"""
import grok
-class TraBar(grok.Model):
+class Bar(grok.Model):
def __init__(self, name):
self.name = name
-#class TraBarIndex(grok.View):
-# grok.context(TraBar)
-# def render(self):
-# return self.name
+class BarIndex(grok.View):
+ grok.context(Bar)
+ grok.name('index')
-class TraFoo(grok.Model): #, grok.Application):
+ def render(self):
+ return self.context.name
+
+class Foo(grok.Model): #, grok.Application):
grok.traversable('bar')
grok.traversable('foo')
+ grok.traversable(attr='bar', name='namedbar')
def __init__(self, name):
self.name = name
- foo = TraBar('foo')
+ foo = Bar('foo')
def bar(self):
- return TraBar('bar')
+ return Bar('bar')
+ z = "i'm not called"
-#class TraFooIndex(grok.View):
-# grok.context(TraFoo)
-# def render(self):
-# return self.name
+class FooIndex(grok.View):
+ grok.context(Foo)
+ grok.name('index')
+ def render(self):
+ return self.context.name
More information about the Checkins
mailing list