From b18cfb17374c5f0ba22db7ebd0099a05073d0fc1 Mon Sep 17 00:00:00 2001 From: Aleksey Porfirov Date: Fri, 13 Nov 2015 14:37:22 +0300 Subject: [PATCH] Prevent multiple collect in jinja template tags --- pipeline/collector.py | 9 ++++--- pipeline/templatetags/ext.py | 3 +++ pipeline/templatetags/pipeline.py | 41 +++++++++++++++++++------------ 3 files changed, 33 insertions(+), 20 deletions(-) diff --git a/pipeline/collector.py b/pipeline/collector.py index 687273bc..9acb38d1 100644 --- a/pipeline/collector.py +++ b/pipeline/collector.py @@ -11,7 +11,8 @@ class Collector(object): - request = None + + env = None def __init__(self, storage=None): if storage is None: @@ -26,10 +27,10 @@ def clear(self, path=""): for d in dirs: self.clear(os.path.join(path, d)) - def collect(self, request=None): - if self.request and self.request is request: + def collect(self, env=None): + if self.env and self.env is env: return - self.request = request + self.env = env found_files = OrderedDict() for finder in finders.get_finders(): # Ignore our finder to avoid looping diff --git a/pipeline/templatetags/ext.py b/pipeline/templatetags/ext.py index f1c80367..166e12ff 100644 --- a/pipeline/templatetags/ext.py +++ b/pipeline/templatetags/ext.py @@ -13,6 +13,9 @@ class PipelineExtension(PipelineMixin, Extension): tags = set(['stylesheet', 'javascript']) + def get_collector_env(self): + return self.environment + def parse(self, parser): tag = next(parser.stream) diff --git a/pipeline/templatetags/pipeline.py b/pipeline/templatetags/pipeline.py index 3cd3632e..af81ad1a 100644 --- a/pipeline/templatetags/pipeline.py +++ b/pipeline/templatetags/pipeline.py @@ -20,14 +20,6 @@ class PipelineMixin(object): - request = None - _request_var = None - - @property - def request_var(self): - if not self._request_var: - self._request_var = template.Variable('request') - return self._request_var def package_for(self, package_name, package_type): package = { @@ -45,18 +37,15 @@ def package_for(self, package_name, package_type): return packager.package_for(package_type, package_name) - def render(self, context): - try: - self.request = self.request_var.resolve(context) - except VariableDoesNotExist: - pass + def get_collector_env(self): + return None def render_compressed(self, package, package_type): if settings.PIPELINE_ENABLED: method = getattr(self, "render_{0}".format(package_type)) return method(package, package.output_filename) else: - default_collector.collect(self.request) + default_collector.collect(self.get_collector_env()) packager = Packager() method = getattr(self, "render_individual_{0}".format(package_type)) @@ -65,7 +54,27 @@ def render_compressed(self, package, package_type): return method(package, paths, templates=templates) -class StylesheetNode(PipelineMixin, template.Node): +class DjangoPipelineMixin(PipelineMixin): + request = None + _request_var = None + + def get_collector_env(self): + return self.request + + @property + def request_var(self): + if not self._request_var: + self._request_var = template.Variable('request') + return self._request_var + + def render(self, context): + try: + self.request = self.request_var.resolve(context) + except VariableDoesNotExist: + pass + + +class StylesheetNode(DjangoPipelineMixin, template.Node): def __init__(self, name): self.name = name @@ -94,7 +103,7 @@ def render_individual_css(self, package, paths, **kwargs): return '\n'.join(tags) -class JavascriptNode(PipelineMixin, template.Node): +class JavascriptNode(DjangoPipelineMixin, template.Node): def __init__(self, name): self.name = name