Added staticfiles compatible file finder to be independent from the FileSystemFinder.
This commit is contained in:
@@ -32,8 +32,8 @@ class Compressor(object):
|
|||||||
base_url = settings.COMPRESS_URL
|
base_url = settings.COMPRESS_URL
|
||||||
if not url.startswith(base_url):
|
if not url.startswith(base_url):
|
||||||
raise UncompressableFileError(
|
raise UncompressableFileError(
|
||||||
"'%s' is not in COMPRESS_URL ('%s') and can not be compressed"
|
"'%s' isn't accesible via COMPRESS_URL ('%s') and can't be"
|
||||||
% (url, base_url))
|
" compressed" % (url, base_url))
|
||||||
basename = url.replace(base_url, "", 1)
|
basename = url.replace(base_url, "", 1)
|
||||||
filename = os.path.join(settings.COMPRESS_ROOT, basename)
|
filename = os.path.join(settings.COMPRESS_ROOT, basename)
|
||||||
if not os.path.exists(filename):
|
if not os.path.exists(filename):
|
||||||
|
|||||||
23
compressor/finders.py
Normal file
23
compressor/finders.py
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
from django.core.exceptions import ImproperlyConfigured
|
||||||
|
|
||||||
|
from compressor.conf import settings
|
||||||
|
from compressor.storage import CompressorFileStorage
|
||||||
|
|
||||||
|
if "django.contrib.staticfiles" in settings.INSTALLED_APPS:
|
||||||
|
from django.contrib.staticfiles.finders import BaseStorageFinder
|
||||||
|
elif "staticfiles" in settings.INSTALLED_APPS:
|
||||||
|
from staticfiles import BaseStorageFinder
|
||||||
|
else:
|
||||||
|
raise ImproperlyConfigured("When using the compressor staticfiles finder"
|
||||||
|
"either django.contrib.staticfiles or the "
|
||||||
|
"standalone version django-staticfiles needs "
|
||||||
|
"to be installed.")
|
||||||
|
|
||||||
|
class CompressorFinder(BaseStorageFinder):
|
||||||
|
"""
|
||||||
|
A staticfiles finder that looks in COMPRESS_ROOT
|
||||||
|
for compressed files, to be used during development
|
||||||
|
with staticfiles development file server or during
|
||||||
|
deployment.
|
||||||
|
"""
|
||||||
|
storage = CompressorFileStorage
|
||||||
@@ -63,35 +63,18 @@ class CompressorSettings(AppSettings):
|
|||||||
# In case staticfiles is used, make sure the FileSystemFinder is
|
# In case staticfiles is used, make sure the FileSystemFinder is
|
||||||
# installed, and if it is, check if COMPRESS_ROOT is listed in
|
# installed, and if it is, check if COMPRESS_ROOT is listed in
|
||||||
# STATICFILES_DIRS to allow finding compressed files
|
# STATICFILES_DIRS to allow finding compressed files
|
||||||
if ("staticfiles" in self.INSTALLED_APPS or
|
staticfiles_settings = None
|
||||||
"django.contrib.staticfiles" in self.INSTALLED_APPS):
|
if "staticfiles" in self.INSTALLED_APPS:
|
||||||
try:
|
from staticfiles.conf import settings as staticfiles_settings
|
||||||
from staticfiles.conf import settings as staticfiles_settings
|
elif "django.contrib.staticfiles" in self.INSTALLED_APPS:
|
||||||
finders = staticfiles_settings.STATICFILES_FINDERS
|
staticfiles_settings = settings
|
||||||
standalone = True
|
if staticfiles_settings is not None:
|
||||||
except ImportError:
|
if ("compressor.finders.CompressorFinder" not in
|
||||||
finders = []
|
staticfiles_settings.STATICFILES_FINDERS):
|
||||||
standalone = False
|
|
||||||
if not finders:
|
|
||||||
finders = getattr(settings, 'STATICFILES_FINDERS', [])
|
|
||||||
if ("django.contrib.staticfiles.finders.FileSystemFinder" not in finders and
|
|
||||||
"staticfiles.finders.FileSystemFinder" not in finders):
|
|
||||||
raise ImproperlyConfigured(
|
raise ImproperlyConfigured(
|
||||||
'Please enable the FileSystemFinder finder of the '
|
"When using django_compressor together with staticfiles, "
|
||||||
'staticfiles app to use it with django_compressor.')
|
"please add 'compressor.finders.CompressorFinder' to the "
|
||||||
abs_paths = []
|
"STATICFILES_FINDERS setting.")
|
||||||
output_path = os.path.join(value, self.COMPRESS_OUTPUT_DIR)
|
|
||||||
for path in getattr(settings, 'STATICFILES_DIRS', []):
|
|
||||||
if isinstance(path, tuple) or isinstance(path, list): # stupid Python 2.4
|
|
||||||
path = path[1] # in case the STATICFILES_DIRS setting has a prefix
|
|
||||||
abs_paths.append(os.path.abspath(path))
|
|
||||||
if os.path.abspath(output_path) not in abs_paths:
|
|
||||||
extension = ((self.COMPRESS_OUTPUT_DIR, output_path),)
|
|
||||||
if standalone:
|
|
||||||
from staticfiles.conf import settings as staticfiles_settings
|
|
||||||
staticfiles_settings.STATICFILES_DIRS += extension
|
|
||||||
else:
|
|
||||||
settings.STATICFILES_DIRS += extension
|
|
||||||
return value
|
return value
|
||||||
|
|
||||||
def configure_url(self, value):
|
def configure_url(self, value):
|
||||||
|
|||||||
@@ -3,8 +3,36 @@ Django compressor
|
|||||||
|
|
||||||
Compresses linked and inline JavaScript or CSS into a single cached file.
|
Compresses linked and inline JavaScript or CSS into a single cached file.
|
||||||
|
|
||||||
Syntax
|
Installation
|
||||||
------
|
------------
|
||||||
|
|
||||||
|
* Install django_compressor with your favorite Python package manager::
|
||||||
|
|
||||||
|
pip install django_compressor
|
||||||
|
|
||||||
|
* Add ``'compressor'`` to your ``INSTALLED_APPS`` setting::
|
||||||
|
|
||||||
|
INSTALLED_APPS = (
|
||||||
|
# other apps
|
||||||
|
"compressor",
|
||||||
|
)
|
||||||
|
|
||||||
|
* See the list of settings_ to modify django_compressor's default behaviour.
|
||||||
|
|
||||||
|
* In case you use Django 1.3's staticfiles_ contrib app (or its standalone
|
||||||
|
clone django-staticfiles_) you have to add django_compressor's file finder
|
||||||
|
to the ``STATICFILES_FINDERS`` setting::
|
||||||
|
|
||||||
|
STATICFILES_FINDERS = (
|
||||||
|
# other finders..
|
||||||
|
'compressor.finders.CompressorFinder',
|
||||||
|
)
|
||||||
|
|
||||||
|
.. _staticfiles: http://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/
|
||||||
|
.. _django-staticfiles: http://pypi.python.org/pypi/django-staticfiles
|
||||||
|
|
||||||
|
Usage
|
||||||
|
-----
|
||||||
|
|
||||||
.. code-block:: django
|
.. code-block:: django
|
||||||
|
|
||||||
@@ -14,7 +42,7 @@ Syntax
|
|||||||
{% endcompress %}
|
{% endcompress %}
|
||||||
|
|
||||||
Examples
|
Examples
|
||||||
--------
|
^^^^^^^^
|
||||||
|
|
||||||
.. code-block:: django
|
.. code-block:: django
|
||||||
|
|
||||||
@@ -49,8 +77,9 @@ Which would be rendered something like:
|
|||||||
|
|
||||||
<script type="text/javascript" src="/static/cache/js/3f33b9146e12.js" charset="utf-8"></script>
|
<script type="text/javascript" src="/static/cache/js/3f33b9146e12.js" charset="utf-8"></script>
|
||||||
|
|
||||||
Linked files must be accesible via COMPRESS_URL_. If DEBUG is true off-site
|
Linked files must be accesible via COMPRESS_URL_. If DEBUG is ``True``,
|
||||||
files will throw exceptions. If DEBUG is false they will be silently stripped.
|
off-site files will throw exceptions. If DEBUG is ``False`` they will be
|
||||||
|
silently stripped.
|
||||||
|
|
||||||
If COMPRESS is ``False`` (defaults to the opposite of DEBUG) the ``compress``
|
If COMPRESS is ``False`` (defaults to the opposite of DEBUG) the ``compress``
|
||||||
template tag simply returns exactly what it was given, to ease development.
|
template tag simply returns exactly what it was given, to ease development.
|
||||||
@@ -145,7 +174,8 @@ COMPRESS_ROOT
|
|||||||
:Default: ``STATIC_ROOT`` (``MEDIA_ROOT`` for older Django versions)
|
:Default: ``STATIC_ROOT`` (``MEDIA_ROOT`` for older Django versions)
|
||||||
|
|
||||||
Controls the absolute file path that linked static will be read from and
|
Controls the absolute file path that linked static will be read from and
|
||||||
compressed static will be written to.
|
compressed static will be written to when using the default COMPRESS_STORAGE_
|
||||||
|
``compressor.storage.CompressorFileStorage``.
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user