summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Merickel <michael@merickel.org>2018-11-04 13:25:32 -0600
committerMichael Merickel <michael@merickel.org>2018-11-04 13:25:32 -0600
commit44e7e63ac1f95d52bdd25c22c36efd820c059e35 (patch)
treee6062558dd3c5a4716f9cd521059f1621bc76457
parent6f0d73d6eea8064f1c5d9294beb7f7d64619071d (diff)
parentde5e4f40e0246d71ba5875c957281fb764da74dd (diff)
downloadpyramid-44e7e63ac1f95d52bdd25c22c36efd820c059e35.tar.gz
pyramid-44e7e63ac1f95d52bdd25c22c36efd820c059e35.tar.bz2
pyramid-44e7e63ac1f95d52bdd25c22c36efd820c059e35.zip
Merge branch 'pr/1639' into bare-route-prefix
-rw-r--r--CONTRIBUTORS.txt3
-rw-r--r--docs/narr/urldispatch.rst18
-rw-r--r--src/pyramid/config/routes.py5
-rw-r--r--tests/test_config/test_routes.py12
4 files changed, 37 insertions, 1 deletions
diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt
index 7256b66db..6eb42c18c 100644
--- a/CONTRIBUTORS.txt
+++ b/CONTRIBUTORS.txt
@@ -230,6 +230,7 @@ Contributors
- Antti Haapala, 2013/11/15
- Amit Mane, 2014/01/23
+<<<<<<< HEAD
- Fenton Travers, 2014/05/06
@@ -297,6 +298,8 @@ Contributors
- Kirill Kuzminykh, 2017/03/01
+- Charlie Choiniere, 2017/04/03
+
- Aleph Melo, 2017/04/16
- Jeremy(Ching-Rui) Chen, 2017/04/19
diff --git a/docs/narr/urldispatch.rst b/docs/narr/urldispatch.rst
index 52d64891c..701326fab 100644
--- a/docs/narr/urldispatch.rst
+++ b/docs/narr/urldispatch.rst
@@ -998,6 +998,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/src/pyramid/config/routes.py b/src/pyramid/config/routes.py
index f9180bd76..7bddf4e67 100644
--- a/src/pyramid/config/routes.py
+++ b/src/pyramid/config/routes.py
@@ -364,7 +364,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/tests/test_config/test_routes.py b/tests/test_config/test_routes.py
index 32a64b4bd..1c1ed6700 100644
--- a/tests/test_config/test_routes.py
+++ b/tests/test_config/test_routes.py
@@ -54,6 +54,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')