diff --git a/compressor/storage.py b/compressor/storage.py index 6492a3a..a852cec 100644 --- a/compressor/storage.py +++ b/compressor/storage.py @@ -1,3 +1,5 @@ +import gzip + from django.core.files.storage import FileSystemStorage, get_storage_class from django.utils.functional import LazyObject @@ -19,6 +21,21 @@ class CompressorFileStorage(FileSystemStorage): super(CompressorFileStorage, self).__init__(location, base_url, *args, **kwargs) +class GzipCompressorFileStorage(CompressorFileStorage): + """ + The standard compressor file system storage that gzips storage files + additionally to the usual files. + """ + def url(self, name): + return u'%s.gz' % super(GzipCompressorFileStorage, self).url(name) + + def save(self, filename, content): + filename = super(GzipCompressorFileStorage, self).save(filename, content) + out = gzip.open(u'%s.gz' % self.path(filename), 'wb') + out.writelines(open(self.path(filename), 'rb')) + out.close() + + class DefaultStorage(LazyObject): def _setup(self): self._wrapped = get_storage_class(settings.COMPRESS_STORAGE)() diff --git a/compressor/tests/storage.py b/compressor/tests/storage.py deleted file mode 100644 index adc5a71..0000000 --- a/compressor/tests/storage.py +++ /dev/null @@ -1,16 +0,0 @@ -import gzip -from compressor.storage import CompressorFileStorage - - -class TestStorage(CompressorFileStorage): - """ - Test compressor storage that gzips storage files - """ - def url(self, name): - return u'%s.gz' % super(TestStorage, self).url(name) - - def save(self, filename, content): - filename = super(TestStorage, self).save(filename, content) - out = gzip.open(u'%s.gz' % self.path(filename), 'wb') - out.writelines(open(self.path(filename), 'rb')) - out.close() diff --git a/compressor/tests/tests.py b/compressor/tests/tests.py index d5dbc2c..d99da6c 100644 --- a/compressor/tests/tests.py +++ b/compressor/tests/tests.py @@ -337,7 +337,7 @@ class TemplatetagTestCase(TestCase): class StorageTestCase(TestCase): def setUp(self): self._storage = base.default_storage - base.default_storage = get_storage_class('compressor.tests.storage.TestStorage')() + base.default_storage = get_storage_class('compressor.storage.GzipCompressorFileStorage')() settings.COMPRESS_ENABLED = True def tearDown(self): diff --git a/docs/index.txt b/docs/index.txt index 4f5e542..c695037 100644 --- a/docs/index.txt +++ b/docs/index.txt @@ -182,6 +182,13 @@ COMPRESS_STORAGE The dotted path to a Django Storage backend to be used to save the compressed files. +``compressor`` ships with one additional storage backend: + +* ``'compressor.storage.GzipCompressorFileStorage'`` + + A subclass of the default storage backend, which will additionally + create ``*.gz`` files of each of the compressed files. + COMPRESS_PARSER ^^^^^^^^^^^^^^^ @@ -222,7 +229,7 @@ no file changes are detected. This is also used by the ``compress`` management command which pre-compresses the contents of ``{% compress %}`` template tags in the cache. -COMPRESS_MINT_DELAY`` +COMPRESS_MINT_DELAY ^^^^^^^^^^^^^^^^^^^ :Default: ``30`` (seconds)