From b0698f0ddc94b83526f056493d29552c805ee6b3 Mon Sep 17 00:00:00 2001 From: Pete Browne Date: Thu, 7 Feb 2013 10:39:28 -0600 Subject: [PATCH] Add #static templatetag. fixes #2 --- assetfiles/__init__.py | 2 +- assetfiles/templatetags/__init__.py | 0 assetfiles/templatetags/staticfiles.py | 4 ++ assetfiles/tests/test_templatetags.py | 52 ++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 assetfiles/templatetags/__init__.py create mode 100644 assetfiles/templatetags/staticfiles.py create mode 100644 assetfiles/tests/test_templatetags.py diff --git a/assetfiles/__init__.py b/assetfiles/__init__.py index abeeedb..2b8877c 100644 --- a/assetfiles/__init__.py +++ b/assetfiles/__init__.py @@ -1 +1 @@ -__version__ = '0.4.0' +__version__ = '0.5.0' diff --git a/assetfiles/templatetags/__init__.py b/assetfiles/templatetags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/assetfiles/templatetags/staticfiles.py b/assetfiles/templatetags/staticfiles.py new file mode 100644 index 0000000..ed11118 --- /dev/null +++ b/assetfiles/templatetags/staticfiles.py @@ -0,0 +1,4 @@ +from django import template +from django.contrib.staticfiles.templatetags import staticfiles + +register = staticfiles.register diff --git a/assetfiles/tests/test_templatetags.py b/assetfiles/tests/test_templatetags.py new file mode 100644 index 0000000..597f9c4 --- /dev/null +++ b/assetfiles/tests/test_templatetags.py @@ -0,0 +1,52 @@ +from django.template import loader, Context +from django.test.utils import override_settings +from django.utils import six + +from assetfiles.tests.base import AssetfilesTestCase + + +class BaseTemplateTagsTestCase(AssetfilesTestCase): + def render_template(self, template, **kwargs): + if isinstance(template, six.string_types): + template = loader.get_template_from_string(template) + return template.render(Context(kwargs)).strip() + + def static_template_snippet(self, path, asvar=False): + return "{%% load static from staticfiles %%}{%% static '%s' %%}" % path + + def assertStaticRenders(self, path, result, asvar=False, **kwargs): + template = self.static_template_snippet(path, asvar) + self.assertEqual(self.render_template(template, **kwargs), result) + + +class TestTemplateTag(BaseTemplateTagsTestCase): + def test_template_tag(self): + self.assertStaticRenders('does/not/exist.png', + '/static/does/not/exist.png') + self.assertStaticRenders('testfile.txt', '/static/testfile.txt') + self.assertStaticRenders('testfile.txt', '/static/testfile.txt') + + +@override_settings( + STATICFILES_STORAGE='django.contrib.staticfiles.storage.CachedStaticFilesStorage', + DEBUG=False, +) +class TestStaticTagWithCachedStorage(BaseTemplateTagsTestCase): + def test_raises_error_without_a_file(self): + self.assertRaises(ValueError, + self.assertStaticRenders, + 'does/not/exist.png', + '/static/does/not/exist.png') + + def test_does_not_change_folder_names(self): + self.assertStaticRenders('path/', + '/static/path/') + self.assertStaticRenders('path/?query', + '/static/path/?query') + + def test_returns_file_with_hash_name(self): + self.mkfile('public/test/file.txt', 'Some Content') + self.assertStaticRenders('test/file.txt', + '/static/test/file.78138d2003f1.txt') + self.assertStaticRenders('test/file.txt', + '/static/test/file.78138d2003f1.txt')