router: Correctly generate application-relative URLs

master
Dustin C. Hatch 2011-06-15 19:12:00 -05:00
parent 648623772c
commit 0ab44ed7ea
1 changed files with 8 additions and 8 deletions

View File

@ -64,14 +64,14 @@ class Router(object):
class REDIRECT(object): pass
class SILENT(object): pass
#: Compiled regular expression for variable segments
template_re = re.compile(r'\{(\w+)(?::([^}]+))?\}')
def __init__(self, trailing_slash=REDIRECT):
self.routes = []
self._cache = {}
self.trailing_slash=trailing_slash
self.trailing_slash = trailing_slash
def resolve(self, path_info):
'''Find a controller for a given path
@ -138,7 +138,7 @@ class Router(object):
Converts the ``{name}`` or ``{name:regexp}`` syntax into a full
regular expression for later parsing.
'''
regex = ''
last_pos = 0
for match in self.template_re.finditer(template):
@ -154,7 +154,7 @@ class Router(object):
def _import_controller(self, name):
'''Resolves a string Python path to a callable'''
module_name, func_name = name.split(':', 1)
__import__(module_name)
module = sys.modules[module_name]
@ -200,7 +200,7 @@ class Router(object):
This string will resolve to the function ``function`` in the
module ``some.module``.
'''
if isinstance(controller, basestring):
controller = self._import_controller(controller)
self.routes.append((self._compile_template(template),
@ -229,10 +229,10 @@ class Generator(object):
def generate(self, *segments, **vars):
'''Combines segments and the application's URL into a new URL
'''
path = '/'.join(str(s) for s in segments)
if not path.startswith('/'):
path = '/' + path
while path.startswith('/'):
path = path[1:]
url = self.request.relative_url(path, to_application=True)
if self.path_only: