From 21707020158f1c161c402776148436ce82c7c5b6 Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Wed, 29 Apr 2009 21:34:09 +0000 Subject: Drop the ILocation testing by default during traversal. --- docs/narr/models.rst | 13 +++++++++++++ docs/narr/security.rst | 12 ++++++++++++ repoze/bfg/tests/test_traversal.py | 38 ++++++++++++++++++++++---------------- repoze/bfg/traversal.py | 6 +++++- 4 files changed, 52 insertions(+), 17 deletions(-) diff --git a/docs/narr/models.rst b/docs/narr/models.rst index c07dfbe18..e9eddf39b 100644 --- a/docs/narr/models.rst +++ b/docs/narr/models.rst @@ -110,6 +110,19 @@ root node says it implements the ``ILocation`` :term:`interface`, will dynamically assign a ``__name__`` and a ``__parent__`` to it, recursively. +.. note:: + In order to use this feature, you must register the + ``WrappingModelGraphTraverser`` as the traversal policy, rather + than the standard ``ModelGraphTraverser``. E.g., your application + will need to have the following in its ``configure.zcml``:: + + + + If you choose to make use of the location-based dynamic assignment of ``__parent__`` and ``__name__``, the root node must have a ``__parent__`` that is ``None``, a ``__name__`` with any value, and it diff --git a/docs/narr/security.rst b/docs/narr/security.rst index 91c7240dc..590b90b2e 100644 --- a/docs/narr/security.rst +++ b/docs/narr/security.rst @@ -179,6 +179,18 @@ during traversal in a *location proxy* that has both the ``__name__`` and ``__parent__`` attributes, but otherwise acts the same as your model object. +.. note:: + In order to use this feature, you must register the + ``WrappingModelGraphTraverser`` as the traversal policy, rather + than the standard ``ModelGraphTraverser``. E.g., your application + will need to have the following in its ``configure.zcml``:: + + + You can of course supply ``__name__`` and ``__parent__`` attributes explicitly on all of your model objects, and no location proxying will be performed. diff --git a/repoze/bfg/tests/test_traversal.py b/repoze/bfg/tests/test_traversal.py index 18e9a9f18..3354dcce1 100644 --- a/repoze/bfg/tests/test_traversal.py +++ b/repoze/bfg/tests/test_traversal.py @@ -173,6 +173,28 @@ class ModelGraphTraverserTests(unittest.TestCase): self.assertEqual(vroot, bar) self.assertEqual(vroot_path, [u'foo', u'bar']) + def test_non_utf8_path_segment_unicode_path_segments_fails(self): + foo = DummyContext() + root = DummyContext(foo) + policy = self._makeOne(root) + segment = unicode('LaPe\xc3\xb1a', 'utf-8').encode('utf-16') + environ = self._getEnviron(PATH_INFO='/%s' % segment) + self.assertRaises(TypeError, policy, environ) + + def test_non_utf8_path_segment_settings_unicode_path_segments_fails(self): + foo = DummyContext() + root = DummyContext(foo) + policy = self._makeOne(root) + segment = unicode('LaPe\xc3\xb1a', 'utf-8').encode('utf-16') + environ = self._getEnviron(PATH_INFO='/%s' % segment) + self.assertRaises(TypeError, policy, environ) + +class WrappingModelGraphTraverserTests(ModelGraphTraverserTests): + + def _getTargetClass(self): + from repoze.bfg.traversal import WrappingModelGraphTraverser + return WrappingModelGraphTraverser + def test_call_with_ILocation_root_proxies(self): baz = DummyContext() bar = DummyContext(baz) @@ -240,22 +262,6 @@ class ModelGraphTraverserTests(unittest.TestCase): self.assertEqual(vroot, root) self.assertEqual(vroot_path, []) - def test_non_utf8_path_segment_unicode_path_segments_fails(self): - foo = DummyContext() - root = DummyContext(foo) - policy = self._makeOne(root) - segment = unicode('LaPe\xc3\xb1a', 'utf-8').encode('utf-16') - environ = self._getEnviron(PATH_INFO='/%s' % segment) - self.assertRaises(TypeError, policy, environ) - - def test_non_utf8_path_segment_settings_unicode_path_segments_fails(self): - foo = DummyContext() - root = DummyContext(foo) - policy = self._makeOne(root) - segment = unicode('LaPe\xc3\xb1a', 'utf-8').encode('utf-16') - environ = self._getEnviron(PATH_INFO='/%s' % segment) - self.assertRaises(TypeError, policy, environ) - class FindInterfaceTests(unittest.TestCase): def _callFUT(self, context, iface): from repoze.bfg.traversal import find_interface diff --git a/repoze/bfg/traversal.py b/repoze/bfg/traversal.py index 60d388809..6c2e6aa9a 100644 --- a/repoze/bfg/traversal.py +++ b/repoze/bfg/traversal.py @@ -382,6 +382,7 @@ _marker = object() class ModelGraphTraverser(object): classProvides(ITraverserFactory) implements(ITraverser) + SUPPLY_LOCATION_PROXIES = False def __init__(self, root): self.root = root @@ -406,7 +407,7 @@ class ModelGraphTraverser(object): ob = vroot = self.root name = '' - locatable = ILocation.providedBy(ob) + locatable = self.SUPPLY_LOCATION_PROXIES and ILocation.providedBy(ob) i = 1 @@ -431,6 +432,9 @@ class ModelGraphTraverser(object): return ob, '', [], traversed, vroot, vroot_path +class WrappingModelGraphTraverser(ModelGraphTraverser): + SUPPLY_LOCATION_PROXIES = True + class TraversalContextURL(object): """ The IContextURL adapter used to generate URLs for a context object obtained via graph traversal""" -- cgit v1.2.3