From b5c0cb969c5b717e2cc95aa8738aa904f8dc2ccd Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Mon, 29 Aug 2011 09:53:40 -0400 Subject: [PATCH] - Pyramid did not properly generate static URLs using ``pyramid.url.static_url`` when passed a caller-package relative path due to a refactoring. Closes #258. --- CHANGES.txt | 7 +++++++ pyramid/tests/test_url.py | 38 +++++++++++++++++++++++++++++++++----- pyramid/url.py | 14 ++++++++++++++ 3 files changed, 54 insertions(+), 5 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 7fed402b1f..48ef24417f 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,6 +1,13 @@ Next release ============ +Bug Fixes +--------- + +- Pyramid did not properly generate static URLs using + ``pyramid.url.static_url`` when passed a caller-package relative path due + to a refactoring. + Internal -------- diff --git a/pyramid/tests/test_url.py b/pyramid/tests/test_url.py index 4d113f0de3..a23b75e73f 100644 --- a/pyramid/tests/test_url.py +++ b/pyramid/tests/test_url.py @@ -528,11 +528,25 @@ def static_url(self, path, **kw): return 'static url' return Request() - def test_it(self): + def test_it_abs(self): + request = self._makeRequest() + result = self._callFUT('/foo/bar/abc', request, _app_url='') + self.assertEqual(result, 'static url') + self.assertEqual(request.path, '/foo/bar/abc') + self.assertEqual(request.kw, {'_app_url':''}) + + def test_it_absspec(self): + request = self._makeRequest() + result = self._callFUT('foo:abc', request, _anchor='anchor') + self.assertEqual(result, 'static url') + self.assertEqual(request.path, 'foo:abc') + self.assertEqual(request.kw, {'_anchor':'anchor'}) + + def test_it_rel(self): request = self._makeRequest() result = self._callFUT('abc', request, _app_url='') self.assertEqual(result, 'static url') - self.assertEqual(request.path, 'abc') + self.assertEqual(request.path, 'pyramid.tests:abc') self.assertEqual(request.kw, {'_app_url':''}) class Test_static_path(unittest.TestCase): @@ -548,13 +562,27 @@ def static_path(self, path, **kw): return 'static path' return Request() - def test_it(self): + def test_it_abs(self): request = self._makeRequest() - result = self._callFUT('abc', request, _anchor='anchor') + result = self._callFUT('/foo/bar/abc', request, _anchor='anchor') self.assertEqual(result, 'static path') - self.assertEqual(request.path, 'abc') + self.assertEqual(request.path, '/foo/bar/abc') self.assertEqual(request.kw, {'_anchor':'anchor'}) + def test_it_absspec(self): + request = self._makeRequest() + result = self._callFUT('foo:abc', request, _anchor='anchor') + self.assertEqual(result, 'static path') + self.assertEqual(request.path, 'foo:abc') + self.assertEqual(request.kw, {'_anchor':'anchor'}) + + def test_it_rel(self): + request = self._makeRequest() + result = self._callFUT('abc', request, _app_url='') + self.assertEqual(result, 'static path') + self.assertEqual(request.path, 'pyramid.tests:abc') + self.assertEqual(request.kw, {'_app_url':''}) + class Test_current_route_url(unittest.TestCase): def _callFUT(self, request, *elements, **kw): from pyramid.url import current_route_url diff --git a/pyramid/url.py b/pyramid/url.py index 58557c1e42..a3059fa879 100644 --- a/pyramid/url.py +++ b/pyramid/url.py @@ -543,6 +543,13 @@ def static_url(path, request, **kw): See :meth:`pyramid.request.Request.static_url` for more information. """ + if not os.path.isabs(path): + if not ':' in path: + # if it's not a package:relative/name and it's not an + # /absolute/path it's a relative/path; this means its relative + # to the package in which the caller's module is defined. + package = caller_package() + path = '%s:%s' % (package.__name__, path) return request.static_url(path, **kw) def static_path(path, request, **kw): @@ -554,6 +561,13 @@ def static_path(path, request, **kw): See :meth:`pyramid.request.Request.static_path` for more information. """ + if not os.path.isabs(path): + if not ':' in path: + # if it's not a package:relative/name and it's not an + # /absolute/path it's a relative/path; this means its relative + # to the package in which the caller's module is defined. + package = caller_package() + path = '%s:%s' % (package.__name__, path) return request.static_path(path, **kw) def current_route_url(request, *elements, **kw):