/usr/lib/python2.7/dist-packages/compressor/templatetags/compress.py is in python-compressor 1.4-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | from django import template
from django.core.exceptions import ImproperlyConfigured
from django.utils import six
from compressor.cache import (cache_get, cache_set, get_offline_hexdigest,
get_offline_manifest, get_templatetag_cachekey)
from compressor.conf import settings
from compressor.exceptions import OfflineGenerationError
from compressor.utils import get_class
register = template.Library()
OUTPUT_FILE = 'file'
OUTPUT_INLINE = 'inline'
OUTPUT_MODES = (OUTPUT_FILE, OUTPUT_INLINE)
class CompressorMixin(object):
def get_original_content(self, context):
raise NotImplementedError
@property
def compressors(self):
return {
'js': settings.COMPRESS_JS_COMPRESSOR,
'css': settings.COMPRESS_CSS_COMPRESSOR,
}
def compressor_cls(self, kind, *args, **kwargs):
if kind not in self.compressors.keys():
raise template.TemplateSyntaxError(
"The compress tag's argument must be 'js' or 'css'.")
return get_class(self.compressors.get(kind),
exception=ImproperlyConfigured)(*args, **kwargs)
def get_compressor(self, context, kind):
return self.compressor_cls(kind,
content=self.get_original_content(context), context=context)
def debug_mode(self, context):
if settings.COMPRESS_DEBUG_TOGGLE:
# Only check for the debug parameter
# if a RequestContext was used
request = context.get('request', None)
if request is not None:
return settings.COMPRESS_DEBUG_TOGGLE in request.GET
def is_offline_compression_enabled(self, forced):
"""
Check if offline compression is enabled or forced
Defaults to just checking the settings and forced argument,
but can be overridden to completely disable compression for
a subclass, for instance.
"""
return (settings.COMPRESS_ENABLED and
settings.COMPRESS_OFFLINE) or forced
def render_offline(self, context, forced):
"""
If enabled and in offline mode, and not forced check the offline cache
and return the result if given
"""
if self.is_offline_compression_enabled(forced) and not forced:
key = get_offline_hexdigest(self.get_original_content(context))
offline_manifest = get_offline_manifest()
if key in offline_manifest:
return offline_manifest[key]
else:
raise OfflineGenerationError('You have offline compression '
'enabled but key "%s" is missing from offline manifest. '
'You may need to run "python manage.py compress".' % key)
def render_cached(self, compressor, kind, mode, forced=False):
"""
If enabled checks the cache for the given compressor's cache key
and return a tuple of cache key and output
"""
if settings.COMPRESS_ENABLED and not forced:
cache_key = get_templatetag_cachekey(compressor, mode, kind)
cache_content = cache_get(cache_key)
return cache_key, cache_content
return None, None
def render_compressed(self, context, kind, mode, forced=False):
# See if it has been rendered offline
cached_offline = self.render_offline(context, forced=forced)
if cached_offline:
return cached_offline
# Take a shortcut if we really don't have anything to do
if ((not settings.COMPRESS_ENABLED and
not settings.COMPRESS_PRECOMPILERS) and not forced):
return self.get_original_content(context)
context['compressed'] = {'name': getattr(self, 'name', None)}
compressor = self.get_compressor(context, kind)
# Prepare the actual compressor and check cache
cache_key, cache_content = self.render_cached(compressor, kind, mode, forced=forced)
if cache_content is not None:
return cache_content
# call compressor output method and handle exceptions
try:
rendered_output = self.render_output(compressor, mode, forced=forced)
if cache_key:
cache_set(cache_key, rendered_output)
assert isinstance(rendered_output, six.string_types)
return rendered_output
except Exception:
if settings.DEBUG or forced:
raise
# Or don't do anything in production
return self.get_original_content(context)
def render_output(self, compressor, mode, forced=False):
return compressor.output(mode, forced=forced)
class CompressorNode(CompressorMixin, template.Node):
def __init__(self, nodelist, kind=None, mode=OUTPUT_FILE, name=None):
self.nodelist = nodelist
self.kind = kind
self.mode = mode
self.name = name
def get_original_content(self, context):
return self.nodelist.render(context)
def debug_mode(self, context):
if settings.COMPRESS_DEBUG_TOGGLE:
# Only check for the debug parameter
# if a RequestContext was used
request = context.get('request', None)
if request is not None:
return settings.COMPRESS_DEBUG_TOGGLE in request.GET
def render(self, context, forced=False):
# Check if in debug mode
if self.debug_mode(context):
return self.get_original_content(context)
return self.render_compressed(context, self.kind, self.mode, forced=forced)
@register.tag
def compress(parser, token):
"""
Compresses linked and inline javascript or CSS into a single cached file.
Syntax::
{% compress <js/css> %}
<html of inline or linked JS/CSS>
{% endcompress %}
Examples::
{% compress css %}
<link rel="stylesheet" href="/static/css/one.css" type="text/css" charset="utf-8">
<style type="text/css">p { border:5px solid green;}</style>
<link rel="stylesheet" href="/static/css/two.css" type="text/css" charset="utf-8">
{% endcompress %}
Which would be rendered something like::
<link rel="stylesheet" href="/static/CACHE/css/f7c661b7a124.css" type="text/css" media="all" charset="utf-8">
or::
{% compress js %}
<script src="/static/js/one.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">obj.value = "value";</script>
{% endcompress %}
Which would be rendered something like::
<script type="text/javascript" src="/static/CACHE/js/3f33b9146e12.js" charset="utf-8"></script>
Linked files must be on your COMPRESS_URL (which defaults to STATIC_URL).
If DEBUG is true off-site files will throw exceptions. If DEBUG is false
they will be silently stripped.
"""
nodelist = parser.parse(('endcompress',))
parser.delete_first_token()
args = token.split_contents()
if not len(args) in (2, 3, 4):
raise template.TemplateSyntaxError(
"%r tag requires either one, two or three arguments." % args[0])
kind = args[1]
if len(args) >= 3:
mode = args[2]
if mode not in OUTPUT_MODES:
raise template.TemplateSyntaxError(
"%r's second argument must be '%s' or '%s'." %
(args[0], OUTPUT_FILE, OUTPUT_INLINE))
else:
mode = OUTPUT_FILE
if len(args) == 4:
name = args[3]
else:
name = None
return CompressorNode(nodelist, kind, mode, name)
|