From 0ab44ed7eabce18b777b8fd49f7b7ca05ce37595 Mon Sep 17 00:00:00 2001 From: "Dustin C. Hatch" Date: Wed, 15 Jun 2011 19:12:00 -0500 Subject: [PATCH] router: Correctly generate application-relative URLs --- src/milla/dispatch/routing.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/milla/dispatch/routing.py b/src/milla/dispatch/routing.py index 997d060..0ef4e6f 100644 --- a/src/milla/dispatch/routing.py +++ b/src/milla/dispatch/routing.py @@ -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: