From 184d815a4e37f09405d0c1311ddcf2cc6d2316ea Mon Sep 17 00:00:00 2001 From: Eric Atkin Date: Fri, 22 Dec 2023 23:06:47 -0700 Subject: [PATCH] Add "LIFT" sentinel for context and name arguments to add_view --- src/pyramid/config/views.py | 17 +++++++++++++++-- src/pyramid/view.py | 1 + 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/pyramid/config/views.py b/src/pyramid/config/views.py index 4f5806df35..0b1d3abc51 100644 --- a/src/pyramid/config/views.py +++ b/src/pyramid/config/views.py @@ -56,7 +56,7 @@ as_sorted_tuple, is_nonstr_iter, ) -from pyramid.view import AppendSlashNotFoundViewFactory +from pyramid.view import LIFT, AppendSlashNotFoundViewFactory import pyramid.viewderivers from pyramid.viewderivers import ( INGRESS, @@ -572,7 +572,9 @@ def wrapper(context, request): name The :term:`view name`. Read :ref:`traversal_chapter` to - understand the concept of a view name. + understand the concept of a view name. When :term:`view` is a class, + the sentinel value view.LIFT will cause the :term:`attr` value to be + copied to name (useful with view_defaults to reduce boilerplate). context @@ -587,6 +589,9 @@ def wrapper(context, request): to ``add_view`` as ``for_`` (an older, still-supported spelling). If the view should *only* match when handling exceptions, then set the ``exception_only`` to ``True``. + When :term:`view` is a class, the sentinel value view.LIFT here + will cause the :term:`context` value to be set at scan time + (useful in conjunction with venusian :term:`lift`). route_name @@ -815,6 +820,14 @@ def wrapper(context, request): containment = self.maybe_dotted(containment) mapper = self.maybe_dotted(mapper) + if inspect.isclass(view): + if context is LIFT: + context = view + if name is LIFT: + name = attr + elif LIFT in (context, name): + raise ValueError('LIFT is only allowed when view is a class') + if is_nonstr_iter(decorator): decorator = combine_decorators(*map(self.maybe_dotted, decorator)) else: diff --git a/src/pyramid/view.py b/src/pyramid/view.py index 1541cdb230..0f96fb774f 100644 --- a/src/pyramid/view.py +++ b/src/pyramid/view.py @@ -23,6 +23,7 @@ from pyramid.util import hide_attrs, reraise as reraise_ _marker = object() +LIFT = object() def render_view_to_response(context, request, name='', secure=True):