Make the default compressor storage a lazy object to not load the storage class on module level.

This commit is contained in:
Jannis Leidel
2010-07-02 10:38:47 +02:00
parent e67f326093
commit 10affd1dd6
3 changed files with 19 additions and 14 deletions

View File

@@ -1,18 +1,14 @@
import os import os
from django.conf import settings as django_settings from django.conf import settings as django_settings
from django.template.loader import render_to_string from django.template.loader import render_to_string
from django.core.files.base import ContentFile from django.core.files.base import ContentFile
from django.core.files.storage import get_storage_class
from compressor.conf import settings from compressor.conf import settings
from compressor import filters from compressor import filters
from compressor.exceptions import UncompressableFileError from compressor.exceptions import UncompressableFileError
from compressor.utils import get_hexdigest, get_mtime, get_class from compressor.utils import get_hexdigest, get_mtime, get_class
STORAGE = get_storage_class(settings.STORAGE)()
class Compressor(object): class Compressor(object):
def __init__(self, content, output_prefix="compressed"): def __init__(self, content, output_prefix="compressed"):
@@ -57,7 +53,8 @@ class Compressor(object):
@property @property
def storage(self): def storage(self):
return STORAGE from compressor.storage import default_storage
return default_storage
@property @property
def hunks(self): def hunks(self):

View File

@@ -1,5 +1,6 @@
from django.core.files.storage import FileSystemStorage from django.core.files.storage import FileSystemStorage
from django.core.files.storage import get_storage_class from django.core.files.storage import get_storage_class
from django.utils.functional import LazyObject
from compressor.conf import settings from compressor.conf import settings
@@ -18,3 +19,9 @@ class CompressorFileStorage(FileSystemStorage):
base_url = settings.MEDIA_URL base_url = settings.MEDIA_URL
super(CompressorFileStorage, self).__init__(location, base_url, super(CompressorFileStorage, self).__init__(location, base_url,
*args, **kwargs) *args, **kwargs)
class DefaultStorage(LazyObject):
def _setup(self):
self._wrapped = get_storage_class(settings.STORAGE)()
default_storage = DefaultStorage()

View File

@@ -1,17 +1,18 @@
import os, re import os
import re
import gzip import gzip
from BeautifulSoup import BeautifulSoup
from django.template import Template, Context, TemplateSyntaxError from django.template import Template, Context, TemplateSyntaxError
from django.test import TestCase from django.test import TestCase
from compressor import CssCompressor, JsCompressor from django.core.files.storage import get_storage_class
from django.conf import settings as django_settings
from compressor import CssCompressor, JsCompressor, storage
from compressor.conf import settings from compressor.conf import settings
from compressor.storage import CompressorFileStorage from compressor.storage import CompressorFileStorage
from compressor.utils import get_hexdigest, get_mtime from compressor.utils import get_hexdigest, get_mtime
from django.conf import settings as django_settings
from BeautifulSoup import BeautifulSoup
class CompressorTestCase(TestCase): class CompressorTestCase(TestCase):
@@ -332,12 +333,12 @@ class TestStorage(CompressorFileStorage):
class StorageTestCase(TestCase): class StorageTestCase(TestCase):
def setUp(self): def setUp(self):
self._storage = settings.STORAGE self._storage = storage.default_storage
settings.STORAGE = 'core.tests.TestStorage' storage.default_storage = get_storage_class('core.tests.TestStorage')()
settings.COMPRESS = True settings.COMPRESS = True
def tearDown(self): def tearDown(self):
settings.STORAGE = self._storage storage.default_storage = self._storage
def test_css_tag_with_storage(self): def test_css_tag_with_storage(self):
template = u"""{% load compress %}{% compress css %} template = u"""{% load compress %}{% compress css %}