summaryrefslogtreecommitdiff
path: root/repoze
diff options
context:
space:
mode:
authorChris McDonough <chrism@agendaless.com>2009-06-24 06:10:14 +0000
committerChris McDonough <chrism@agendaless.com>2009-06-24 06:10:14 +0000
commit5a11c03834cf4aedf2c21c050d2dea0b2d229076 (patch)
tree9f0caf887678c0248d59a35f38597a3c50e6c32e /repoze
parent5e3e39bfb3fe6d5252e428f2eea4e34498b24ba1 (diff)
downloadpyramid-5a11c03834cf4aedf2c21c050d2dea0b2d229076.tar.gz
pyramid-5a11c03834cf4aedf2c21c050d2dea0b2d229076.tar.bz2
pyramid-5a11c03834cf4aedf2c21c050d2dea0b2d229076.zip
Change the implementation and the signature for ``route_url``.
Diffstat (limited to 'repoze')
-rw-r--r--repoze/bfg/tests/test_url.py77
-rw-r--r--repoze/bfg/url.py28
2 files changed, 65 insertions, 40 deletions
diff --git a/repoze/bfg/tests/test_url.py b/repoze/bfg/tests/test_url.py
index e42089173..3c5bd7559 100644
--- a/repoze/bfg/tests/test_url.py
+++ b/repoze/bfg/tests/test_url.py
@@ -158,48 +158,41 @@ class UrlEncodeTests(unittest.TestCase):
self.assertEqual(result, 'a=1')
class TestRouteUrl(unittest.TestCase):
+ def setUp(self):
+ cleanUp()
+
+ def tearDown(self):
+ cleanUp()
+
def _callFUT(self, *arg, **kw):
from repoze.bfg.url import route_url
return route_url(*arg, **kw)
def test_it(self):
- from routes import Mapper
- mapper = Mapper(controller_scan=None, directory=None,
- explicit=True, always_scan=False)
+ from repoze.bfg.interfaces import IRoutesMapper
+ mapper = DummyRoutesMapper({'flub':DummyRoute({})})
+ from zope.component import getSiteManager
+ sm = getSiteManager()
+ sm.registerUtility(mapper, IRoutesMapper)
args = {'a':'1', 'b':'2', 'c':'3'}
- mapper.connect('flub', ':a/:b/:c')
- mapper.create_regs([])
environ = {'SERVER_NAME':'example.com', 'wsgi.url_scheme':'http',
'SERVER_PORT':'80', 'wsgiorg.routing_args':((), args)}
- mapper.environ = environ
- from routes import request_config
- config = request_config()
- config.environ = environ
- config.mapper = mapper
- config.redirect = None
- request = DummyRequest()
- request.environ = environ
- result = self._callFUT('flub', a=1, b=2, c=3)
+ request = DummyRequest(environ)
+ result = self._callFUT(request, 'flub', a=1, b=2, c=3)
self.assertEqual(result, 'http://example.com/1/2/3')
def test_it_generation_error(self):
- from routes import Mapper
- mapper = Mapper(controller_scan=None, directory=None,
- explicit=True, always_scan=False)
+ from repoze.bfg.interfaces import IRoutesMapper
+ mapper = DummyRoutesMapper({'flub':DummyRoute({})})
+ from zope.component import getSiteManager
+ sm = getSiteManager()
+ sm.registerUtility(mapper, IRoutesMapper)
args = {'a':'1', 'b':'2', 'c':'3'}
- mapper.connect('flub', ':a/:b/:c')
- mapper.create_regs([])
+ mapper.raise_exc = True
environ = {'SERVER_NAME':'example.com', 'wsgi.url_scheme':'http',
'SERVER_PORT':'80', 'wsgiorg.routing_args':((), args)}
- mapper.environ = environ
- from routes import request_config
- config = request_config()
- config.environ = environ
- config.mapper = mapper
- config.redirect = None
- request = DummyRequest()
- request.environ = environ
- self.assertRaises(ValueError, self._callFUT, 'flub', a=1)
+ request = DummyRequest(environ)
+ self.assertRaises(ValueError, self._callFUT, request, 'flub', a=1)
class DummyContext(object):
def __init__(self, next=None):
@@ -207,4 +200,30 @@ class DummyContext(object):
class DummyRequest:
application_url = 'http://example.com:5432' # app_url never ends with slash
-
+ def __init__(self, environ=None):
+ if environ is None:
+ environ = {}
+ self.environ = environ
+
+class DummyRoutesMapper:
+ encoding = 'utf-8'
+ hardcode_names = False
+ sub_domains = []
+ raise_exc = False
+ def __init__(self, routes, generate_result='/1/2/3', raise_exc=False):
+ self._routenames = routes
+ self.generate_result = generate_result
+
+ def generate(self, *route_args, **newargs):
+ if self.raise_exc:
+ from routes.util import GenerationException
+ raise GenerationException
+ return self.generate_result
+
+class DummyRoute:
+ filter = None
+ static = False
+ def __init__(self, defaults):
+ self.defaults = defaults
+
+
diff --git a/repoze/bfg/url.py b/repoze/bfg/url.py
index d3fc55203..f9927c431 100644
--- a/repoze/bfg/url.py
+++ b/repoze/bfg/url.py
@@ -3,28 +3,32 @@
import urllib
from zope.component import queryMultiAdapter
+from zope.component import getUtility
from repoze.bfg.interfaces import IContextURL
+from repoze.bfg.interfaces import IRoutesMapper
from repoze.bfg.traversal import TraversalContextURL
from repoze.bfg.traversal import quote_path_segment
-from routes import url_for
+from routes import URLGenerator
from routes.util import GenerationException
-def route_url(*arg, **kw):
+def route_url(request, route_name, **kw):
"""Generates a fully qualified URL for a named BFG route.
- Use the route's ``name`` as the first positional argument. Use
- keyword arguments to supply values which match any dynamic path
- elements in the route definition. Raises a ValueError exception
- if the URL cannot be generated when the
+ Use the request object as the first positional argument. Use the
+ route's ``name`` as the second positional argument. Use keyword
+ arguments to supply values which match any dynamic path elements
+ in the route definition. Raises a ValueError exception if the URL
+ cannot be generated when the
- For example, if you've defined a route named"foobar" with the path
+ For example, if you've defined a route named "foobar" with the path
``:foo/:bar/*traverse``::
- route_url(foo='1') => <ValueError exception>
- route_url(foo='1', bar='2') => <ValueError exception>
- route_url(foo='1', bar='2',traverse='a/b) => http://e.com/1/2/a/b
+ route_url(request, 'foobar', foo='1') => <ValueError exception>
+ route_url(request, 'foobar', foo='1', bar='2') => <ValueError exception>
+ route_url('foobar', foo='1', bar='2',
+ 'traverse='a/b) => http://e.com/1/2/a/b
All keys given to ``route_url`` are sent to the BFG Routes "mapper"
instance for generation except for::
@@ -38,7 +42,9 @@ def route_url(*arg, **kw):
if not 'qualified' in kw:
kw['qualified'] = True
try:
- return url_for(*arg, **kw)
+ mapper = getUtility(IRoutesMapper)
+ generator = URLGenerator(mapper, request.environ)
+ return generator(route_name, **kw)
except GenerationException, why:
raise ValueError(str(why))