summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAtsushi Odagiri <aodagx@gmail.com>2011-07-05 20:13:56 +0900
committerAtsushi Odagiri <aodagx@gmail.com>2011-07-05 20:13:56 +0900
commit8be2e08bfe7fdf527a6fde74bb7544b312e28897 (patch)
treed6c8f7c02b4d402123dce4501edf45bbb9cca6aa
parent54aa947127436e1cb9317c4b96f3901525459c42 (diff)
parentd46cd6ad3bc7e7b8169bf088f6a91040bf7f6a45 (diff)
downloadpyramid-8be2e08bfe7fdf527a6fde74bb7544b312e28897.tar.gz
pyramid-8be2e08bfe7fdf527a6fde74bb7544b312e28897.tar.bz2
pyramid-8be2e08bfe7fdf527a6fde74bb7544b312e28897.zip
use route_prefix rather than existing route.
-rw-r--r--pyramid/config.py35
-rw-r--r--pyramid/tests/test_config.py47
2 files changed, 25 insertions, 57 deletions
diff --git a/pyramid/config.py b/pyramid/config.py
index eb32ca9b9..61463eb86 100644
--- a/pyramid/config.py
+++ b/pyramid/config.py
@@ -604,19 +604,15 @@ class Configurator(object):
config = self.__class__.with_context(context)
c(config)
- def with_root_route(self, route_name):
- mapper = self.get_routes_mapper()
- route = mapper.get_route(route_name)
- if route is None:
- raise ConfigurationError
+ def with_route_prefix(self, route_prefix):
configurator = self.__class__(registry=self.registry,
package=self.package,
- autocommit=self.autocommit,
- root_route_name=route_name)
+ autocommit=self.autocommit)
+ configurator.route_prefix = route_prefix
return configurator
- def mount(self, function, route_name):
- """ mount subapplication on route named ``route_name``.
+ def mount_subapplication(self, function, route_prefix):
+ """ mount subapplication with ``route_prefix``
.. code-block:: python
@@ -626,11 +622,10 @@ class Configurator(object):
def main(global_config, **settings):
config = Configurator()
- config.add_route('admin', '/admin')
- config.mount('myapp.myconfig.includeme', 'admin')
+ config.mount('myapp.myconfig.includeme', '/admin')
- Because the function is named ``includeme``, the function name can
- also be omitted from the dotted name reference:
+ ``myapp.myconfig`` is subapplication
+ and ``includeme`` is that mount point.
.. code-block:: python
:linenos:
@@ -638,14 +633,14 @@ class Configurator(object):
from pyramid.config import Configurator
def includeme(config):
- config.add_route('projects', 'projects')
+ config.add_route('projects', '/projects')
- Subapplication's routes are registerd under the ``root_route``.
+ Subapplication's routes are registerd with the ``route_prefix``
In this case, ``projects`` route is registered with ``/admin/projects`` pattern.
"""
function = self.maybe_dotted(function)
- config = self.with_root_route(route_name)
+ config = self.with_route_prefix(route_prefix)
function(config)
def add_directive(self, name, directive, action_wrap=True):
@@ -1605,6 +1600,7 @@ class Configurator(object):
DeprecationWarning,
4)
+
@action_method
def add_route(self,
name,
@@ -1976,11 +1972,8 @@ class Configurator(object):
if pattern is None:
raise ConfigurationError('"pattern" argument may not be None')
- if self.root_route_name is not None:
- root_route = mapper.get_route(self.root_route_name)
- if root_route is None:
- raise ConfigurationError('route %s is not registered' % self.root_route_name)
- pattern = root_route.pattern.rstrip() + '/' + pattern.lstrip()
+ if hasattr(self, "route_prefix"):
+ pattern = self.route_prefix.rstrip('/') + '/' + pattern.lstrip('/')
discriminator = ['route', name, xhr, request_method, path_info,
request_param, header, accept]
diff --git a/pyramid/tests/test_config.py b/pyramid/tests/test_config.py
index 7f103251b..3423b9784 100644
--- a/pyramid/tests/test_config.py
+++ b/pyramid/tests/test_config.py
@@ -1996,54 +1996,29 @@ class ConfiguratorTests(unittest.TestCase):
self._assertRoute(config, 'name', 'path')
self.assertEqual(route.name, 'name')
- def test_add_route_with_root_route(self):
+ def test_add_route_with_route_prefix(self):
from pyramid.interfaces import IRoutesMapper
- root_config = self._makeOne(autocommit=True)
- root_config.add_route('root_name', 'root')
- config = self._makeOne(registry=root_config.registry, autocommit=True, root_route_name='root_name')
+ config = self._makeOne(autocommit=True)
+ config.route_prefix = 'root'
route = config.add_route('name', 'path')
self.assertEqual(route.name, 'name')
self.assertEqual(route.pattern, 'root/path')
- mapper = config.registry.getUtility(IRoutesMapper)
- routes = mapper.get_routes()
- route = routes[1]
- self.assertEqual(len(routes), 2)
- self.assertEqual(route.name, 'name')
- self.assertEqual(route.path, 'root/path')
-
- def test_add_route_with_root_route_configrationerror(self):
- from pyramid.exceptions import ConfigurationError
- config = self._makeOne(autocommit=True, root_route_name='root_name')
- try:
- route = config.add_route('name', 'path')
- self.fail()
- except ConfigurationError:
- pass
+ self._assertRoute(config, 'name', 'root/path')
- def test_with_root_route(self):
+ def test_with_route_prefix(self):
root_config = self._makeOne(autocommit=True)
- root_config.add_route('root_name', 'root')
- config = root_config.with_root_route('root_name')
+ config = root_config.with_route_prefix('root')
self.assertEqual(config.registry, root_config.registry)
self.assertEqual(config.package, root_config.package)
- self.assertEqual(config.root_route_name, 'root_name')
-
- def test_with_root_route_configerror(self):
- from pyramid.exceptions import ConfigurationError
- root_config = self._makeOne(autocommit=True)
- try:
- config = root_config.with_root_route('root_name')
- self.fail()
- except ConfigurationError:
- pass
+ self.assertEqual(config.route_prefix, 'root')
- def test_mount(self):
+ def test_mount_subapplication(self):
root_config = self._makeOne(autocommit=True)
- root_config.add_route('root_name', 'root')
def dummy_subapp(config):
- self.assertEqual(config.root_route_name, 'root_name')
- root_config.mount(dummy_subapp, 'root_name')
+ self.assertEqual(config.route_prefix, 'root')
+ root_config.mount_subapplication(dummy_subapp,
+ route_prefix='root')
def test_add_route_with_factory(self):
config = self._makeOne(autocommit=True)