diff --git a/compress/conf/settings.py b/compress/conf/settings.py index 4c71561..822af09 100644 --- a/compress/conf/settings.py +++ b/compress/conf/settings.py @@ -2,6 +2,9 @@ from django.conf import settings COMPRESS = getattr(settings, 'COMPRESS', not settings.DEBUG) +COMPRESS_SOURCE = getattr(settings, 'COMPRESS_SOURCE', settings.MEDIA_ROOT) +COMPRESS_ROOT = getattr(settings, 'COMPRESS_ROOT', settings.MEDIA_ROOT) +COMPRESS_URL = getattr(settings, 'COMPRESS_URL', settings.MEDIA_URL) COMPRESS_AUTO = getattr(settings, 'COMPRESS_AUTO', True) COMPRESS_VERSION = getattr(settings, 'COMPRESS_VERSION', False) COMPRESS_VERSION_PLACEHOLDER = getattr(settings, 'COMPRESS_VERSION_PLACEHOLDER', '?') diff --git a/compress/templatetags/compressed.py b/compress/templatetags/compressed.py index f9c55f9..dbe0849 100644 --- a/compress/templatetags/compressed.py +++ b/compress/templatetags/compressed.py @@ -5,7 +5,7 @@ from django.conf import settings as django_settings from compress.conf import settings -from compress.utils import media_root, media_url, needs_update, filter_css, filter_js, get_output_filename, get_version, get_version_from_file +from compress.utils import compress_root, compress_url, needs_update, filter_css, filter_js, get_output_filename, get_version, get_version_from_file register = template.Library() @@ -18,7 +18,7 @@ def render_common(template_name, obj, filename, version): if filename.startswith('http://'): context['url'] = filename else: - context['url'] = media_url(filename, prefix) + context['url'] = compress_url(filename, prefix) return template.loader.render_to_string(template_name, context) @@ -51,7 +51,7 @@ def render(self, context): filter_css(css) else: filename_base, filename = os.path.split(css['output_filename']) - path_name = media_root(filename_base) + path_name = compress_root(filename_base) version = get_version_from_file(path_name, filename) return render_css(css, css['output_filename'], version) @@ -92,7 +92,7 @@ def render(self, context): filter_js(js) else: filename_base, filename = os.path.split(js['output_filename']) - path_name = media_root(filename_base) + path_name = compress_root(filename_base) version = get_version_from_file(path_name, filename) return render_js(js, js['output_filename'], version) diff --git a/compress/utils.py b/compress/utils.py index 1f542fd..79040a5 100644 --- a/compress/utils.py +++ b/compress/utils.py @@ -46,7 +46,7 @@ def needs_update(output_file, source_files, verbosity=0): version = get_version(source_files) on = get_output_filename(output_file, version) - compressed_file_full = media_root(on) + compressed_file_full = compress_root(on) if not os.path.exists(compressed_file_full): return True, version @@ -54,16 +54,22 @@ def needs_update(output_file, source_files, verbosity=0): update_needed = getattr(get_class(settings.COMPRESS_VERSIONING)(), 'needs_update')(output_file, source_files, version) return update_needed -def media_root(filename): +def compress_root(filename): """ - Return the full path to ``filename``. ``filename`` is a relative path name in MEDIA_ROOT + Return the full path to ``filename``. ``filename`` is a relative path name in COMPRESS_ROOT """ - return os.path.join(django_settings.MEDIA_ROOT, filename) + return os.path.join(settings.COMPRESS_ROOT, filename) -def media_url(url, prefix=None): +def compress_source(filename): + """ + Return the full path to ``filename``. ``filename`` is a relative path name in COMPRESS_SOURCE + """ + return os.path.join(settings.COMPRESS_SOURCE, filename) + +def compress_url(url, prefix=None): if prefix: return prefix + urlquote(url) - return django_settings.MEDIA_URL + urlquote(url) + return settings.COMPRESS_URL + urlquote(url) def concat(filenames, separator=''): """ @@ -71,20 +77,17 @@ def concat(filenames, separator=''): """ r = '' for filename in filenames: - fd = open(media_root(filename), 'rb') + fd = open(compress_source(filename), 'rb') r += fd.read() r += separator fd.close() return r -def max_mtime(files): - return int(max([os.stat(media_root(f)).st_mtime for f in files])) - def save_file(filename, contents): - dirname = os.path.dirname(media_root(filename)) + dirname = os.path.dirname(compress_root(filename)) if not os.path.exists(dirname): os.makedirs(dirname) - fd = open(media_root(filename), 'wb+') + fd = open(compress_root(filename), 'wb+') fd.write(contents) fd.close() @@ -121,7 +124,7 @@ def filter_common(obj, verbosity, filters, attr, separator, signal): filename = get_output_filename(obj['output_filename'], get_version(obj['source_filenames'])) if settings.COMPRESS_VERSION: - remove_files(os.path.dirname(media_root(filename)), obj['output_filename'], verbosity) + remove_files(os.path.dirname(compress_root(filename)), obj['output_filename'], verbosity) if verbosity >= 1: print "Saving %s" % filename diff --git a/compress/versioning/mtime/__init__.py b/compress/versioning/mtime/__init__.py index 31b9d0b..62a0ad7 100644 --- a/compress/versioning/mtime/__init__.py +++ b/compress/versioning/mtime/__init__.py @@ -1,6 +1,6 @@ import os -from compress.utils import get_output_filename, media_root +from compress.utils import get_output_filename, compress_source, compress_root from compress.versioning.base import VersioningBase class MTimeVersioning(VersioningBase): @@ -8,12 +8,13 @@ class MTimeVersioning(VersioningBase): def get_version(self, source_files): # Return the modification time for the newest source file - return str(max([int(os.stat(media_root(f)).st_mtime) for f in source_files])) + return str(max( + [int(os.stat(compress_source(f)).st_mtime) for f in source_files])) def needs_update(self, output_file, source_files, version): output_file_name = get_output_filename(output_file, version) - compressed_file_full = media_root(output_file_name) + compressed_file_full = compress_root(output_file_name) return (int(os.stat(compressed_file_full).st_mtime) < int(version)), version