Chris McDonough
2012-11-05 a8d71ca12aa648aa70e0c00f303e60da1fa97a61
- Allow a ``_depth`` argument to ``pyramid.view.view_config``, which will
permit limited compisition reuse of the decorator by other software that
wants to provide custom decorators that are much like view_config.

Closes #637.
3 files modified
26 ■■■■■ changed files
CHANGES.txt 4 ●●●● patch | view | raw | blame | history
pyramid/tests/test_view.py 11 ●●●●● patch | view | raw | blame | history
pyramid/view.py 11 ●●●●● patch | view | raw | blame | history
CHANGES.txt
@@ -33,6 +33,10 @@
  class; that's enough to satisfy WebOb and behave as it did before with the
  monkeypatching.
- Allow a ``_depth`` argument to ``pyramid.view.view_config``, which will
  permit limited composition reuse of the decorator by other software that
  wants to provide custom decorators that are much like view_config.
Bug Fixes
---------
pyramid/tests/test_view.py
@@ -519,6 +519,14 @@
        self.assertTrue(renderer is renderer_helper)
        self.assertEqual(config.pkg, pyramid.tests)
    def test_call_withdepth(self):
        decorator = self._makeOne(_depth=2)
        venusian = DummyVenusian()
        decorator.venusian = venusian
        def foo(): pass
        decorator(foo)
        self.assertEqual(venusian.depth, 2)
class Test_append_slash_notfound_view(BaseTest, unittest.TestCase):
    def _callFUT(self, context, request):
        from pyramid.view import append_slash_notfound_view
@@ -746,8 +754,9 @@
        self.info = info
        self.attachments = []
    def attach(self, wrapped, callback, category=None):
    def attach(self, wrapped, callback, category=None, depth=1):
        self.attachments.append((wrapped, callback, category))
        self.depth = depth
        return self.info
class DummyRegistry(object):
pyramid/view.py
@@ -176,6 +176,13 @@
    :meth:`pyramid.config.Configurator.add_view`.  If any argument is left
    out, its default will be the equivalent ``add_view`` default.
    An additional keyword argument named ``_depth`` is provided for people who
    wish to reuse this class from another decorator.  It will be passed in to
    the :term:`venusian` ``attach`` function as the depth of the callstack when
    Venusian checks if the decorator is being used in a class or module
    context.  It's not often used, but it can be useful in this circumstance.
    See the ``attach`` function in Venusian for more information.
    See :ref:`mapping_views_using_a_decorator_section` for details about
    using :class:`view_config`.
@@ -189,12 +196,14 @@
    def __call__(self, wrapped):
        settings = self.__dict__.copy()
        depth = settings.pop('_depth', 1)
        def callback(context, name, ob):
            config = context.config.with_package(info.module)
            config.add_view(view=ob, **settings)
        info = self.venusian.attach(wrapped, callback, category='pyramid')
        info = self.venusian.attach(wrapped, callback, category='pyramid',
                                    depth=depth)
        if info.scope == 'class':
            # if the decorator was attached to a method in a class, or