PageRenderTime 153ms CodeModel.GetById 71ms app.highlight 4ms RepoModel.GetById 76ms app.codeStats 1ms

/django/contrib/staticfiles/storage.py

https://code.google.com/p/mango-py/
Python | 56 lines | 32 code | 7 blank | 17 comment | 6 complexity | 9a1f41fd5b0629cfb56365571d2dc186 MD5 | raw file
 1import os
 2from django.conf import settings
 3from django.core.exceptions import ImproperlyConfigured
 4from django.core.files.storage import FileSystemStorage
 5from django.utils.importlib import import_module
 6
 7from django.contrib.staticfiles import utils
 8
 9
10class StaticFilesStorage(FileSystemStorage):
11    """
12    Standard file system storage for static files.
13
14    The defaults for ``location`` and ``base_url`` are
15    ``STATIC_ROOT`` and ``STATIC_URL``.
16    """
17    def __init__(self, location=None, base_url=None, *args, **kwargs):
18        if location is None:
19            location = settings.STATIC_ROOT
20        if base_url is None:
21            base_url = settings.STATIC_URL
22        if not location:
23            raise ImproperlyConfigured("You're using the staticfiles app "
24                "without having set the STATIC_ROOT setting.")
25        # check for None since we might use a root URL (``/``)
26        if base_url is None:
27            raise ImproperlyConfigured("You're using the staticfiles app "
28                "without having set the STATIC_URL setting.")
29        utils.check_settings()
30        super(StaticFilesStorage, self).__init__(location, base_url, *args, **kwargs)
31
32
33class AppStaticStorage(FileSystemStorage):
34    """
35    A file system storage backend that takes an app module and works
36    for the ``static`` directory of it.
37    """
38    prefix = None
39    source_dir = 'static'
40
41    def __init__(self, app, *args, **kwargs):
42        """
43        Returns a static file storage if available in the given app.
44        """
45        # app is the actual app module
46        self.app_module = app
47        # We special case the admin app here since it has its static files
48        # in 'media' for historic reasons.
49        if self.app_module == 'django.contrib.admin':
50            self.prefix = 'admin'
51            self.source_dir = 'media'
52        mod = import_module(self.app_module)
53        mod_path = os.path.dirname(mod.__file__)
54        location = os.path.join(mod_path, self.source_dir)
55        super(AppStaticStorage, self).__init__(location, *args, **kwargs)
56