diff options
| author | Chris McDonough <chrism@agendaless.com> | 2009-06-11 03:15:15 +0000 |
|---|---|---|
| committer | Chris McDonough <chrism@agendaless.com> | 2009-06-11 03:15:15 +0000 |
| commit | dfc2b65c1b6d2f938f68b7868a14d8f9a4faab9e (patch) | |
| tree | f3241401b7175a401e00286b11e3efe3c21f5093 /repoze/bfg/traversal.py | |
| parent | f8b0065b6ede54424d7a7b49f9f113e87634b5ab (diff) | |
| download | pyramid-dfc2b65c1b6d2f938f68b7868a14d8f9a4faab9e.tar.gz pyramid-dfc2b65c1b6d2f938f68b7868a14d8f9a4faab9e.tar.bz2 pyramid-dfc2b65c1b6d2f938f68b7868a14d8f9a4faab9e.zip | |
Merge unifyroutesandtraversal branch into trunk
Diffstat (limited to 'repoze/bfg/traversal.py')
| -rw-r--r-- | repoze/bfg/traversal.py | 46 |
1 files changed, 38 insertions, 8 deletions
diff --git a/repoze/bfg/traversal.py b/repoze/bfg/traversal.py index b8ba68faa..a0fdc5c71 100644 --- a/repoze/bfg/traversal.py +++ b/repoze/bfg/traversal.py @@ -489,10 +489,31 @@ class ModelGraphTraverser(object): self.root = root def __call__(self, environ, _marker=_marker): - try: - path = environ['PATH_INFO'] - except KeyError: - path = '/' + if 'bfg.routes.matchdict' in environ: + # this request matched a Routes route + matchdict = environ['bfg.routes.matchdict'] + if 'path_info' in matchdict: + # this is stolen from routes.middleware; if the route map + # has a *path_info capture, use it to influence the path + # info and script_name of the generated environment + oldpath = environ['PATH_INFO'] + newpath = matchdict['path_info'] or '' + environ['PATH_INFO'] = newpath + if not environ['PATH_INFO'].startswith('/'): + environ['PATH_INFO'] = '/' + environ['PATH_INFO'] + pattern = r'^(.*?)/' + re.escape(newpath) + '$' + environ['SCRIPT_NAME'] += re.sub(pattern, r'\1', oldpath) + if environ['SCRIPT_NAME'].endswith('/'): + environ['SCRIPT_NAME'] = environ['SCRIPT_NAME'][:-1] + path = matchdict.get('traverse', '/') + subpath = filter(None, matchdict.get('subpath', '').split('/')) + else: + # this request did not match a Routes route + subpath = [] + try: + path = environ['PATH_INFO'] + except KeyError: + path = '/' try: vroot_path_string = environ[VH_ROOT_KEY] except KeyError: @@ -535,8 +556,9 @@ class ModelGraphTraverser(object): ob = next i += 1 - return dict(context=ob, view_name=u'', subpath=[], traversed=traversed, - virtual_root=vroot, virtual_root_path=vroot_path, + return dict(context=ob, view_name=u'', subpath=subpath, + traversed=traversed, virtual_root=vroot, + virtual_root_path=vroot_path, root=self.root) class TraversalContextURL(object): @@ -584,8 +606,16 @@ class TraversalContextURL(object): else: if path.startswith(vroot_path): path = path[len(vroot_path):] - - app_url = request.application_url # never ends in a slash + + environ = request.environ + if 'bfg.routes.route' in environ: + matchdict = environ['bfg.routes.matchdict'].copy() + matchdict['traverse'] = path + route = environ['bfg.routes.route'] + app_url = route.generate(**matchdict) + else: + app_url = request.application_url # never ends in a slash + return app_url + path always_safe = ('ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
