Skip to content

Commit

Permalink
make compressed media play well with https
Browse files Browse the repository at this point in the history
  • Loading branch information
dziegler committed May 27, 2010
1 parent e4f6f11 commit 75e33eb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 29 deletions.
25 changes: 10 additions & 15 deletions compressor/__init__.py
Expand Up @@ -10,7 +10,6 @@
from sha import new as sha1
from django import template
from django.conf import settings as django_settings
from django.template.loader import render_to_string
from django.core.files.base import ContentFile
from django.core.files.storage import default_storage
from django.utils.encoding import smart_str
Expand Down Expand Up @@ -69,24 +68,26 @@ def split_contents(self):
raise NotImplementedError('split_contents must be defined in a subclass')

def get_filename(self, url):
url = url.replace("https://","http://")
if not url.startswith(settings.MEDIA_URL):
raise UncompressableFileError('"%s" is not in COMPRESS_URL ("%s") and can not be compressed' % (url, settings.MEDIA_URL))
basename = url[len(settings.MEDIA_URL):]
filename = os.path.join(settings.MEDIA_ROOT, basename)
return filename

@property
def mtimes(self):
return (os.path.getmtime(h[1]) for h in self.split_contents() if h[0] == 'file')
def cachebits(self):
for hunk in self.split_contents():
if hunk[0] == 'file':
yield hunk[0]
yield os.path.getmtime(hunk[1])

@property
def cachekey(self):
"""
cachekey for this block of css or js.
"""
cachebits = [self.content]
cachebits.extend([str(m) for m in self.mtimes])
cachestr = "".join(cachebits)
cachestr = "".join((str(c) for c in self.cachebits))
return "%s.django_compressor.%s.%s" % (DOMAIN, get_hexdigest(cachestr)[:12], settings.COMPRESS)

@property
Expand Down Expand Up @@ -148,8 +149,6 @@ def new_filepath(self):
return filepath

def save_file(self):
if default_storage.exists(self.new_filepath):
return False
default_storage.save(self.new_filepath, ContentFile(self.combined))
return True

Expand All @@ -173,13 +172,9 @@ def output(self):
"""
if not settings.COMPRESS:
return self.return_compiled_content(self.content)
url = "/".join((settings.MEDIA_URL.rstrip('/'), self.new_filepath))
self.save_file()
context = getattr(self, 'extra_context', {})
context['url'] = url
context['xhtml'] = self.xhtml
return render_to_string(self.template_name, context)

return self.new_filepath


class CssCompressor(Compressor):

Expand Down
6 changes: 1 addition & 5 deletions compressor/templates/compressor/css.html
@@ -1,5 +1 @@
{% if xhtml %}
<link rel="stylesheet" href="{{ url }}" type="text/css" />
{% else %}
<link rel="stylesheet" href="{{ url }}" type="text/css">
{% endif %}
<link rel="stylesheet" href="{{ MEDIA_URL }}{{ filepath }}" type="text/css" {% if xhtml %}/>{% else %}>{% endif %}
2 changes: 1 addition & 1 deletion compressor/templates/compressor/js.html
@@ -1 +1 @@
<script type="text/javascript" src="{{ url }}"></script>
<script type="text/javascript" src="{{ MEDIA_URL }}{{ filepath }}"></script>
16 changes: 8 additions & 8 deletions compressor/templatetags/compress.py
@@ -1,5 +1,6 @@
from django import template
from django.core.cache import cache
from django.template.loader import render_to_string

try:
from django.contrib.sites.models import Site
Expand All @@ -25,22 +26,21 @@ def render(self, context):
compressor = CssCompressor(content, xhtml=self.xhtml)
if self.kind == 'js':
compressor = JsCompressor(content, xhtml=self.xhtml)
in_cache = cache.get(compressor.cachekey)
if in_cache:
return in_cache
else:
filepath = cache.get(compressor.cachekey)
if filepath is None:
# do this to prevent dog piling
in_progress_key = '%s.django_css.in_progress.%s' % (DOMAIN, compressor.cachekey)
added_to_cache = cache.add(in_progress_key, True, 300)
if added_to_cache:
output = compressor.output()
cache.set(compressor.cachekey, output, 2591000) # rebuilds the cache every 30 days if nothing has changed.
filepath = compressor.output()
cache.set(compressor.cachekey, filepath, 2591000) # rebuilds the cache every 30 days if nothing has changed.
cache.set(in_progress_key, False, 300)
else:
while cache.get(in_progress_key):
sleep(0.1)
output = cache.get(compressor.cachekey)
return output
filepath = cache.get(compressor.cachekey)

return render_to_string(compressor.template_name, {'filepath':filepath, 'xhtml':self.xhtml}, context)

@register.tag
def compress(parser, token):
Expand Down

0 comments on commit 75e33eb

Please sign in to comment.