From e58daf95b98a76f2b58a42568a15e8c4ad4f59ae Mon Sep 17 00:00:00 2001 From: Atsushi Odagiri Date: Tue, 5 Jul 2011 19:53:30 +0900 Subject: mount subapplication with route_prefix --- pyramid/config.py | 30 ++++++++++------------------ pyramid/tests/test_config.py | 47 +++++++++++--------------------------------- 2 files changed, 21 insertions(+), 56 deletions(-) diff --git a/pyramid/config.py b/pyramid/config.py index 78dd78a4f..0724d10ae 100644 --- a/pyramid/config.py +++ b/pyramid/config.py @@ -293,7 +293,6 @@ class Configurator(object): default_view_mapper=None, autocommit=False, exceptionresponse_view=default_exceptionresponse_view, - root_route_name=None, ): if package is None: package = caller_package() @@ -303,7 +302,6 @@ class Configurator(object): self.package = name_resolver.package self.registry = registry self.autocommit = autocommit - self.root_route_name = root_route_name if registry is None: registry = Registry(self.package_name) self.registry = registry @@ -603,19 +601,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 @@ -625,8 +619,7 @@ 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: @@ -640,11 +633,11 @@ class Configurator(object): 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): @@ -1922,11 +1915,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 07d44f176..63644da2d 100644 --- a/pyramid/tests/test_config.py +++ b/pyramid/tests/test_config.py @@ -1975,54 +1975,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) -- cgit v1.2.3