Navigation Menu

Skip to content

Commit

Permalink
- Pyramid did not properly generate static URLs using
Browse files Browse the repository at this point in the history
  ``pyramid.url.static_url`` when passed a caller-package relative path due
  to a refactoring.

Closes #258.
  • Loading branch information
mcdonc committed Aug 29, 2011
1 parent 62a48b5 commit b5c0cb9
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
7 changes: 7 additions & 0 deletions 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
--------

Expand Down
38 changes: 33 additions & 5 deletions pyramid/tests/test_url.py
Expand Up @@ -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):
Expand All @@ -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
Expand Down
14 changes: 14 additions & 0 deletions pyramid/url.py
Expand Up @@ -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):
Expand All @@ -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):
Expand Down

0 comments on commit b5c0cb9

Please sign in to comment.