Login

Make HTML5 appcache manifest slice work with django-compress compressed files

Author:
PetrDlouhy
Posted:
September 11, 2015
Language:
Python
Version:
1.7
Score:
0 (after 0 ratings)

I needed to make appcache for my application which used django-compress for JS and CSS compression. This is, how I solved the problem with putting compressed files into the manifest.

I went for offline compression (with COMPRESS_OFFLINE=True). This snippet shows code of command file (put it in apps/cyklomapa/management/commands/compress_create_manifest.py), which creates compress_cache_manifest.txt file in my templates.

Then I just use {% include "compress_cache_manifest.txt" %} in my appcache template.

 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
# -*- coding: utf-8 -*-

from compressor.management.commands.compress import Command as OldCommand
import os

from compressor.signals import post_compress
from django.dispatch import receiver
from django.core.cache import cache

normpath = lambda *args: os.path.normpath(os.path.abspath(os.path.join(*args)))
file_name = normpath(__file__, "..", "..", "..", "templates", 'compress_cache_manifest.txt')

@receiver(post_compress)
def compress(sender, *args, **kwargs):
    context = kwargs['context']
    mode = kwargs['mode']
    if mode == 'file':
       with open(file_name, "a") as manifest_file:
          manifest_file.write(context['compressed']['url'] + "\n")

class Command(OldCommand):
    def compress(self, *args, **kwargs):
       try:
          os.remove(file_name)
       except OSError:
          pass
       return super(Command, self).compress(*args, **kwargs)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

Please login first before commenting.