Skip to content

Commit 74ab941

Browse files
sir-sigurdAndrey Fedoseev
authored andcommitted
Reworked inlinecompile templatetag to fix Django 2.0 compatibility.
1 parent d928325 commit 74ab941

File tree

4 files changed

+55
-69
lines changed

4 files changed

+55
-69
lines changed

static_precompiler/templatetags/base.py

Lines changed: 0 additions & 49 deletions
This file was deleted.

static_precompiler/templatetags/compile_static.py

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
from static_precompiler import settings, utils
88

9-
from . import base
10-
119
register = django.template.Library()
1210

1311

@@ -35,24 +33,50 @@ def compile_tag(source_path, compiler=None):
3533
return compiled
3634

3735

38-
@base.container_tag(register)
39-
def inlinecompile(nodelist, context, compiler):
40-
source = nodelist.render(context)
36+
class InlineCompileNode(django.template.Node):
37+
def __init__(self, nodelist, compiler):
38+
self.nodelist = nodelist
39+
self.compiler = compiler
40+
41+
def render(self, context):
42+
source = self.nodelist.render(context)
43+
44+
if self.compiler[0] == self.compiler[-1] and self.compiler[0] in ('"', "'"):
45+
compiler = self.compiler[1:-1]
46+
else:
47+
compiler = django.template.Variable(self.compiler).resolve(context)
48+
49+
if isinstance(compiler, six.string_types):
50+
compiler = utils.get_compiler_by_name(compiler)
51+
52+
if settings.USE_CACHE:
53+
cache_key = utils.get_cache_key("{0}.{1}".format(
54+
compiler.__class__.__name__,
55+
utils.get_hexdigest(source)
56+
))
57+
cache = utils.get_cache()
58+
cached = cache.get(cache_key, None)
59+
if cached is not None:
60+
return cached
61+
output = compiler.compile_source(source)
62+
cache.set(cache_key, output, settings.CACHE_TIMEOUT)
63+
return output
4164

42-
if isinstance(compiler, six.string_types):
43-
compiler = utils.get_compiler_by_name(compiler)
65+
return compiler.compile_source(source)
4466

45-
if settings.USE_CACHE:
46-
cache_key = utils.get_cache_key("{0}.{1}".format(
47-
compiler.__class__.__name__,
48-
utils.get_hexdigest(source)
49-
))
50-
cache = utils.get_cache()
51-
cached = cache.get(cache_key, None)
52-
if cached is not None:
53-
return cached
54-
output = compiler.compile_source(source)
55-
cache.set(cache_key, output, settings.CACHE_TIMEOUT)
56-
return output
5767

58-
return compiler.compile_source(source)
68+
@register.tag
69+
def inlinecompile(parser, token):
70+
bits = token.split_contents()
71+
tag_name = bits[0]
72+
try:
73+
compiler, = bits[1:]
74+
except ValueError:
75+
raise django.template.TemplateSyntaxError(
76+
'%r tag requires exactly one argument.' % tag_name
77+
)
78+
if compiler.startswith('compiler='):
79+
compiler = compiler[len('compiler='):]
80+
nodelist = parser.parse(('end' + tag_name,))
81+
parser.delete_first_token()
82+
return InlineCompileNode(nodelist, compiler)

static_precompiler/tests/django_settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,5 @@
3131
"static_precompiler",
3232
)
3333
MTIME_DELAY = 2
34+
35+
STATIC_PRECOMPILER_USE_CACHE = False

static_precompiler/tests/test_templatetags.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,12 @@ def test_inlinecompile_tag(monkeypatch):
2828

2929
assert get_compiler_by_name.calls == [pretend.call("sass")]
3030
assert compiler.compile_source.calls == [pretend.call("source")]
31+
32+
33+
def test_inlinecompile_tag_compiler_as_variable(monkeypatch):
34+
compiler = pretend.stub(compile_source=pretend.call_recorder(lambda *args: 'compiled'))
35+
template = django.template.Template(
36+
"{% load compile_static %}{% inlinecompile compiler %}source{% endinlinecompile %}"
37+
)
38+
assert template.render(django.template.Context({'compiler': compiler})) == 'compiled'
39+
assert compiler.compile_source.calls == [pretend.call('source')]

0 commit comments

Comments
 (0)