remove CSS_PATH and JS_PATH settings

This commit is contained in:
Jeremy Carbaugh
2011-01-05 15:01:54 -05:00
parent d896b0a6e3
commit cb8cdcc124
5 changed files with 18 additions and 81 deletions
+3 -22
View File
@@ -30,8 +30,8 @@ Requirements
Upgrading from mediasync 1.x
----------------------------
1. Update your mediasync settings as described in the next section.
1. Run *./manage.py syncmedia --force* to force updates of all files:
#. Update your mediasync settings as described in the next section.
#. Run *./manage.py syncmedia --force* to force updates of all files:
* gzip instead of deflate compression
* sync both compressed and original versions of files
@@ -495,26 +495,6 @@ When served remotely, one HTML tag will be rendered with the name of the joined
<link rel="stylesheet" href="http://bucket.s3.amazonaws.com/styles/joined.css" type="text/css" media="screen, projection" />
Media Path Shortcuts
====================
In some cases, all CSS and JS files will be in their own directory. It can be a
pain to write full paths from *MEDIA_ROOT* when they can be inferred from the
type of media being used. Shortcuts can be used in template tags and the joined
files configuration if the default paths to JS and CSS files are set.
::
'CSS_PATH': 'styles',
'JS_PATH': 'scripts',
When these paths are set, you can leave them off of the media paths in template
tags. Using the above path settings, styles/reset.css and scripts/jquery.js
can be referred to using::
{% css 'reset.css' %}
{% js 'jquery.js' %}
Smart GZIP for S3
=================
@@ -543,6 +523,7 @@ Change Log
2.0 (in progress)
=================
* remove CSS_PATH and JS_PATH settings
* use gzip instead of deflate for compression (better browser support)
* smart gzip client support detection
* add pluggable backends
+2 -7
View File
@@ -74,10 +74,8 @@ def combine_files(joinfile, sourcefiles, client):
joinfile = joinfile.strip('/')
if joinfile.endswith('.css'):
dirname = msettings['CSS_PATH'].strip('/')
separator = '\n'
elif joinfile.endswith('.js'):
dirname = msettings['JS_PATH'].strip('/')
separator = ';\n'
else:
# By-pass this file since we only join CSS and JS.
@@ -86,7 +84,7 @@ def combine_files(joinfile, sourcefiles, client):
buffer = cStringIO.StringIO()
for sourcefile in sourcefiles:
sourcepath = os.path.join(client.media_root, dirname, sourcefile)
sourcepath = os.path.join(client.media_root, sourcefile)
if os.path.isfile(sourcepath):
f = open(sourcepath)
buffer.write(f.read())
@@ -95,7 +93,7 @@ def combine_files(joinfile, sourcefiles, client):
filedata = buffer.getvalue()
buffer.close()
return (filedata, dirname)
return filedata
def sync(client=None, force=False, verbose=True):
""" Let's face it... pushing this stuff to S3 is messy.
@@ -123,13 +121,10 @@ def sync(client=None, force=False, verbose=True):
if filedata is None:
# combine_files() is only interested in CSS/JS files.
continue
filedata, dirname = filedata
content_type = mimetypes.guess_type(joinfile)[0] or 'application/octet-stream'
remote_path = joinfile
if dirname:
remote_path = "%s/%s" % (dirname, remote_path)
if client.process_and_put(filedata, content_type, remote_path, force=force):
if verbose:
-2
View File
@@ -2,12 +2,10 @@ from django.conf import settings
from mediasync import processors
_settings = {
'CSS_PATH': "",
'DOCTYPE': 'html5',
'EMULATE_COMBO': False,
'EXPIRATION_DAYS': 365,
'JOINED': {},
'JS_PATH': "",
'MEDIA_ROOT': settings.MEDIA_ROOT,
'MEDIA_URL': settings.MEDIA_URL,
'PROCESSORS': (processors.css_minifier, processors.js_minifier),
+10 -12
View File
@@ -202,25 +202,24 @@ class CssTagNode(BaseTagNode):
def render(self, context):
media_url = self.get_media_url(context)
css_path = msettings['CSS_PATH']
joined = msettings['JOINED']
if msettings['SERVE_REMOTE'] and self.path in joined:
# Serving from S3/Cloud Files.
return self.linktag(media_url, css_path, self.path, self.media_type, context)
return self.linktag(media_url, self.path, self.media_type, context)
elif not msettings['SERVE_REMOTE'] and msettings['EMULATE_COMBO']:
# Don't split the combo file into its component files. Emulate
# the combo behavior, but generate/serve it locally. Useful for
# testing combo CSS before deploying.
return self.linktag(media_url, css_path, self.path, self.media_type, context)
return self.linktag(media_url, self.path, self.media_type, context)
else:
# If this is a combo file seen in the JOINED key on the
# MEDIASYNC dict, break it apart into its component files and
# write separate <link> tags for each.
filenames = joined.get(self.path, (self.path,))
return ' '.join((self.linktag(media_url, css_path, fn, self.media_type, context) for fn in filenames))
return ' '.join((self.linktag(media_url, fn, self.media_type, context) for fn in filenames))
def linktag(self, url, path, filename, media, context):
def linktag(self, url, filename, media, context):
"""
Renders a <link> tag for the stylesheet(s).
"""
@@ -228,7 +227,7 @@ class CssTagNode(BaseTagNode):
markup = """<link rel="stylesheet" href="%s" type="text/css" media="%s" />"""
else:
markup = """<link rel="stylesheet" href="%s" type="text/css" media="%s">"""
return markup % (self.mkpath(url, path, filename, gzip=self.supports_gzip(context)), media)
return markup % (self.mkpath(url, '', filename, gzip=self.supports_gzip(context)), media)
"""
# JavaScript related tags
@@ -253,25 +252,24 @@ class JsTagNode(BaseTagNode):
"""
def render(self, context):
media_url = self.get_media_url(context)
js_path = msettings['JS_PATH']
joined = msettings['JOINED']
if msettings['SERVE_REMOTE'] and self.path in joined:
# Serving from S3/Cloud Files.
return self.scripttag(media_url, js_path, self.path, context)
return self.scripttag(media_url, self.path, context)
elif not msettings['SERVE_REMOTE'] and msettings['EMULATE_COMBO']:
# Don't split the combo file into its component files. Emulate
# the combo behavior, but generate/serve it locally. Useful for
# testing combo JS before deploying.
return self.scripttag(media_url, js_path, self.path, context)
return self.scripttag(media_url, self.path, context)
else:
# If this is a combo file seen in the JOINED key on the
# MEDIASYNC dict, break it apart into its component files and
# write separate <link> tags for each.
filenames = joined.get(self.path, (self.path,))
return ' '.join((self.scripttag(media_url, js_path, fn, context) for fn in filenames))
return ' '.join((self.scripttag(media_url, fn, context) for fn in filenames))
def scripttag(self, url, path, filename, context):
def scripttag(self, url, filename, context):
"""
Renders a <script> tag for the JS file(s).
"""
@@ -279,4 +277,4 @@ class JsTagNode(BaseTagNode):
markup = """<script src="%s"></script>"""
else:
markup = """<script type="text/javascript" charset="utf-8" src="%s"></script>"""
return markup % self.mkpath(url, path, filename, gzip=self.supports_gzip(context))
return markup % self.mkpath(url, '', filename, gzip=self.supports_gzip(context))
+3 -38
View File
@@ -32,57 +32,22 @@ def combo_serve(request, path, client):
return HttpResponse(combo_data, mimetype=mime_type)
def _form_key_str(path):
"""
Given a URL path, massage it into a key we can perform a lookup on the
MEDIASYNC['JOINED'] dict with.
This mostly involves figuring into account the CSS_PATH and JS_PATH
settings, if they have been set.
"""
if path.endswith('.css'):
media_path_prefix = msettings['CSS_PATH']
elif path.endswith('.js'):
media_path_prefix = msettings['JS_PATH']
else:
# This isn't a CSS/JS file, no combo for you.
return None
if media_path_prefix:
# CS/JSS path prefix has been set. Factor that into the key lookup.
if not media_path_prefix.endswith('/'):
# We need to add this slash so we can lop it off the 'path'
# variable, to match the value in the JOINED dict.
media_path_prefix += '/'
if path.startswith(media_path_prefix):
# Given path starts with the CSS/JS media prefix. Lop this off
# so we can perform a lookup in the JOINED dict.
return path[len(media_path_prefix):]
else:
# Path is in a root dir, send along as-is.
return path
# No CSS/JS path prefix set. Keep it raw.
return path
def _find_combo_match(path):
"""
Calculate the key to check the MEDIASYNC['JOINED'] dict for, perform the
lookup, and return the matching key string if a match is found. If no
match is found, return None instead.
"""
key_str = _form_key_str(path)
if not key_str:
if not path:
# _form_key_str() says this isn't even a CSS/JS file.
return None
if not msettings['JOINED'].has_key(key_str):
if not msettings['JOINED'].has_key(path):
# No combo file match found. Must be an single file.
return None
else:
# Combo match found, return the JOINED key.
return key_str
return path
def static_serve(request, path, client):
"""