Replace Request.relative_url with Request.create_url and friends
parent
11271ebc31
commit
c4e9397e8c
|
@ -61,25 +61,44 @@ class Request(webob.Request):
|
||||||
req.config = {}
|
req.config = {}
|
||||||
return req
|
return req
|
||||||
|
|
||||||
def relative_url(self, other_url, to_application=True, path_only=True,
|
def create_href(self, path, **keywords):
|
||||||
**vars): #@ReservedAssignment
|
'''Combine the application's path with a path to form an HREF
|
||||||
'''Create a new URL relative to the request URL
|
|
||||||
|
|
||||||
:param other_url: relative path to join with the request URL
|
:param path: relative path to join with the request URL
|
||||||
:param to_application: If true, generated URL will be relative
|
|
||||||
to the application's root path, otherwise relative to the
|
|
||||||
server root
|
|
||||||
:param path_only: If true, scheme and host will be omitted
|
|
||||||
|
|
||||||
Any other keyword arguments will be encoded and appended to the URL
|
Any other keyword arguments will be encoded and appended to the URL
|
||||||
as querystring arguments.
|
as querystring arguments.
|
||||||
|
|
||||||
|
The HREF returned will will be the absolute path on the same host
|
||||||
|
and protocol as the request. To get the full URL including scheme
|
||||||
|
and host information, use :py:meth:`create_href_full` instead.
|
||||||
'''
|
'''
|
||||||
|
|
||||||
url = super(Request, self).relative_url(other_url, to_application)
|
url = self._merge_url(self.script_name, path)
|
||||||
if path_only:
|
|
||||||
url = urlparse.urlsplit(url).path
|
if keywords:
|
||||||
if vars:
|
url += '?' + urllib.urlencode(keywords)
|
||||||
url += '?' + urllib.urlencode(vars)
|
|
||||||
|
return url
|
||||||
|
|
||||||
|
def create_href_full(self, path, **keywords):
|
||||||
|
'''Combine the application's full URL with a path to form a new URL
|
||||||
|
|
||||||
|
:param path: relative path to join with the request URL
|
||||||
|
|
||||||
|
Any other keyword arguments will be encoded and appended to the
|
||||||
|
URL as querystring arguments/
|
||||||
|
|
||||||
|
The HREF returned will be the full URL, including scheme and host
|
||||||
|
information. To get the path only, use :py:meth:`create_href`
|
||||||
|
instead.
|
||||||
|
'''
|
||||||
|
|
||||||
|
url = self._merge_url(self.application_url, path)
|
||||||
|
|
||||||
|
if keywords:
|
||||||
|
url += '?' + urllib.urlencode(keywords)
|
||||||
|
|
||||||
return url
|
return url
|
||||||
|
|
||||||
def static_resource(self, path):
|
def static_resource(self, path):
|
||||||
|
@ -114,6 +133,9 @@ class Request(webob.Request):
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return path
|
return path
|
||||||
|
|
||||||
|
return self._merge_url(root, path)
|
||||||
|
|
||||||
|
def _merge_url(self, root, path):
|
||||||
if path.startswith('/'):
|
if path.startswith('/'):
|
||||||
path = path[1:]
|
path = path[1:]
|
||||||
if not root.endswith('/'):
|
if not root.endswith('/'):
|
||||||
|
|
|
@ -223,7 +223,7 @@ class Generator(object):
|
||||||
url = Generator(request).generate
|
url = Generator(request).generate
|
||||||
|
|
||||||
.. deprecated:: 0.2
|
.. deprecated:: 0.2
|
||||||
Use :py:meth:`milla.Request.relative_url` instead.
|
Use :py:meth:`milla.Request.create_href` instead.
|
||||||
'''
|
'''
|
||||||
|
|
||||||
def __init__(self, request, path_only=True):
|
def __init__(self, request, path_only=True):
|
||||||
|
@ -231,7 +231,7 @@ class Generator(object):
|
||||||
self.path_only = path_only
|
self.path_only = path_only
|
||||||
warnings.warn(
|
warnings.warn(
|
||||||
'Use of Generator is deprecated; '
|
'Use of Generator is deprecated; '
|
||||||
'use milla.Request.relative_url instead',
|
'use milla.Request.create_href instead',
|
||||||
DeprecationWarning,
|
DeprecationWarning,
|
||||||
stacklevel=2
|
stacklevel=2
|
||||||
)
|
)
|
||||||
|
@ -241,10 +241,8 @@ class Generator(object):
|
||||||
'''
|
'''
|
||||||
|
|
||||||
path = '/'.join(str(s) for s in segments)
|
path = '/'.join(str(s) for s in segments)
|
||||||
while path.startswith('/'):
|
|
||||||
path = path[1:]
|
|
||||||
|
|
||||||
return self.request.relative_url(path,
|
if self.path_only:
|
||||||
to_application=True,
|
return self.request.create_href(path, **vars)
|
||||||
path_only=self.path_only,
|
else:
|
||||||
**vars)
|
return self.request.create_href_full(path, **vars)
|
||||||
|
|
|
@ -403,3 +403,57 @@ def test_allow_decorator():
|
||||||
return 'success'
|
return 'success'
|
||||||
|
|
||||||
assert controller.allowed_methods == ('GET', 'HEAD', 'POST')
|
assert controller.allowed_methods == ('GET', 'HEAD', 'POST')
|
||||||
|
|
||||||
|
def test_create_href_simple():
|
||||||
|
'''Request.create_href creates a valid URL path from the application root'''
|
||||||
|
|
||||||
|
environ = environ_for_testing()
|
||||||
|
request = milla.Request(environ)
|
||||||
|
url = request.create_href('/bar')
|
||||||
|
assert url == '/bar', url
|
||||||
|
|
||||||
|
def test_create_href_nonroot():
|
||||||
|
'''Request.create_href handles applications mounted somewhere besides /'''
|
||||||
|
|
||||||
|
environ = environ_for_testing()
|
||||||
|
environ.update({
|
||||||
|
'SCRIPT_NAME': '/test'
|
||||||
|
})
|
||||||
|
request = milla.Request(environ)
|
||||||
|
url = request.create_href('/bar')
|
||||||
|
assert url == '/test/bar', url
|
||||||
|
|
||||||
|
def test_create_href_full():
|
||||||
|
'''Request.create_href_full creates appropriate full URL'''
|
||||||
|
|
||||||
|
environ = environ_for_testing()
|
||||||
|
request = milla.Request(environ)
|
||||||
|
url = request.create_href_full('/bar')
|
||||||
|
assert url == 'http://127.0.0.1/bar', url
|
||||||
|
|
||||||
|
def test_create_href_full_nonroot():
|
||||||
|
'''Request.create_href_full creates correct full URL for nonroot applications'''
|
||||||
|
|
||||||
|
environ = environ_for_testing()
|
||||||
|
environ.update({
|
||||||
|
'SCRIPT_NAME': '/test'
|
||||||
|
})
|
||||||
|
request = milla.Request(environ)
|
||||||
|
url = request.create_href_full('/bar')
|
||||||
|
assert url == 'http://127.0.0.1/test/bar', url
|
||||||
|
|
||||||
|
def test_create_href_keywords():
|
||||||
|
'''Request.create_href properly appends querystring arguments'''
|
||||||
|
|
||||||
|
environ = environ_for_testing()
|
||||||
|
request = milla.Request(environ)
|
||||||
|
url = request.create_href('/bar', foo='baz')
|
||||||
|
assert url == '/bar?foo=baz'
|
||||||
|
|
||||||
|
def test_create_href_full_keywords():
|
||||||
|
'''Request.create_href_full properly appends querystring arguments'''
|
||||||
|
|
||||||
|
environ = environ_for_testing()
|
||||||
|
request = milla.Request(environ)
|
||||||
|
url = request.create_href_full('/bar', foo='baz')
|
||||||
|
assert url == 'http://127.0.0.1/bar?foo=baz'
|
||||||
|
|
Loading…
Reference in New Issue