Skip to content

Commit

Permalink
Add "LIFT" sentinel for context and name arguments to add_view
Browse files Browse the repository at this point in the history
  • Loading branch information
ericatkin committed Dec 23, 2023
1 parent 2bb8eab commit 8027dfd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
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
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()


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

0 comments on commit 8027dfd

Please sign in to comment.