Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "LIFT" sentinel for context and name arguments to add_view #3739

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
17 changes: 15 additions & 2 deletions src/pyramid/config/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
as_sorted_tuple,
is_nonstr_iter,
)
from pyramid.view import AppendSlashNotFoundViewFactory
from pyramid.view import LIFT, AppendSlashNotFoundViewFactory
mmerickel marked this conversation as resolved.
Show resolved Hide resolved
import pyramid.viewderivers
from pyramid.viewderivers import (
INGRESS,
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions src/pyramid/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from pyramid.util import hide_attrs, reraise as reraise_

_marker = object()
LIFT = object()
mmerickel marked this conversation as resolved.
Show resolved Hide resolved

mmerickel marked this conversation as resolved.
Show resolved Hide resolved

def render_view_to_response(context, request, name='', secure=True):
Expand Down
Loading