diff options
| author | Atsushi Odagiri <aodagx@gmail.com> | 2011-07-02 13:31:39 +0900 |
|---|---|---|
| committer | Atsushi Odagiri <aodagx@gmail.com> | 2011-07-02 13:31:39 +0900 |
| commit | 762d7196430a681e2fd5e55c253e3e2ea04847cc (patch) | |
| tree | b771fa86cb8c62b7d718a873530209dccbd13b7d | |
| parent | e21ed88a6dccc1b6f7fee825c6e7afa6d22bd51e (diff) | |
| download | pyramid-762d7196430a681e2fd5e55c253e3e2ea04847cc.tar.gz pyramid-762d7196430a681e2fd5e55c253e3e2ea04847cc.tar.bz2 pyramid-762d7196430a681e2fd5e55c253e3e2ea04847cc.zip | |
mount subapplication
| -rw-r--r-- | pyramid/config.py | 52 | ||||
| -rw-r--r-- | pyramid/tests/test_config.py | 51 |
2 files changed, 102 insertions, 1 deletions
diff --git a/pyramid/config.py b/pyramid/config.py index bf3793c26..934f56050 100644 --- a/pyramid/config.py +++ b/pyramid/config.py @@ -293,6 +293,7 @@ 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() @@ -302,6 +303,7 @@ 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 @@ -601,6 +603,50 @@ 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 + configurator = self.__class__(registry=self.registry, + package=self.package, + autocommit=self.autocommit, + root_route_name=route_name) + return configurator + + def mount(self, function, route_name): + """ mount subapplication on route named ``route_name``. + + + .. code-block:: python + :linenos: + + from pyramid.config import Configurator + + def main(global_config, **settings): + config = Configurator() + config.add_route('admin', '/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: + + .. code-block:: python + :linenos: + + from pyramid.config import Configurator + + def includeme(config): + config.add_route('projects', 'projects') + + + Subapplication's routes are registerd under the ``root_route``. + In this case, ``projects`` route is registered with ``/admin/projects`` pattern. + """ + function = self.maybe_dotted(function) + config = self.with_root_route(route_name) + function(config) + def add_directive(self, name, directive, action_wrap=True): """ Add a directive method to the configurator. @@ -1875,6 +1921,12 @@ 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() + discriminator = ['route', name, xhr, request_method, path_info, request_param, header, accept] discriminator.extend(sorted(custom_predicates)) diff --git a/pyramid/tests/test_config.py b/pyramid/tests/test_config.py index 20fdd93e8..07d44f176 100644 --- a/pyramid/tests/test_config.py +++ b/pyramid/tests/test_config.py @@ -1975,6 +1975,55 @@ class ConfiguratorTests(unittest.TestCase): self._assertRoute(config, 'name', 'path') self.assertEqual(route.name, 'name') + def test_add_route_with_root_route(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') + 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 + + def test_with_root_route(self): + root_config = self._makeOne(autocommit=True) + root_config.add_route('root_name', 'root') + config = root_config.with_root_route('root_name') + 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 + + def test_mount(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') + def test_add_route_with_factory(self): config = self._makeOne(autocommit=True) factory = object() @@ -5263,7 +5312,7 @@ def dummy_extend(config, discrim): def dummy_extend2(config, discrim): config.action(discrim, None, config.registry) - + class DummyRegistry(object): def __init__(self, adaptation=None): self.utilities = [] |
