Fixed #17857 -- Stopped CachedStaticFilesStorage from creating absolute URLs unnecessarily. Thanks, tgecho.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17697 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jannis Leidel 2012-03-13 03:48:11 +00:00
parent 7487c74020
commit 0ee801e360
3 changed files with 15 additions and 14 deletions

View File

@ -165,9 +165,11 @@ class CachedFilesMixin(object):
start, end = 1, sub_level - 1 start, end = 1, sub_level - 1
joined_result = '/'.join(name_parts[:-start] + url_parts[end:]) joined_result = '/'.join(name_parts[:-start] + url_parts[end:])
hashed_url = self.url(unquote(joined_result), force=True) hashed_url = self.url(unquote(joined_result), force=True)
file_name = hashed_url.split('/')[-1:]
relative_url = '/'.join(url.split('/')[:-1] + file_name)
# Return the hashed and normalized version to the file # Return the hashed version to the file
return 'url("%s")' % unquote(hashed_url) return 'url("%s")' % unquote(relative_url)
return converter return converter
def post_process(self, paths, dry_run=False, **options): def post_process(self, paths, dry_run=False, **options):

View File

@ -327,7 +327,7 @@ CachedStaticFilesStorage
.. code-block:: css+django .. code-block:: css+django
@import url("/static/admin/css/base.27e20196a850.css"); @import url("../admin/css/base.27e20196a850.css");
To enable the ``CachedStaticFilesStorage`` you have to make sure the To enable the ``CachedStaticFilesStorage`` you have to make sure the
following requirements are met: following requirements are met:

View File

@ -385,7 +385,7 @@ class TestCollectionCachedStorage(BaseCollectionTestCase,
with storage.staticfiles_storage.open(relpath) as relfile: with storage.staticfiles_storage.open(relpath) as relfile:
content = relfile.read() content = relfile.read()
self.assertNotIn("cached/other.css", content) self.assertNotIn("cached/other.css", content)
self.assertIn("/static/cached/other.d41d8cd98f00.css", content) self.assertIn("other.d41d8cd98f00.css", content)
def test_path_with_querystring(self): def test_path_with_querystring(self):
relpath = self.cached_file_path("cached/styles.css?spam=eggs") relpath = self.cached_file_path("cached/styles.css?spam=eggs")
@ -395,7 +395,7 @@ class TestCollectionCachedStorage(BaseCollectionTestCase,
"cached/styles.93b1147e8552.css") as relfile: "cached/styles.93b1147e8552.css") as relfile:
content = relfile.read() content = relfile.read()
self.assertNotIn("cached/other.css", content) self.assertNotIn("cached/other.css", content)
self.assertIn("/static/cached/other.d41d8cd98f00.css", content) self.assertIn("other.d41d8cd98f00.css", content)
def test_path_with_fragment(self): def test_path_with_fragment(self):
relpath = self.cached_file_path("cached/styles.css#eggs") relpath = self.cached_file_path("cached/styles.css#eggs")
@ -404,15 +404,15 @@ class TestCollectionCachedStorage(BaseCollectionTestCase,
"cached/styles.93b1147e8552.css") as relfile: "cached/styles.93b1147e8552.css") as relfile:
content = relfile.read() content = relfile.read()
self.assertNotIn("cached/other.css", content) self.assertNotIn("cached/other.css", content)
self.assertIn("/static/cached/other.d41d8cd98f00.css", content) self.assertIn("other.d41d8cd98f00.css", content)
def test_path_with_querystring_and_fragment(self): def test_path_with_querystring_and_fragment(self):
relpath = self.cached_file_path("cached/css/fragments.css") relpath = self.cached_file_path("cached/css/fragments.css")
self.assertEqual(relpath, "cached/css/fragments.75433540b096.css") self.assertEqual(relpath, "cached/css/fragments.75433540b096.css")
with storage.staticfiles_storage.open(relpath) as relfile: with storage.staticfiles_storage.open(relpath) as relfile:
content = relfile.read() content = relfile.read()
self.assertIn('/static/cached/css/fonts/font.a4b0478549d0.eot?#iefix', content) self.assertIn('fonts/font.a4b0478549d0.eot?#iefix', content)
self.assertIn('/static/cached/css/fonts/font.b8d603e42714.svg#webfontIyfZbseF', content) self.assertIn('fonts/font.b8d603e42714.svg#webfontIyfZbseF', content)
self.assertIn('data:font/woff;charset=utf-8;base64,d09GRgABAAAAADJoAA0AAAAAR2QAAQAAAAAAAAAAAAA', content) self.assertIn('data:font/woff;charset=utf-8;base64,d09GRgABAAAAADJoAA0AAAAAR2QAAQAAAAAAAAAAAAA', content)
self.assertIn('#default#VML', content) self.assertIn('#default#VML', content)
@ -431,21 +431,20 @@ class TestCollectionCachedStorage(BaseCollectionTestCase,
with storage.staticfiles_storage.open(relpath) as relfile: with storage.staticfiles_storage.open(relpath) as relfile:
content = relfile.read() content = relfile.read()
self.assertNotIn("..//cached///styles.css", content) self.assertNotIn("..//cached///styles.css", content)
self.assertIn("/static/cached/styles.93b1147e8552.css", content) self.assertIn("../cached/styles.93b1147e8552.css", content)
self.assertNotIn("url(img/relative.png )", content) self.assertNotIn("url(img/relative.png )", content)
self.assertIn("/static/cached/img/relative.acae32e4532b.png", content) self.assertIn('url("img/relative.acae32e4532b.png', content)
def test_template_tag_relative(self): def test_template_tag_relative(self):
relpath = self.cached_file_path("cached/relative.css") relpath = self.cached_file_path("cached/relative.css")
self.assertEqual(relpath, "cached/relative.2217ea7273c2.css") self.assertEqual(relpath, "cached/relative.2217ea7273c2.css")
with storage.staticfiles_storage.open(relpath) as relfile: with storage.staticfiles_storage.open(relpath) as relfile:
content = relfile.read() content = relfile.read()
self.assertIn("/static/cached/styles.93b1147e8552.css", content)
self.assertNotIn("../cached/styles.css", content) self.assertNotIn("../cached/styles.css", content)
self.assertNotIn('@import "styles.css"', content) self.assertNotIn('@import "styles.css"', content)
self.assertNotIn('url(img/relative.png)', content) self.assertNotIn('url(img/relative.png)', content)
self.assertIn('url("/static/cached/img/relative.acae32e4532b.png")', content) self.assertIn('url("img/relative.acae32e4532b.png")', content)
self.assertIn("/static/cached/styles.93b1147e8552.css", content) self.assertIn("../cached/styles.93b1147e8552.css", content)
def test_template_tag_deep_relative(self): def test_template_tag_deep_relative(self):
relpath = self.cached_file_path("cached/css/window.css") relpath = self.cached_file_path("cached/css/window.css")
@ -453,7 +452,7 @@ class TestCollectionCachedStorage(BaseCollectionTestCase,
with storage.staticfiles_storage.open(relpath) as relfile: with storage.staticfiles_storage.open(relpath) as relfile:
content = relfile.read() content = relfile.read()
self.assertNotIn('url(img/window.png)', content) self.assertNotIn('url(img/window.png)', content)
self.assertIn('url("/static/cached/css/img/window.acae32e4532b.png")', content) self.assertIn('url("img/window.acae32e4532b.png")', content)
def test_template_tag_url(self): def test_template_tag_url(self):
relpath = self.cached_file_path("cached/url.css") relpath = self.cached_file_path("cached/url.css")