Commit f33292d8 by Timothée Peignier

fix jinja tag

parent 3cf8257f
from __future__ import unicode_literals from __future__ import unicode_literals
import inspect from django.utils import six
from django.contrib.staticfiles.storage import staticfiles_storage from django.contrib.staticfiles.storage import staticfiles_storage
from django.conf import settings as django_settings from django.conf import settings
from jinja2 import Environment, FileSystemLoader from jinja2 import Environment, FileSystemLoader
from pipeline.conf import settings as pipeline_settings
from pipeline.packager import Packager, PackageNotFound from pipeline.packager import Packager, PackageNotFound
from pipeline.utils import guess_type from pipeline.utils import guess_type
...@@ -18,45 +16,13 @@ class Jinja2Compressed(object): ...@@ -18,45 +16,13 @@ class Jinja2Compressed(object):
raise PackageNotFound("Package type must be css or js, supplied %s" % package_type) raise PackageNotFound("Package type must be css or js, supplied %s" % package_type)
self.package_type = package_type self.package_type = package_type
self.loader = FileSystemLoader((app_directories.app_template_dirs + self.loader = FileSystemLoader((app_directories.app_template_dirs +
django_settings.TEMPLATE_DIRS)) settings.TEMPLATE_DIRS))
self.get_pipeline_settings()
def get_pipeline_settings(self):
"""
Because extra Jinja2 functions have to be declared
at creation time the new functions have to be declared before
django settings evaluation so when pipeline tries to import django
settings it will get the default globals rather than user defined
settings. This function attempts to fudge back in user defined
settings into pipeline settings as django.conf.settings is lazy
loaded and pipeline settings are not.
No harm intended :)
I guess a better more robust solution would be to make pipeline
settings lazy loaded also.
"""
members = inspect.getmembers(pipeline_settings)
for setting, val in members:
if setting.startswith('PIPELINE'):
if hasattr(django_settings, setting):
val = getattr(django_settings, setting)
else:
if type(getattr(pipeline_settings, setting)) == str:
val = "'%s'" % val
val = val if val else "''"
expr = "pipeline_settings.%s = %s" % (
setting, val)
exec(expr)
pipeline_settings.PIPELINE = getattr(django_settings,
'PIPELINE', not django_settings.DEBUG)
self.settings = pipeline_settings
def get_package(self, name): def get_package(self, name):
"""Get the js or css package.""" """Get the js or css package."""
package = { package = {
'js': self.settings.PIPELINE_JS.get(name, {}), 'js': settings.PIPELINE_JS.get(name, {}),
'css': self.settings.PIPELINE_CSS.get(name, {}), 'css': settings.PIPELINE_CSS.get(name, {}),
}[self.package_type] }[self.package_type]
if package: if package:
...@@ -101,7 +67,7 @@ class Jinja2Compressed(object): ...@@ -101,7 +67,7 @@ class Jinja2Compressed(object):
"""Render the HTML Snippet""" """Render the HTML Snippet"""
self.get_package(name) self.get_package(name)
if self.package: if self.package:
if self.settings.PIPELINE: if settings.PIPELINE:
return self.render(self.package.output_filename) return self.render(self.package.output_filename)
else: else:
paths = self.packager.compile(self.package.paths) paths = self.packager.compile(self.package.paths)
......
...@@ -5,7 +5,7 @@ import base64 ...@@ -5,7 +5,7 @@ import base64
try: try:
from mock import patch from mock import patch
except: except ImportError:
from unittest.mock import patch # noqa from unittest.mock import patch # noqa
from django.test import TestCase from django.test import TestCase
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment