Skip to content
This repository was archived by the owner on Feb 28, 2023. It is now read-only.

made cache busting optional #6

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions django_gears/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@

GEARS_DEBUG = getattr(settings, 'GEARS_DEBUG', settings.DEBUG)

# Controls whether or not a query string with the file's modified time is
# appended to the file path.
GEARS_CACHE_BUST = getattr(settings, 'GEARS_CACHE_BUST', GEARS_DEBUG)

GEARS_URL = getattr(settings, 'GEARS_URL', settings.STATIC_URL)


Expand Down
23 changes: 16 additions & 7 deletions django_gears/templatetags/gears.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,47 @@
from __future__ import absolute_import
from django.template import Node, Library, TemplateSyntaxError
from gears.assets import build_asset
from ..settings import environment, GEARS_URL, GEARS_DEBUG
from ..settings import environment, GEARS_URL, GEARS_DEBUG, GEARS_CACHE_BUST


register = Library()


class AssetTagNode(Node):

def __init__(self, logical_path, debug):
def __init__(self, logical_path, debug, cache_bust):
self.logical_path = logical_path
self.debug = debug
self.cache_bust = cache_bust

@classmethod
def handle_token(cls, parser, token):
bits = token.split_contents()
if len(bits) not in (2, 3):
if len(bits) not in (2, 3, 4):
msg = '%r tag takes one argument: the logical path to the public asset'
raise TemplateSyntaxError(msg % bits[0])
debug = (len(bits) == 3)
debug = (len(bits) == 3 or len(bits) == 4)
if debug and bits[2] != 'debug':
msg = "Second (optional) argument to %r tag must be 'debug'"
raise TemplateSyntaxError(msg % bits[0])
cache_bust = (len(bits) == 4)
if cache_bust and bits[3] != 'cache_bust':
msg = "Third (optional) argument to %r tag must be 'cache_bust'"
raise TemplateSyntaxError(msg % bits[0])
logical_path = parser.compile_filter(bits[1])
return cls(logical_path, debug)
return cls(logical_path, debug, cache_bust)


def render(self, context):
logical_path = self.logical_path.resolve(context)
if self.debug or GEARS_DEBUG:
asset = build_asset(environment, logical_path)
paths = (('%s?body=1&v=%s' % (r.attributes.logical_path, r.mtime))\
for r in asset.requirements)
if self.cache_bust or GEARS_CACHE_BUST:
paths = (('%s?body=1&v=%s' % (r.attributes.logical_path, r.mtime))\
for r in asset.requirements)
else:
paths = (('%s?body=1' % (r.attributes.logical_path,))\
for r in asset.requirements)
else:
if logical_path in environment.manifest.files:
logical_path = environment.manifest.files[logical_path]
Expand Down
14 changes: 12 additions & 2 deletions tests/test_templatetags/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,23 @@ def test_outputs_public_asset_in_normal_mode(self):
self.render(u'{% css_asset_tag "css/script.css" %}'),
u'<link rel="stylesheet" href="/static/css/script.css">')

def test_outputs_all_requirements_in_debug_mode(self):
def test_outputs_all_requirements_in_debug_mode_with_cache_busting(self):

with patch.object(Asset, 'mtime') as mtime:
mtime.__get__ = Mock(return_value = 123)

self.assertEqual(
self.render(u'{% css_asset_tag "css/style.css" debug %}'),
self.render(u'{% css_asset_tag "css/style.css" debug cache_bust %}'),
(u'<link rel="stylesheet" href="/static/css/reset.css?body=1&v=123">\n'
u'<link rel="stylesheet" href="/static/css/base.css?body=1&v=123">\n'
u'<link rel="stylesheet" href="/static/css/style.css?body=1&v=123">'))
def test_outputs_all_requirements_in_debug_mode_without_cache_busting(self):

with patch.object(Asset, 'mtime') as mtime:
mtime.__get__ = Mock(return_value = 123)

self.assertEqual(
self.render(u'{% css_asset_tag "css/style.css" debug %}'),
(u'<link rel="stylesheet" href="/static/css/reset.css?body=1">\n'
u'<link rel="stylesheet" href="/static/css/base.css?body=1">\n'
u'<link rel="stylesheet" href="/static/css/style.css?body=1">'))