Ejemplo n.º 1
0
    def render_compressed(self, context, kind, mode, forced=False):

        # See if it has been rendered offline
        if self.is_offline_compression_enabled(forced) and not forced:
            return self.render_offline(context)

        # 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)

        # Check cache
        cache_key = None
        if settings.COMPRESS_ENABLED and not forced:
            cache_key, cache_content = self.render_cached(
                compressor, kind, mode)
            if cache_content is not None:
                return cache_content

        file_basename = getattr(self, 'name', None) or getattr(
            self, 'basename', None)
        if file_basename is None:
            file_basename = 'output'

        rendered_output = compressor.output(mode,
                                            forced=forced,
                                            basename=file_basename)
        assert isinstance(rendered_output, six.string_types)
        if cache_key:
            cache_set(cache_key, rendered_output)
        return rendered_output
Ejemplo n.º 2
0
    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)}
        context['group'] = getattr(self, 'group', False)
        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)
            return rendered_output.decode('utf-8')
        except Exception:
            if settings.DEBUG or forced:
                raise

        # Or don't do anything in production
        return self.get_original_content(context)
Ejemplo n.º 3
0
    def _compress(self, kind, mode, nodelist_string, caller):
        mode = mode or OUTPUT_FILE
        Compressor = get_class(self.compressors.get(kind),
            exception=ImproperlyConfigured)
        original_content = caller()
        compressor = Compressor(original_content)

        forced = getattr(self.environment, '_django_compressor_offline_forced', False)

        # See if it has been rendered offline
        cached_offline = self.render_offline(kind, mode, compressor, forced, nodelist_string)
        if cached_offline:
            return cached_offline

        # Prepare the actual compressor and check cache
        cache_key, cache_content = self.render_cached(kind, mode, compressor,
            forced)
        if cache_content is not None:
            return cache_content

        # call compressor output method and handle exceptions
        try:
            rendered_output = compressor.output(mode, forced)
            if cache_key:
                cache_set(cache_key, rendered_output)
            return rendered_output
        except Exception, e:
            if settings.DEBUG:
                raise e
def render_compressed(self, context, kind, mode, forced=False):

    # See if it has been rendered offline
    if self.is_offline_compression_enabled(forced) and not forced:
        return self.render_offline(context)

    # 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)

    # Check cache
    cache_key = None
    if settings.COMPRESS_ENABLED and not forced:
        cache_key, cache_content = self.render_cached(compressor, kind, mode)
        if cache_content is not None:
            return cache_content

    rendered_output = compressor.output(mode, forced=forced, context=context)

    assert isinstance(rendered_output, six.string_types)
    if cache_key:
        cache_set(cache_key, rendered_output)
    return rendered_output
Ejemplo n.º 5
0
    def render(self, context, forced=False):
        # 1. Check if in debug mode
        if self.debug_mode(context):
            return self.nodelist.render(context)

        # 2. Try offline cache.
        cached_offline = self.render_offline(forced)
        if cached_offline:
            return cached_offline

        # 3. Prepare the actual compressor and check cache
        compressor = self.compressor_cls(self.nodelist.render(context))
        cache_key, cache_content = self.render_cached(compressor, forced)
        if cache_content is not None:
            return cache_content

        # 4. call compressor output method and handle exceptions
        try:
            rendered_output = compressor.output(self.mode, forced=forced)
            if cache_key:
                cache_set(cache_key, rendered_output)
            return rendered_output
        except Exception, e:
            if settings.DEBUG or forced:
                raise e
Ejemplo n.º 6
0
    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)
            return rendered_output.decode('utf-8')
        except Exception:
            if settings.DEBUG or forced:
                raise

        # Or don't do anything in production
        return self.get_original_content(context)
Ejemplo n.º 7
0
    def render(self, context, forced=False):
        # Check if in debug mode
        if self.debug_mode(context):
            return self.nodelist.render(context)

        # Prepare the compressor
        context.update({'name': self.name})
        compressor = self.compressor_cls(content=self.nodelist.render(context),
                                         context=context)

        # See if it has been rendered offline
        cached_offline = self.render_offline(compressor, forced)
        if cached_offline:
            return cached_offline

        # Check cache
        cache_key, cache_content = self.render_cached(compressor, forced)
        if cache_content is not None:
            return cache_content

        # call compressor output method and handle exceptions
        rendered_output = compressor.output(self.mode, forced=forced)
        if cache_key:
            cache_set(cache_key, rendered_output)
        return rendered_output
Ejemplo n.º 8
0
    def render(self, context, forced=False):
        # 1. Check if in debug mode
        if self.debug_mode(context):
            return self.nodelist.render(context)

        # 2. Try offline cache.
        cached_offline = self.render_offline(forced)
        if cached_offline:
            return cached_offline

        # 3. Prepare the actual compressor and check cache
        compressor = self.compressor_cls(self.nodelist.render(context), block_name=self.name)
        cache_key, cache_content = self.render_cached(compressor, forced)
        if cache_content is not None:
            return cache_content

        # 4. call compressor output method and handle exceptions
        try:
            rendered_output = compressor.output(self.mode, forced=forced)
            if cache_key:
                cache_set(cache_key, rendered_output)
            return rendered_output
        except Exception, e:
            if settings.DEBUG or forced:
                raise e
Ejemplo n.º 9
0
    def _compress(self, kind, mode, nodelist_string, caller):
        mode = mode or OUTPUT_FILE
        Compressor = get_class(self.compressors.get(kind),
                               exception=ImproperlyConfigured)
        original_content = caller()
        compressor = Compressor(original_content)

        forced = getattr(self.environment, '_django_compressor_offline_forced',
                         False)

        # See if it has been rendered offline
        cached_offline = self.render_offline(kind, mode, compressor, forced,
                                             nodelist_string)
        if cached_offline:
            return cached_offline

        # Prepare the actual compressor and check cache
        cache_key, cache_content = self.render_cached(kind, mode, compressor,
                                                      forced)
        if cache_content is not None:
            return cache_content

        # call compressor output method and handle exceptions
        try:
            rendered_output = compressor.output(mode, forced)
            if cache_key:
                cache_set(cache_key, rendered_output)
            return rendered_output
        except Exception, e:
            if settings.DEBUG:
                raise e
Ejemplo n.º 10
0
    def render(self, context, forced=False):
        # Check if in debug mode
        if self.debug_mode(context):
            return self.nodelist.render(context)

        # Prepare the compressor
        context.update({'name': self.name})
        compressor = self.compressor_cls(content=self.nodelist.render(context),
                                         context=context)

        # See if it has been rendered offline
        cached_offline = self.render_offline(compressor, forced)
        if cached_offline:
            return cached_offline

        # Check cache
        cache_key, cache_content = self.render_cached(compressor, forced)
        if cache_content is not None:
            return cache_content

        # call compressor output method and handle exceptions
        rendered_output = compressor.output(self.mode, forced=forced)
        if cache_key:
            cache_set(cache_key, rendered_output)
        return rendered_output
Ejemplo n.º 11
0
def compress(cache_key, content, kind, mode):
    logger = compress.get_logger()

    if cache_get(cache_key) is not None:
        logger.debug('No update required for %s', cache_key)
        return

    compressors = CompressorMixin().compressors
    compressor = get_class(compressors.get(kind),
                           exception=ImproperlyConfigured)
    compressor = compressor(content=content)
    output = compressor.output(mode)
    cache_set(cache_key, output)

    logger.debug("Successfully updated cache key %s", cache_key)
    def compiled_render(self, raw_content):

        if has_compressor:
            digest = hashlib.md5(smart_str(raw_content)).hexdigest()
            cached = cache_get(digest)
            if cached:
                return cached

        parser = HandlebarScriptParser()
        parser.feed(raw_content)

        non_template_content = parser.get_non_template_content()
        raw_templates = parser.get_templates()

        compile_root = tempfile.mkdtemp()

        content_parts = []
        content_parts.append('<script type="text/javascript">')
        content_parts.append('window.addEventListener("load", function() {')

        for template in raw_templates:
            compressed = compress_template(
                            compile_root,
                            template.name,
                            template.raw_content)

            content_parts.append(compressed)

        os.rmdir(compile_root)

        content_parts.append(self.get_templates_loaded_js())
        content_parts.append('});')
        content_parts.append("</script>")
        content_parts.append(non_template_content)

        compiled = "".join(content_parts)

        if has_compressor:
            cache_set(digest, compiled)
        return compiled
Ejemplo n.º 13
0
    def _compress(self, kind, mode, caller):
        mode = mode or OUTPUT_FILE
        Compressor = get_class(COMPRESSORS.get(kind))
        original_content = caller()
        compressor = Compressor(original_content)
        # This extension assumes that we won't force compression
        forced = False

        # Prepare the actual compressor and check cache
        cache_key, cache_content = self.render_cached(kind, mode, compressor,
            forced)
        if cache_content is not None:
            return cache_content

        # call compressor output method and handle exceptions
        try:
            rendered_output = compressor.output(mode, forced)
            if cache_key:
                cache_set(cache_key, rendered_output)
            return rendered_output
        except Exception, e:
            pass
Ejemplo n.º 14
0
    def render(self, context, forced=False):
        # 1. Check if in debug mode
        if self.debug_mode(context):
            return self.nodelist.render(context)

        # 2. Try offline cache.
        cached_offline = self.render_offline(forced)
        if cached_offline:
            return cached_offline

        # 3. Prepare the actual compressor and check cache
        context.update({"name": self.name})
        compressor = self.compressor_cls(content=self.nodelist.render(context), context=context)
        cache_key, cache_content = self.render_cached(compressor, forced)
        if cache_content is not None:
            return cache_content

        # 4. call compressor output method and handle exceptions
        rendered_output = compressor.output(self.mode, forced=forced)
        if cache_key:
            cache_set(cache_key, rendered_output)
        return rendered_output
Ejemplo n.º 15
0
    def _compress(self, kind, mode, caller):
        mode = mode or OUTPUT_FILE
        Compressor = get_class(self.compressors.get(kind),
            exception=ImproperlyConfigured)
        original_content = caller()
        compressor = Compressor(original_content)
        # This extension assumes that we won't force compression
        forced = False

        # Prepare the actual compressor and check cache
        cache_key, cache_content = self.render_cached(kind, mode, compressor,
            forced)
        if cache_content is not None:
            return cache_content

        # call compressor output method and handle exceptions
        try:
            rendered_output = compressor.output(mode, forced)
            if cache_key:
                cache_set(cache_key, rendered_output)
            return rendered_output
        except Exception, e:
            if settings.DEBUG:
                raise e