Commit 261107ff by Timothée Peignier

add a finder based storage

parent 0b06c867
...@@ -21,5 +21,17 @@ Using with staticfiles ...@@ -21,5 +21,17 @@ Using with staticfiles
Pipeline is providing a Finder for `staticfiles app <https://docs.djangoproject.com/en/dev/howto/static-files/>`_, Pipeline is providing a Finder for `staticfiles app <https://docs.djangoproject.com/en/dev/howto/static-files/>`_,
to use it configure ``STATICFILES_FINDERS`` like so : :: to use it configure ``STATICFILES_FINDERS`` like so : ::
STATICFILES_FINDERS = 'pipeline.finders.PipelineFinder' STATICFILES_FINDERS = (
'pipeline.finders.PipelineFinder',
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder'
)
.. note::
``PipelineFinder`` should be the first finder in ``STATICFILES_FINDERS``.
Pipeline is also providing a storage that play nicely with staticfiles app
particularly for development : ::
PIPELINE_STORAGE = 'pipeline.storage.PipelineFinderStorage'
...@@ -6,7 +6,6 @@ PIPELINE_ROOT = getattr(settings, 'PIPELINE_ROOT', settings.MEDIA_ROOT) ...@@ -6,7 +6,6 @@ PIPELINE_ROOT = getattr(settings, 'PIPELINE_ROOT', settings.MEDIA_ROOT)
PIPELINE_URL = getattr(settings, 'PIPELINE_URL', settings.MEDIA_URL) PIPELINE_URL = getattr(settings, 'PIPELINE_URL', settings.MEDIA_URL)
PIPELINE = getattr(settings, 'PIPELINE', not settings.DEBUG) PIPELINE = getattr(settings, 'PIPELINE', not settings.DEBUG)
PIPELINE_SOURCE = getattr(settings, 'PIPELINE_SOURCE', settings.STATIC_ROOT)
PIPELINE_ROOT = getattr(settings, 'PIPELINE_ROOT', settings.STATIC_ROOT) PIPELINE_ROOT = getattr(settings, 'PIPELINE_ROOT', settings.STATIC_ROOT)
PIPELINE_URL = getattr(settings, 'PIPELINE_URL', settings.STATIC_URL) PIPELINE_URL = getattr(settings, 'PIPELINE_URL', settings.STATIC_URL)
PIPELINE_AUTO = getattr(settings, 'PIPELINE_AUTO', True) PIPELINE_AUTO = getattr(settings, 'PIPELINE_AUTO', True)
......
import errno
import os import os
from datetime import datetime from datetime import datetime
from django.contrib.staticfiles import finders
from django.core.exceptions import ImproperlyConfigured
from django.core.files.storage import FileSystemStorage, get_storage_class from django.core.files.storage import FileSystemStorage, get_storage_class
from django.utils.functional import LazyObject from django.utils.functional import LazyObject
...@@ -25,6 +28,46 @@ class PipelineStorage(FileSystemStorage): ...@@ -25,6 +28,46 @@ class PipelineStorage(FileSystemStorage):
def modified_time(self, name): def modified_time(self, name):
return datetime.fromtimestamp(os.path.getmtime(self.path(name))) return datetime.fromtimestamp(os.path.getmtime(self.path(name)))
def _open(self, name, mode='rb'):
full_path = self.path(name)
directory = os.path.dirname(full_path)
if not os.path.exists(directory):
try:
os.makedirs(directory)
except OSError, e:
if e.errno != errno.EEXIST:
raise
if not os.path.isdir(directory):
raise IOError("%s exists and is not a directory." % directory)
return super(PipelineStorage, self)._open(name, mode)
class BaseFinderStorage(PipelineStorage):
finders = None
def __init__(self, finders=None, *args, **kwargs):
if finders is not None:
self.finders = finders
if self.finders is None:
raise ImproperlyConfigured("The storage %r doesn't have a finders class assigned." % self.__class__)
super(BaseFinderStorage, self).__init__(*args, **kwargs)
def path(self, name):
path = self.finders.find(name)
if not path:
path = super(BaseFinderStorage, self).path(name)
return path
def exists(self, name):
exists = self.finders.find(name) != None
if not exists:
exists = super(BaseFinderStorage, self).exists(name)
return exists
class PipelineFinderStorage(BaseFinderStorage):
finders = finders
class DefaultStorage(LazyObject): class DefaultStorage(LazyObject):
def _setup(self): def _setup(self):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment