From 855fe374108779e82530da74f1c0fd21ab9033b3 Mon Sep 17 00:00:00 2001 From: Charlie Choiniere Date: Fri, 17 Apr 2015 07:12:11 -0400 Subject: Added support for a prefixed base url to not contain a trailing slash --- docs/narr/urldispatch.rst | 18 ++++++++++++++++++ pyramid/config/routes.py | 5 ++++- pyramid/tests/test_config/test_routes.py | 12 ++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/docs/narr/urldispatch.rst b/docs/narr/urldispatch.rst index 87a962a9a..f2a94341d 100644 --- a/docs/narr/urldispatch.rst +++ b/docs/narr/urldispatch.rst @@ -964,6 +964,24 @@ then only match if the URL path is ``/users/show``, and when the :meth:`pyramid.request.Request.route_url` function is called with the route name ``show_users``, it will generate a URL with that same path. +To create a prefixed route that matches requests to the ``route_prefix`` +without a trailing slash set the route ``pattern`` to an empty string. + +.. code-block:: python + :linenos: + + from pyramid.config import Configurator + + def users_include(config): + config.add_route('show_users', '') + + def main(global_config, **settings): + config = Configurator() + config.include(users_include, route_prefix='/users') + +The above configuration will match ``/users`` instead of ``/users/`` if a slash +had been supplied to the :meth:`pyramid.config.Configurator.add_route` function. + Route prefixes are recursive, so if a callable executed via an include itself turns around and includes another callable, the second-level route prefix will be prepended with the first: diff --git a/pyramid/config/routes.py b/pyramid/config/routes.py index f1463b50b..971e3fc95 100644 --- a/pyramid/config/routes.py +++ b/pyramid/config/routes.py @@ -333,7 +333,10 @@ class RoutesConfiguratorMixin(object): static = True elif self.route_prefix: - pattern = self.route_prefix.rstrip('/') + '/' + pattern.lstrip('/') + if pattern == '': + pattern = self.route_prefix.rstrip('/') + else: + pattern = self.route_prefix.rstrip('/') + '/' + pattern.lstrip('/') mapper = self.get_routes_mapper() diff --git a/pyramid/tests/test_config/test_routes.py b/pyramid/tests/test_config/test_routes.py index 1d2530c02..778d33938 100644 --- a/pyramid/tests/test_config/test_routes.py +++ b/pyramid/tests/test_config/test_routes.py @@ -50,6 +50,18 @@ class RoutesConfiguratorMixinTests(unittest.TestCase): config.add_route('name', 'path') self._assertRoute(config, 'name', 'root/path') + def test_add_route_with_empty_string_with_route_prefix(self): + config = self._makeOne(autocommit=True) + config.route_prefix = 'root' + config.add_route('name', '') + self._assertRoute(config, 'name', 'root') + + def test_add_route_with_root_slash_with_route_prefix(self): + config = self._makeOne(autocommit=True) + config.route_prefix = 'root' + config.add_route('name', '/') + self._assertRoute(config, 'name', 'root/') + def test_add_route_discriminator(self): config = self._makeOne() config.add_route('name', 'path') -- cgit v1.2.3 From de5e4f40e0246d71ba5875c957281fb764da74dd Mon Sep 17 00:00:00 2001 From: Charlie Choiniere Date: Mon, 3 Apr 2017 21:36:03 -0400 Subject: signed contributors agreement --- CONTRIBUTORS.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index d1ac72df5..08d06875e 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -230,3 +230,5 @@ Contributors - Antti Haapala, 2013/11/15 - Amit Mane, 2014/01/23 + +- Charlie Choiniere, 2017/04/03 -- cgit v1.2.3