[Checkins] SVN: grok/trunk/src/grok/ JSON view components need to
make sure certain methods are not registered as views
Jan-Wijbrand Kolman
janwijbrand at gmail.com
Fri Jan 4 17:04:57 EST 2008
Log message for revision 82671:
JSON view components need to make sure certain methods are not registered as views
even though their names do not start with an '_'. These are methods inherited from
the JSON baseclass.
Changed:
U grok/trunk/src/grok/meta.py
U grok/trunk/src/grok/tests/json/view_lookup.py
-=-
Modified: grok/trunk/src/grok/meta.py
===================================================================
--- grok/trunk/src/grok/meta.py 2008-01-04 21:39:55 UTC (rev 82670)
+++ grok/trunk/src/grok/meta.py 2008-01-04 22:04:57 UTC (rev 82671)
@@ -294,6 +294,13 @@
default_permission = get_default_permission(factory)
for method in methods:
+ # The grok.JSON component inherits methods from its baseclass
+ # (being zope.publisher.browser.BrowserPage) with names that
+ # do not start with an underscore, but should still not
+ # be registered as views. Ignore these methods:
+ if method.__name__ in ['browserDefault', 'publishTraverse']:
+ continue
+
# Create a new class with a __view_name__ attribute so the
# JSON class knows what method to call.
method_view = type(
Modified: grok/trunk/src/grok/tests/json/view_lookup.py
===================================================================
--- grok/trunk/src/grok/tests/json/view_lookup.py 2008-01-04 21:39:55 UTC (rev 82670)
+++ grok/trunk/src/grok/tests/json/view_lookup.py 2008-01-04 22:04:57 UTC (rev 82671)
@@ -20,7 +20,44 @@
>>> view = getMultiAdapter((mammoth, request), name='another')
>>> view()
'{"another": "grok"}'
-
+
+Although principally all methods of the JSON class are registered as views,
+methods with names that start with an underscore are not::
+
+ >>> view = getMultiAdapter((mammoth, request), name='_private')
+ Traceback (most recent call last):
+ ...
+ ComponentLookupError: ((<grok.tests.json.view_lookup.Mammoth object at ...>,
+ <zope.publisher.browser.TestRequest instance URL=http://127.0.0.1>),
+ <InterfaceClass zope.interface.Interface>, '_private')
+
+Even more important, special methods like __call__ are not registered as viewws
+too. This test is here to make sure a previous bug has been fixed::
+
+ >>> view = getMultiAdapter((mammoth, request), name='__call__')
+ Traceback (most recent call last):
+ ...
+ ComponentLookupError: ((<grok.tests.json.view_lookup.Mammoth object at ...>,
+ <zope.publisher.browser.TestRequest instance URL=http://127.0.0.1>),
+ <InterfaceClass zope.interface.Interface>, '__call__')
+
+For JSON views we also need to confirm some methods that are defined on the
+baseclass (BrowserPage) are not registered as views::
+
+ >>> view = getMultiAdapter((mammoth, request), name='browserDefault')
+ Traceback (most recent call last):
+ ...
+ ComponentLookupError: ((<grok.tests.json.view_lookup.Mammoth object at ...>,
+ <zope.publisher.browser.TestRequest instance URL=http://127.0.0.1>),
+ <InterfaceClass zope.interface.Interface>, 'browserDefault')
+
+ >>> view = getMultiAdapter((mammoth, request), name='publishTraverse')
+ Traceback (most recent call last):
+ ...
+ ComponentLookupError: ((<grok.tests.json.view_lookup.Mammoth object at ...>,
+ <zope.publisher.browser.TestRequest instance URL=http://127.0.0.1>),
+ <InterfaceClass zope.interface.Interface>, 'publishTraverse')
+
"""
import grok
@@ -35,4 +72,9 @@
def another(self):
return { 'another': 'grok'}
-
+
+class SecondMammothView(grok.JSON):
+ grok.context(Mammoth)
+
+ def _private(self):
+ return {'should': 'not be registered'}
More information about the Checkins
mailing list