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