diff options
| author | Michael Merickel <github@m.merickel.org> | 2018-11-12 20:07:15 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-11-12 20:07:15 -0600 |
| commit | 7429fcb5a96aa10bbe86da08bd0b30c9292efdde (patch) | |
| tree | 548defadbf50447b4e6a0b1f9363f3d08ded8bfc /src | |
| parent | 420ea05bee3ad376c4369f401b2696b31312c5e1 (diff) | |
| parent | 2d32ae81abc473fb21b4bc42aec479c656693f1c (diff) | |
| download | pyramid-7429fcb5a96aa10bbe86da08bd0b30c9292efdde.tar.gz pyramid-7429fcb5a96aa10bbe86da08bd0b30c9292efdde.tar.bz2 pyramid-7429fcb5a96aa10bbe86da08bd0b30c9292efdde.zip | |
Merge pull request #3420 from mmerickel/bare-route-prefix-2
support inherit_slash=True on add_route
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, 38 insertions, 5 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 f9180bd76..52540c935 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,27 @@ 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 + include a trailing slash, but this argument provides a way to + opt-out if both, you (the developer invoking ``add_route``) 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 +351,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,7 +391,12 @@ class RoutesConfiguratorMixin(object): static = True elif self.route_prefix: - pattern = self.route_prefix.rstrip('/') + '/' + pattern.lstrip('/') + if pattern == '' and inherit_slash: + pattern = self.route_prefix + else: + pattern = ( + self.route_prefix.rstrip('/') + '/' + pattern.lstrip('/') + ) mapper = self.get_routes_mapper() @@ -514,9 +546,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. |
