diff options
| author | Michael Merickel <michael@merickel.org> | 2018-11-11 22:39:52 -0600 |
|---|---|---|
| committer | Michael Merickel <michael@merickel.org> | 2018-11-11 23:45:24 -0600 |
| commit | dff0798e352d0afeeec2058200ac983692b606c2 (patch) | |
| tree | 9e6c4da2d683db86808437e0af1aad1231cdc846 /src | |
| parent | ebb56ebe4dd0cbecd6d19b5741ddf52c96a0176d (diff) | |
| download | pyramid-dff0798e352d0afeeec2058200ac983692b606c2.tar.gz pyramid-dff0798e352d0afeeec2058200ac983692b606c2.tar.bz2 pyramid-dff0798e352d0afeeec2058200ac983692b606c2.zip | |
use inherit_slash=True on add_route to opt-in to no trailing slash
Diffstat (limited to 'src')
| -rw-r--r-- | src/pyramid/config/__init__.py | 4 | ||||
| -rw-r--r-- | src/pyramid/config/routes.py | 39 |
2 files changed, 36 insertions, 7 deletions
diff --git a/src/pyramid/config/__init__.py b/src/pyramid/config/__init__.py index 00c3e6a02..475f0d9a2 100644 --- a/src/pyramid/config/__init__.py +++ b/src/pyramid/config/__init__.py @@ -602,7 +602,9 @@ class Configurator( configuration conflict by registering something with the same configuration parameters. - If the ``route_prefix`` is supplied, it must be a string. Any calls + If the ``route_prefix`` is supplied, it must be a string and will + have a similar effect to using + :meth:`pyramid.config.Configurator.route_prefix_context`. Any calls to :meth:`pyramid.config.Configurator.add_route` within the included callable will have their pattern prefixed with the value of ``route_prefix``. This can be used to help mount a set of routes at a diff --git a/src/pyramid/config/routes.py b/src/pyramid/config/routes.py index 7bddf4e67..e7e981df0 100644 --- a/src/pyramid/config/routes.py +++ b/src/pyramid/config/routes.py @@ -40,6 +40,7 @@ class RoutesConfiguratorMixin(object): path=None, pregenerator=None, static=False, + inherit_slash=None, **predicates ): """ Add a :term:`route configuration` to the current @@ -139,6 +140,26 @@ class RoutesConfiguratorMixin(object): .. versionadded:: 1.1 + inherit_slash + + This argument can only be used when the ``pattern`` is an empty + string (``''``). By default, the composed route pattern will always + includes a trailing slash, but this argument provides a way to + opt-out if both, you and the integrator (the developer setting the + :term:`route prefix`), agree that the pattern should not contain + a trailing slash. For example: + + .. code-block:: python + + with config.route_prefix_context('/users'): + config.add_route('users', '', inherit_slash=True) + + In this example, the resulting route pattern will be ``/users``. + Alternatively, if the route prefix were ``/users/`` then the + resulting route pattern would be ``/users/``. + + .. versionadded:: 2.0 + Predicate Arguments pattern @@ -329,6 +350,11 @@ class RoutesConfiguratorMixin(object): if pattern is None: raise ConfigurationError('"pattern" argument may not be None') + if inherit_slash and pattern != '': + raise ConfigurationError( + '"inherit_slash" may only be used with an empty pattern' + ) + # check for an external route; an external route is one which is # is a full url (e.g. 'http://example.com/{id}') parsed = urlparse.urlparse(pattern) @@ -364,10 +390,12 @@ class RoutesConfiguratorMixin(object): static = True elif self.route_prefix: - if pattern == '': - pattern = self.route_prefix.rstrip('/') + if pattern == '' and inherit_slash: + pattern = self.route_prefix else: - pattern = self.route_prefix.rstrip('/') + '/' + pattern.lstrip('/') + pattern = ( + self.route_prefix.rstrip('/') + '/' + pattern.lstrip('/') + ) mapper = self.get_routes_mapper() @@ -517,9 +545,8 @@ class RoutesConfiguratorMixin(object): @contextlib.contextmanager def route_prefix_context(self, route_prefix): - """ Return this configurator with the - :attr:`pyramid.config.Configurator.route_prefix` attribute mutated to - include the new ``route_prefix``. + """ + Return this configurator with a :term:`route prefix` temporarily set. When the context exits, the ``route_prefix`` is reset to the original. |
