Work-around to get compressor working for jinja2 templates in django 1.8.
Update JINJA2_GET_ENVIRONMENT() to retrieve correct environment for jinja2 backend.
This commit is contained in:
committed by
Mathieu Pillard
parent
4674c13259
commit
a5b31a1130
@@ -2,6 +2,7 @@ from __future__ import unicode_literals
|
|||||||
import os
|
import os
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.exceptions import ImproperlyConfigured
|
from django.core.exceptions import ImproperlyConfigured
|
||||||
|
from django.template.utils import InvalidTemplateEngineError
|
||||||
|
|
||||||
from appconf import AppConf
|
from appconf import AppConf
|
||||||
|
|
||||||
@@ -69,11 +70,22 @@ class CompressorConf(AppConf):
|
|||||||
OFFLINE_MANIFEST = 'manifest.json'
|
OFFLINE_MANIFEST = 'manifest.json'
|
||||||
# The Context to be used when TemplateFilter is used
|
# The Context to be used when TemplateFilter is used
|
||||||
TEMPLATE_FILTER_CONTEXT = {}
|
TEMPLATE_FILTER_CONTEXT = {}
|
||||||
# Function that returns the Jinja2 environment to use in offline compression.
|
|
||||||
|
# Returns the Jinja2 environment to use in offline compression.
|
||||||
def JINJA2_GET_ENVIRONMENT():
|
def JINJA2_GET_ENVIRONMENT():
|
||||||
|
alias = 'Jinja2'
|
||||||
try:
|
try:
|
||||||
import jinja2
|
from django.template.loader import _engine_list
|
||||||
return jinja2.Environment()
|
engines = _engine_list(alias)
|
||||||
|
if engines:
|
||||||
|
engine = engines[0]
|
||||||
|
return engine.env
|
||||||
|
except InvalidTemplateEngineError:
|
||||||
|
raise InvalidTemplateEngineError(
|
||||||
|
"Could not find config for '{}' "
|
||||||
|
"in settings.TEMPLATES. "
|
||||||
|
"COMPRESS_JINJA2_GET_ENVIRONMENT() may "
|
||||||
|
"need to be defined in settings".format(alias))
|
||||||
except ImportError:
|
except ImportError:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|||||||
@@ -12,8 +12,8 @@ from django.core.management.base import BaseCommand, CommandError
|
|||||||
import django.template
|
import django.template
|
||||||
from django.template import Context
|
from django.template import Context
|
||||||
from django.utils import six
|
from django.utils import six
|
||||||
|
|
||||||
from django.template.loader import get_template # noqa Leave this in to preload template locations
|
from django.template.loader import get_template # noqa Leave this in to preload template locations
|
||||||
|
from django.template.utils import InvalidTemplateEngineError
|
||||||
from django.template import engines
|
from django.template import engines
|
||||||
|
|
||||||
from compressor.cache import get_offline_hexdigest, write_offline_manifest
|
from compressor.cache import get_offline_hexdigest, write_offline_manifest
|
||||||
@@ -101,6 +101,7 @@ class Command(BaseCommand):
|
|||||||
The result is cached with a cache-key derived from the content of the
|
The result is cached with a cache-key derived from the content of the
|
||||||
compress nodes (not the content of the possibly linked files!).
|
compress nodes (not the content of the possibly linked files!).
|
||||||
"""
|
"""
|
||||||
|
engine = options.get("engine", "django")
|
||||||
extensions = options.get('extensions')
|
extensions = options.get('extensions')
|
||||||
extensions = self.handle_extensions(extensions or ['html'])
|
extensions = self.handle_extensions(extensions or ['html'])
|
||||||
verbosity = int(options.get("verbosity", 0))
|
verbosity = int(options.get("verbosity", 0))
|
||||||
@@ -110,6 +111,8 @@ class Command(BaseCommand):
|
|||||||
raise OfflineGenerationError("No template loaders defined. You "
|
raise OfflineGenerationError("No template loaders defined. You "
|
||||||
"must set TEMPLATE_LOADERS in your "
|
"must set TEMPLATE_LOADERS in your "
|
||||||
"settings.")
|
"settings.")
|
||||||
|
templates = set()
|
||||||
|
if engine == 'django':
|
||||||
paths = set()
|
paths = set()
|
||||||
for loader in self.get_loaders():
|
for loader in self.get_loaders():
|
||||||
try:
|
try:
|
||||||
@@ -122,6 +125,7 @@ class Command(BaseCommand):
|
|||||||
except (ImportError, AttributeError, TypeError):
|
except (ImportError, AttributeError, TypeError):
|
||||||
# Yeah, this didn't work out so well, let's move on
|
# Yeah, this didn't work out so well, let's move on
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if not paths:
|
if not paths:
|
||||||
raise OfflineGenerationError("No template paths found. None of "
|
raise OfflineGenerationError("No template paths found. None of "
|
||||||
"the configured template loaders "
|
"the configured template loaders "
|
||||||
@@ -131,13 +135,20 @@ class Command(BaseCommand):
|
|||||||
"loaders.")
|
"loaders.")
|
||||||
if verbosity > 1:
|
if verbosity > 1:
|
||||||
log.write("Considering paths:\n\t" + "\n\t".join(paths) + "\n")
|
log.write("Considering paths:\n\t" + "\n\t".join(paths) + "\n")
|
||||||
templates = set()
|
|
||||||
for path in paths:
|
for path in paths:
|
||||||
for root, dirs, files in os.walk(path,
|
for root, dirs, files in os.walk(path,
|
||||||
followlinks=options.get('followlinks', False)):
|
followlinks=options.get('followlinks', False)):
|
||||||
templates.update(os.path.join(root, name)
|
templates.update(os.path.join(root, name)
|
||||||
for name in files if not name.startswith('.') and
|
for name in files if not name.startswith('.') and
|
||||||
any(fnmatch(name, "*%s" % glob) for glob in extensions))
|
any(fnmatch(name, "*%s" % glob) for glob in extensions))
|
||||||
|
elif engine == 'jinja2' and django.VERSION >= (1, 8):
|
||||||
|
env = settings.COMPRESS_JINJA2_GET_ENVIRONMENT()
|
||||||
|
if env and hasattr(env, 'list_templates'):
|
||||||
|
templates |= set([env.loader.get_source(env, template)[1] for template in
|
||||||
|
env.list_templates(filter_func=lambda _path:
|
||||||
|
os.path.splitext(_path)[-1] in extensions)])
|
||||||
|
|
||||||
if not templates:
|
if not templates:
|
||||||
raise OfflineGenerationError("No templates found. Make sure your "
|
raise OfflineGenerationError("No templates found. Make sure your "
|
||||||
"TEMPLATE_LOADERS and TEMPLATE_DIRS "
|
"TEMPLATE_LOADERS and TEMPLATE_DIRS "
|
||||||
@@ -145,9 +156,7 @@ class Command(BaseCommand):
|
|||||||
if verbosity > 1:
|
if verbosity > 1:
|
||||||
log.write("Found templates:\n\t" + "\n\t".join(templates) + "\n")
|
log.write("Found templates:\n\t" + "\n\t".join(templates) + "\n")
|
||||||
|
|
||||||
engine = options.get("engine", "django")
|
|
||||||
parser = self.__get_parser(engine)
|
parser = self.__get_parser(engine)
|
||||||
|
|
||||||
compressor_nodes = OrderedDict()
|
compressor_nodes = OrderedDict()
|
||||||
for template_name in templates:
|
for template_name in templates:
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user