From dd357250217ec4b5eeba96e245c46509f231c6d8 Mon Sep 17 00:00:00 2001 From: Michael Merickel Date: Mon, 3 Sep 2018 21:21:20 -0500 Subject: fallback to __contains__ if a media range is supplied --- pyramid/config/routes.py | 9 +++++++++ pyramid/config/views.py | 13 ++++++++++++- pyramid/predicates.py | 5 +++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/pyramid/config/routes.py b/pyramid/config/routes.py index 051bd9edb..b701a4bef 100644 --- a/pyramid/config/routes.py +++ b/pyramid/config/routes.py @@ -299,6 +299,15 @@ class RoutesConfiguratorMixin(object): stacklevel=3 ) + if accept is not None and '*' in accept: + warnings.warn( + ('The usage of a media range in the "accept" route predicate ' + 'is deprecated as of Pyramid 1.10. Use a list of explicit ' + 'media types instead.'), + DeprecationWarning, + stacklevel=4, + ) + if accept is not None: accept = accept.lower() diff --git a/pyramid/config/views.py b/pyramid/config/views.py index d6a80fe11..87584b858 100644 --- a/pyramid/config/views.py +++ b/pyramid/config/views.py @@ -123,7 +123,7 @@ class MultiView(object): self.views[i] = (order, view, phash) return - if accept is None: + if accept is None or '*' in accept: self.views.append((order, view, phash)) self.views.sort(key=operator.itemgetter(0)) else: @@ -818,6 +818,17 @@ class ViewsConfiguratorMixin(object): stacklevel=4, ) + if accept is not None and '*' in accept: + warnings.warn( + ('The usage of a media range in the "accept" view predicate ' + 'is deprecated as of Pyramid 1.10. Register multiple views ' + 'with explicit media ranges and read ' + '"Accept Header Content Negotiation" in the ' + '"View Configuration" documentation for more information.'), + DeprecationWarning, + stacklevel=4, + ) + if accept is not None and is_nonstr_iter(accept): raise ConfigurationError( 'A list is not supported in the "accept" view predicate.', diff --git a/pyramid/predicates.py b/pyramid/predicates.py index 91fb41006..262b28d89 100644 --- a/pyramid/predicates.py +++ b/pyramid/predicates.py @@ -130,8 +130,11 @@ class HeaderPredicate(object): return self.val.match(val) is not None class AcceptPredicate(object): + _use_deprecated_range_fallback = False + def __init__(self, val, config): if not is_nonstr_iter(val): + self._use_deprecated_range_fallback = '*' in val val = (val,) self.values = val @@ -141,6 +144,8 @@ class AcceptPredicate(object): phash = text def __call__(self, context, request): + if self._use_deprecated_range_fallback: + return self.values[0] in request.accept return bool(request.accept.acceptable_offers(self.values)) class ContainmentPredicate(object): -- cgit v1.2.3