Ticket #12323: 12323.4.diff

File 12323.4.diff, 60.7 KB (added by jezdez, 5 years ago)

logging adoption and other smaller things

  • django/conf/global_settings.py

    diff --git a/django/conf/global_settings.py b/django/conf/global_settings.py
    index f75982f..8af2b9d 100644
    a b TEMPLATE_CONTEXT_PROCESSORS = ( 
    193193    'django.contrib.auth.context_processors.auth',
    194194    'django.core.context_processors.debug',
    195195    'django.core.context_processors.i18n',
    196     'django.core.context_processors.media',
     196    'django.contrib.staticfiles.context_processors.media',
    197197#    'django.core.context_processors.request',
    198198    'django.contrib.messages.context_processors.messages',
    199199)
    TEMPLATE_CONTEXT_PROCESSORS = ( 
    201201# Output to use in template system for invalid (e.g. misspelled) variables.
    202202TEMPLATE_STRING_IF_INVALID = ''
    203203
    204 # URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
    205 # trailing slash.
    206 # Examples: "http://foo.com/media/", "/media/".
    207 ADMIN_MEDIA_PREFIX = '/media/'
    208 
    209204# Default e-mail address to use for various automated correspondence from
    210205# the site managers.
    211206DEFAULT_FROM_EMAIL = 'webmaster@localhost'
    TEST_DATABASE_COLLATION = None 
    550545
    551546# The list of directories to search for fixtures
    552547FIXTURE_DIRS = ()
     548
     549###############
     550# STATICFILES #
     551###############
     552
     553# Absolute path to the directory that holds media.
     554# Example: "/home/media/media.lawrence.com/static/"
     555STATICFILES_ROOT = ''
     556
     557# URL that handles the static files served from STATICFILES_ROOT.
     558# Example: "http://media.lawrence.com/static/"
     559STATICFILES_URL = '/static/'
     560
     561# A tuple of two-tuples with a name and the path of additional directories
     562# which hold static files and should be taken into account during resolving
     563STATICFILES_DIRS = ()
     564
     565# The default file storage backend used during the build process
     566STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.StaticFilesStorage'
     567
     568# List of finder classes that know how to find static files in
     569# various locations.
     570STATICFILES_FINDERS = (
     571    'django.contrib.staticfiles.finders.FileSystemFinder',
     572    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
     573    'django.contrib.staticfiles.finders.StorageFinder',
     574)
     575
     576# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
     577# trailing slash.
     578# Examples: "http://foo.com/media/admin/", "/media/admin/".
     579ADMIN_MEDIA_PREFIX = '/static/admin/'
  • new file django/contrib/staticfiles/context_processors.py

    diff --git a/django/contrib/staticfiles/__init__.py b/django/contrib/staticfiles/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/django/contrib/staticfiles/context_processors.py b/django/contrib/staticfiles/context_processors.py
    new file mode 100644
    index 0000000..e162861
    - +  
     1from django.conf import settings
     2
     3def media(request):
     4    return {
     5        'STATICFILES_URL': settings.STATICFILES_URL,
     6        'MEDIA_URL': settings.MEDIA_URL,
     7    }
  • new file django/contrib/staticfiles/finders.py

    diff --git a/django/contrib/staticfiles/finders.py b/django/contrib/staticfiles/finders.py
    new file mode 100644
    index 0000000..8766aca
    - +  
     1import os
     2from django.conf import settings
     3from django.db import models
     4from django.core.exceptions import ImproperlyConfigured
     5from django.utils.importlib import import_module
     6from django.core.files.storage import default_storage
     7from django.utils.functional import memoize
     8
     9from django.contrib.staticfiles import utils
     10
     11_finders = {}
     12
     13
     14class BaseFinder(object):
     15    """
     16    A base file finder to be used for custom staticfiles finder classes.
     17
     18    """
     19    def find(self, path, all=False):
     20        """
     21        Given a relative file path this ought to find an
     22        absolute file path.
     23
     24        If the ``all`` parameter is ``False`` (default) only
     25        the first found file path will be returned; if set
     26        to ``True`` a list of all found files paths is returned.
     27        """
     28        raise NotImplementedError("Finder subclasses need to implement find()")
     29
     30
     31class FileSystemFinder(BaseFinder):
     32    """
     33    A static files finder that uses the ``STATICFILES_DIRS`` setting
     34    to locate files.
     35    """
     36    def find(self, path, all=False):
     37        """
     38        Looks for files in the extra media locations
     39        as defined in ``STATICFILES_DIRS``.
     40        """
     41        matches = []
     42        for root in settings.STATICFILES_DIRS:
     43            if isinstance(root, (list, tuple)):
     44                prefix, root = root
     45            else:
     46                prefix = ''
     47            matched_path = self.find_location(root, path, prefix)
     48            if matched_path:
     49                if not all:
     50                    return matched_path
     51                matches.append(matched_path)
     52        return matches
     53
     54    def find_location(self, root, path, prefix=None):
     55        """
     56        Find a requested static file in a location, returning the found
     57        absolute path (or ``None`` if no match).
     58        """
     59        if prefix:
     60            prefix = '%s/' % prefix
     61            if not path.startswith(prefix):
     62                return None
     63            path = path[len(prefix):]
     64        path = os.path.join(root, path)
     65        if os.path.exists(path):
     66            return path
     67
     68
     69class AppDirectoriesFinder(BaseFinder):
     70    """
     71    A static files finder that looks in the ``media`` directory of each app.
     72    """
     73    def find(self, path, all=False):
     74        """
     75        Looks for files in the app directories.
     76        """
     77        matches = []
     78        for app in models.get_apps():
     79            app_matches = self.find_in_app(app, path, all=all)
     80            if app_matches:
     81                if not all:
     82                    return app_matches
     83                matches.extend(app_matches)
     84        return matches
     85
     86    def find_in_app(self, app, path, all=False):
     87        """
     88        Find a requested static file in an app's media locations.
     89
     90        If ``all`` is ``False`` (default), return the first matching
     91        absolute path (or ``None`` if no match). Otherwise return a list of
     92        found absolute paths.
     93
     94        """
     95        prefix = utils.get_app_prefix(app)
     96        if prefix:
     97            prefix = '%s/' % prefix
     98            if not path.startswith(prefix):
     99                return []
     100            path = path[len(prefix):]
     101        paths = []
     102        storage = utils.app_static_storage(app)
     103        if storage and storage.exists(path):
     104            matched_path = storage.path(path)
     105            if not all:
     106                return matched_path
     107            paths.append(matched_path)
     108        return paths
     109
     110
     111class StorageFinder(BaseFinder):
     112    """
     113    A static files finder that uses the default storage backend.
     114    """
     115    static_storage = default_storage
     116
     117    def __init__(self, storage=None, *args, **kwargs):
     118        if storage is not None:
     119            self.static_storage = storage
     120        super(StorageFinder, self).__init__(*args, **kwargs)
     121
     122    def find(self, path, all=False):
     123        """
     124        Last resort, looks for files in the
     125        static files storage if it's local.
     126        """
     127        try:
     128            self.static_storage.path('')
     129        except NotImplementedError:
     130            pass
     131        else:
     132            if self.static_storage.exists(path):
     133                match = self.static_storage.path(path)
     134                if all:
     135                    match = [match]
     136                return match
     137        return []
     138
     139
     140def find(path, all=False):
     141    """
     142    Find a requested static file, first looking in any defined extra media
     143    locations and next in any (non-excluded) installed apps.
     144   
     145    If no matches are found and the static location is local, look for a match
     146    there too.
     147   
     148    If ``all`` is ``False`` (default), return the first matching
     149    absolute path (or ``None`` if no match). Otherwise return a list of
     150    found absolute paths.
     151   
     152    """
     153    matches = []
     154    for finder_path in settings.STATICFILES_FINDERS:
     155        finder = get_finder(finder_path)
     156        result = finder.find(path, all=all)
     157        if not all and result:
     158            return result
     159        if not isinstance(result, (list, tuple)):
     160            result = [result]
     161        matches.extend(result)
     162
     163    if matches:
     164        return matches
     165
     166    # No match.
     167    return all and [] or None
     168
     169
     170def _get_finder(import_path):
     171    """
     172    Imports the message storage class described by import_path, where
     173    import_path is the full Python path to the class.
     174    """
     175    module, attr = import_path.rsplit('.', 1)
     176    try:
     177        mod = import_module(module)
     178    except ImportError, e:
     179        raise ImproperlyConfigured('Error importing module %s: "%s"' %
     180                                   (module, e))
     181    try:
     182        Finder = getattr(mod, attr)
     183    except AttributeError:
     184        raise ImproperlyConfigured('Module "%s" does not define a "%s" '
     185                                   'class.' % (module, attr))
     186    if not issubclass(Finder, BaseFinder):
     187        raise ImproperlyConfigured('Finder "%s" is not a subclass of "%s"' %
     188                                   (Finder, BaseFinder))
     189    return Finder()
     190get_finder = memoize(_get_finder, _finders, 1)
  • new file django/contrib/staticfiles/handlers.py

    diff --git a/django/contrib/staticfiles/handlers.py b/django/contrib/staticfiles/handlers.py
    new file mode 100644
    index 0000000..9915fe9
    - +  
     1import os
     2import urllib
     3
     4from django.conf import settings
     5from django.core.handlers.wsgi import WSGIHandler, STATUS_CODE_TEXT
     6from django.http import Http404
     7from django.views import static
     8
     9class StaticFilesHandler(WSGIHandler):
     10    """
     11    WSGI middleware that intercepts calls to the static files directory, as
     12    defined by the STATICFILES_URL setting, and serves those files.
     13    """
     14    media_dir = settings.STATICFILES_ROOT
     15    media_url = settings.STATICFILES_URL
     16
     17    def __init__(self, application, media_dir=None):
     18        self.application = application
     19        if media_dir:
     20            self.media_dir = media_dir
     21
     22    def file_path(self, url):
     23        """
     24        Returns the relative path to the media file on disk for the given URL.
     25
     26        The passed URL is assumed to begin with ``media_url``.  If the
     27        resultant file path is outside the media directory, then a ValueError
     28        is raised.
     29        """
     30        # Remove ``media_url``.
     31        relative_url = url[len(self.media_url):]
     32        return urllib.url2pathname(relative_url)
     33
     34    def serve(self, request, path):
     35        from django.contrib.staticfiles import finders
     36        absolute_path = finders.find(path)
     37        if not absolute_path:
     38            raise Http404('%r could not be matched to a static file.' % path)
     39        absolute_path, filename = os.path.split(absolute_path)
     40        return static.serve(request, path=filename, document_root=absolute_path)
     41
     42    def __call__(self, environ, start_response):
     43        # Ignore requests that aren't under the media prefix.
     44        # Also ignore all requests if prefix isn't a relative URL.
     45        if (self.media_url.startswith('http://') or
     46            self.media_url.startswith('https://') or
     47                not environ['PATH_INFO'].startswith(self.media_url)):
     48            return self.application(environ, start_response)
     49        request = self.application.request_class(environ)
     50        try:
     51            response = self.serve(request, self.file_path(environ['PATH_INFO']))
     52        except Http404:
     53            status = '404 NOT FOUND'
     54            start_response(status, {'Content-type': 'text/plain'}.items())
     55            return [str('Page not found: %s' % environ['PATH_INFO'])]
     56        status_text = STATUS_CODE_TEXT[response.status_code]
     57        status = '%s %s' % (response.status_code, status_text)
     58        response_headers = [(str(k), str(v)) for k, v in response.items()]
     59        for c in response.cookies.values():
     60            response_headers.append(('Set-Cookie', str(c.output(header=''))))
     61        start_response(status, response_headers)
     62        return response
     63
  • new file django/contrib/staticfiles/management/commands/__init__.py

    diff --git a/django/contrib/staticfiles/management/__init__.py b/django/contrib/staticfiles/management/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/django/contrib/staticfiles/management/commands/__init__.py b/django/contrib/staticfiles/management/commands/__init__.py
    new file mode 100644
    index 0000000..1bca2d4
    - +  
     1import logging
     2
     3
  • new file django/contrib/staticfiles/management/commands/build_static.py

    diff --git a/django/contrib/staticfiles/management/commands/build_static.py b/django/contrib/staticfiles/management/commands/build_static.py
    new file mode 100644
    index 0000000..6923587
    - +  
     1import os
     2import sys
     3import shutil
     4from optparse import make_option
     5
     6from django.conf import settings
     7from django.core.files.storage import FileSystemStorage, get_storage_class
     8from django.core.management.base import CommandError, OptionalAppCommand, logger
     9
     10from django.contrib.staticfiles import utils
     11
     12
     13class Command(OptionalAppCommand):
     14    """
     15    Command that allows to copy or symlink media files from different
     16    locations to the settings.STATICFILES_ROOT.
     17    """
     18    option_list = OptionalAppCommand.option_list + (
     19        make_option('--noinput', action='store_false', dest='interactive',
     20            default=True, help="Do NOT prompt the user for input of any "
     21                "kind."),
     22        make_option('-i', '--ignore', action='append', default=[],
     23            dest='ignore_patterns', metavar='PATTERN',
     24            help="Ignore files or directories matching this glob-style "
     25                "pattern. Use multiple times to ignore more."),
     26        make_option('-n', '--dry-run', action='store_true', dest='dry_run',
     27            help="Do everything except modify the filesystem."),
     28        make_option('-l', '--link', action='store_true', dest='link',
     29            help="Create a symbolic link to each file instead of copying."),
     30        make_option('--exclude-dirs', action='store_true',
     31            help="Exclude additional static locations specified in the "
     32                "STATICFILES_DIRS setting."),
     33        make_option('--no-default-ignore', action='store_false',
     34            dest='use_default_ignore_patterns', default=True,
     35            help="Don't ignore the common private glob-style patterns 'CVS', "
     36                "'.*' and '*~'."),
     37    )
     38    help = ("Copy static media files from apps and other locations in a "
     39            "single location.")
     40
     41    def handle(self, *app_labels, **options):
     42        ignore_patterns = options['ignore_patterns']
     43        if options['use_default_ignore_patterns']:
     44            ignore_patterns += ['CVS', '.*', '*~']
     45            options['ignore_patterns'] = ignore_patterns
     46        options['skipped_files'] = []
     47        options['copied_files'] = []
     48        options['symlinked_files'] = []
     49        options['destination_storage'] = get_storage_class(settings.STATICFILES_STORAGE)()
     50        try:
     51            destination_paths = utils.get_files(options['destination_storage'], ignore_patterns)
     52        except OSError:
     53            # The destination storage location may not exist yet. It'll get
     54            # created when the first file is copied.
     55            destination_paths = []
     56        options['destination_paths'] = destination_paths
     57        try:
     58            options['destination_storage'].path('')
     59            destination_local = True
     60        except NotImplementedError:
     61            destination_local = False
     62        options['destination_local'] = destination_local
     63        if options.get('link', False):
     64            if sys.platform == 'win32':
     65                message = "Symlinking is not supported by this platform (%s)."
     66                raise CommandError(message % sys.platform)
     67            if not destination_local:
     68                raise CommandError("Can't symlink to a remote destination.")
     69        # Warn before doing anything more.
     70        if options.get('interactive'):
     71            confirm = raw_input("""
     72You have requested to collate static media files and copy them to the
     73destination location as specified in your settings file.
     74
     75This will overwrite existing files. Are you sure you want to do this?
     76
     77Type 'yes' to continue, or 'no' to cancel: """)
     78            if confirm != 'yes':
     79                raise CommandError("Static files build cancelled.")
     80        return super(Command, self).handle(*app_labels, **options)
     81
     82    def pre_handle_apps(self, **options):
     83        """
     84        Copy all files from a directory.
     85
     86        """
     87        if not options.get('exclude_dirs', False):
     88            ignore_patterns = options['ignore_patterns']
     89            for root in settings.STATICFILES_DIRS:
     90                if isinstance(root, (list, tuple)):
     91                    prefix, root = root
     92                else:
     93                    prefix = ''
     94                source_storage = FileSystemStorage(location=root)
     95                for source in utils.get_files(source_storage, ignore_patterns):
     96                    self.copy_file(source, prefix, source_storage, **options)
     97
     98    def post_handle_apps(self, **options):
     99        copied_files = options['copied_files']
     100        symlinked_files = options['symlinked_files']
     101        count = len(copied_files) + len(symlinked_files)
     102        logger.info("%s static file%s built." % (count,
     103                                                 count != 1 and 's' or ''))
     104
     105    def handle_app(self, app, **options):
     106        """
     107        Copy all static media files from an application.
     108       
     109        """
     110        ignore_patterns = options['ignore_patterns']
     111        prefix = utils.get_app_prefix(app)
     112        storage = utils.app_static_storage(app)
     113        if storage:
     114            for path in utils.get_files(storage, ignore_patterns):
     115                self.copy_file(path, prefix, storage, **options)
     116
     117    def excluded_app(self, app, **options):
     118        """
     119        Gather all the static media files for excluded apps.
     120       
     121        This is so a warning can be issued for later copied files which would
     122        normally be copied by excluded apps.
     123       
     124        """
     125        skipped_files = options['skipped_files']
     126        ignore_patterns = options['ignore_patterns']
     127        all_files = utils.get_files_for_app(app, ignore_patterns)
     128        skipped_files.extend(all_files)
     129
     130    def copy_file(self, source, destination_prefix, source_storage, **options):
     131        """
     132        Attempt to copy (or symlink) `source` to `destination`, returning True
     133        if successful.
     134        """
     135        destination_storage = options['destination_storage']
     136        dry_run = options.get('dry_run', False)
     137        if destination_prefix:
     138            destination = '/'.join([destination_prefix, source])
     139        else:
     140            destination = source
     141
     142        if destination in options['copied_files']:
     143            logger.warning("Skipping duplicate file (already copied earlier):"
     144                           "\n  %s" % destination)
     145            return False
     146        if destination in options['symlinked_files']:
     147            logger.warning("Skipping duplicate file (already linked earlier):"
     148                           "\n  %s" % destination)
     149            return False
     150        if destination in options['skipped_files']:
     151            logger.warning("Copying file that would normally be provided by "
     152                           "an excluded application:\n  %s" % destination)
     153        source_path = source_storage.path(source)
     154        if destination in options['destination_paths']:
     155            if dry_run:
     156                logger.info("Pretending to delete:\n  %s" % destination)
     157            else:
     158                logger.debug("Deleting:\n  %s" % destination)
     159                destination_storage.delete(destination)
     160        if options.get('link', False):
     161            destination_path = destination_storage.path(destination)
     162            if dry_run:
     163                logger.info("Pretending to symlink:\n  %s\nto:\n  %s"
     164                            % (source_path, destination_path))
     165            else:
     166                logger.debug("Symlinking:\n  %s\nto:\n  %s"
     167                             % (source_path, destination_path))
     168                try:
     169                    os.makedirs(os.path.dirname(destination_path))
     170                except OSError:
     171                    pass
     172                os.symlink(source_path, destination_path)
     173            options['symlinked_files'].append(destination)
     174        else:
     175            if dry_run:
     176                logger.info("Pretending to copy:\n  %s\nto:\n  %s"
     177                            % (source_path, destination))
     178            else:
     179                if options['destination_local']:
     180                    destination_path = destination_storage.path(destination)
     181                    try:
     182                        os.makedirs(os.path.dirname(destination_path))
     183                    except OSError:
     184                        pass
     185                    shutil.copy2(source_path, destination_path)
     186                    logger.debug("Copying:\n  %s\nto:\n  %s"
     187                                 % (source_path, destination_path))
     188                else:
     189                    source_file = source_storage.open(source)
     190                    destination_storage.write(destination, source_file)
     191                    logger.debug("Copying:\n  %s\nto:\n  %s"
     192                                 % (source_path, destination))
     193            options['copied_files'].append(destination)
     194        return True
  • new file django/contrib/staticfiles/management/commands/find_static.py

    diff --git a/django/contrib/staticfiles/management/commands/find_static.py b/django/contrib/staticfiles/management/commands/find_static.py
    new file mode 100644
    index 0000000..788386a
    - +  
     1import os
     2from optparse import make_option
     3from django.core.management.base import LabelCommand, logger
     4
     5from django.contrib.staticfiles import finders
     6
     7class Command(LabelCommand):
     8    help = "Finds the absolute paths for the given static file(s)."
     9    args = "[static_file ...]"
     10    label = 'static file'
     11    option_list = LabelCommand.option_list + (
     12        make_option('--first', action='store_false', dest='all', default=True,
     13                    help="Only return the first match for each static file."),
     14    )
     15
     16    def handle_label(self, media_path, **options):
     17        all = options['all']
     18        result = finders.find(media_path, all=all)
     19        if not result:
     20            logger.warning("No matching file found for %r." % media_path)
     21            return
     22        output = '\n  '.join((os.path.realpath(path) for path in result))
     23        logger.info("Found %r here:\n  %s" % (media_path, output))
  • new file django/contrib/staticfiles/storage.py

    diff --git a/django/contrib/staticfiles/models.py b/django/contrib/staticfiles/models.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/django/contrib/staticfiles/storage.py b/django/contrib/staticfiles/storage.py
    new file mode 100644
    index 0000000..b8d883c
    - +  
     1import os, posixpath
     2
     3from django.core.exceptions import ImproperlyConfigured
     4from django.conf import settings
     5from django.core.files.storage import FileSystemStorage, get_storage_class
     6from django.utils.functional import LazyObject
     7
     8
     9class StaticFilesStorage(FileSystemStorage):
     10    """
     11    Standard file system storage for site media files.
     12   
     13    The defaults for ``location`` and ``base_url`` are
     14    ``STATICFILES_ROOT`` and ``STATICFILES_URL``.
     15    """
     16    def __init__(self, location=None, base_url=None, *args, **kwargs):
     17        if location is None:
     18            location = settings.STATICFILES_ROOT
     19        if base_url is None:
     20            base_url = settings.STATICFILES_URL
     21        if not location:
     22            raise ImproperlyConfigured("You're using the staticfiles app "
     23                "without having set the STATICFILES_ROOT setting. Set it to "
     24                "the absolute path of the directory that holds static media.")
     25        if not base_url:
     26            raise ImproperlyConfigured("You're using the staticfiles app "
     27                "without having set the STATICFILES_URL setting. Set it to "
     28                "URL that handles the files served from STATICFILES_ROOT.")
     29        super(StaticFilesStorage, self).__init__(location, base_url, *args, **kwargs)
  • new file django/contrib/staticfiles/urls.py

    diff --git a/django/contrib/staticfiles/urls.py b/django/contrib/staticfiles/urls.py
    new file mode 100644
    index 0000000..f8a30ac
    - +  
     1from django.conf.urls.defaults import patterns, url
     2from django.conf import settings
     3
     4urlpatterns = []
     5
     6# only serve non-fqdn URLs
     7if ':' not in settings.STATICFILES_URL:
     8    urlpatterns += patterns('',
     9        url(r'^(?P<path>.*)$', 'django.contrib.staticfiles.views.serve'),
     10    )
  • new file django/contrib/staticfiles/utils.py

    diff --git a/django/contrib/staticfiles/utils.py b/django/contrib/staticfiles/utils.py
    new file mode 100644
    index 0000000..b533747
    - +  
     1import os
     2import fnmatch
     3
     4from django.core.files.storage import FileSystemStorage
     5from django.utils.importlib import import_module
     6
     7def get_files_for_app(app, ignore_patterns=[]):
     8    """
     9    Return a list containing the relative source paths for all files that
     10    should be copied for an app.
     11   
     12    """
     13    prefix = get_app_prefix(app)
     14    files = []
     15    storage = app_static_storage(app)
     16    if storage:
     17        for path in get_files(storage, ignore_patterns):
     18            if prefix:
     19                path = '/'.join([prefix, path])
     20            files.append(path)
     21    return files
     22
     23def app_static_storage(app):
     24    """
     25    Returns a static file storage if available in the given app.
     26   
     27    """
     28    # "app" is actually the models module of the app. Remove the '.models'.
     29    app_module = '.'.join(app.__name__.split('.')[:-1])
     30
     31    # The models module may be a package in which case dirname(app.__file__)
     32    # would be wrong. Import the actual app as opposed to the models module.
     33    app = import_module(app_module)
     34    app_root = os.path.dirname(app.__file__)
     35    location = os.path.join(app_root, 'media')
     36    if not os.path.isdir(location):
     37        return None
     38    return FileSystemStorage(location=location)
     39
     40def get_app_prefix(app):
     41    """
     42    Return the path name that should be prepended to files for this app.
     43   
     44    """
     45    # "app" is actually the models module of the app. Remove the '.models'.
     46    bits = app.__name__.split('.')[:-1]
     47    app_name = bits[-1]
     48    app_module = '.'.join(bits)
     49    if app_module == 'django.contrib.admin':
     50        return app_name
     51    else:
     52        return None
     53
     54def get_files(storage, ignore_patterns=[], location=''):
     55    """
     56    Recursively walk the storage directories gathering a complete list of files
     57    that should be copied, returning this list.
     58   
     59    """
     60    def is_ignored(path):
     61        """
     62        Return True or False depending on whether the ``path`` should be
     63        ignored (if it matches any pattern in ``ignore_patterns``).
     64       
     65        """
     66        for pattern in ignore_patterns:
     67            if fnmatch.fnmatchcase(path, pattern):
     68                return True
     69        return False
     70
     71    directories, files = storage.listdir(location)
     72    static_files = [location and '/'.join([location, fn]) or fn
     73                    for fn in files
     74                    if not is_ignored(fn)]
     75    for dir in directories:
     76        if is_ignored(dir):
     77            continue
     78        if location:
     79            dir = '/'.join([location, dir])
     80        static_files.extend(get_files(storage, ignore_patterns, dir))
     81    return static_files
  • new file django/contrib/staticfiles/views.py

    diff --git a/django/contrib/staticfiles/views.py b/django/contrib/staticfiles/views.py
    new file mode 100644
    index 0000000..ffd5264
    - +  
     1"""
     2Views and functions for serving static files. These are only to be used during
     3development, and SHOULD NOT be used in a production setting.
     4
     5"""
     6import os
     7from django import http
     8from django.views import static
     9
     10from django.contrib.staticfiles import finders
     11
     12
     13def serve(request, path, show_indexes=False):
     14    """
     15    Serve static files from locations inferred from the static files finders.
     16
     17    To use, put a URL pattern such as::
     18
     19        (r'^(?P<path>.*)$', 'django.contrib.staticfiles.views.serve')
     20
     21    in your URLconf. You may also set ``show_indexes`` to ``True`` if you'd
     22    like to serve a basic index of the directory.  This index view will use the
     23    template hardcoded below, but if you'd like to override it, you can create
     24    a template called ``static/directory_index``.
     25    """
     26    absolute_path = finders.find(path)
     27    if not absolute_path:
     28        raise http.Http404('%r could not be matched to a static file.' % path)
     29    absolute_path, filename = os.path.split(absolute_path)
     30    return static.serve(request, path=filename, document_root=absolute_path,
     31                        show_indexes=show_indexes)
  • django/core/context_processors.py

    diff --git a/django/core/context_processors.py b/django/core/context_processors.py
    index 7a59728..f24c6cf 100644
    a b def media(request): 
    7171    Adds media-related context variables to the context.
    7272
    7373    """
    74     return {'MEDIA_URL': settings.MEDIA_URL}
     74    import warnings
     75    warnings.warn(
     76        "The context processor at `django.core.context_processors.media` is " \
     77        "deprecated; use the path `django.contrib.staticfiles.context_processors.media` " \
     78        "instead.",
     79        PendingDeprecationWarning
     80    )
     81    from django.contrib.staticfiles.context_processors import media as media_context_processor
     82    return media_context_processor(request)
    7583
    7684def request(request):
    7785    return {'request': request}
  • django/core/management/base.py

    diff --git a/django/core/management/base.py b/django/core/management/base.py
    index 6b9ce6e..18ed01a 100644
    a b be executed through ``django-admin.py`` or ``manage.py``). 
    55"""
    66
    77import os
     8import logging
    89import sys
    910from optparse import make_option, OptionParser
    1011
    import django 
    1213from django.core.exceptions import ImproperlyConfigured
    1314from django.core.management.color import color_style
    1415from django.utils.encoding import smart_str
     16from django.utils.log import getLogger
     17
     18logger = getLogger('django.core.management')
    1519
    1620class CommandError(Exception):
    1721    """
    class BaseCommand(object): 
    134138    can_import_settings = True
    135139    requires_model_validation = True
    136140    output_transaction = False # Whether to wrap the output in a "BEGIN; COMMIT;"
     141    log_levels = {
     142        0: logging.ERROR,
     143        1: logging.INFO,
     144        2: logging.DEBUG,
     145    }
    137146
    138147    def __init__(self):
    139148        self.style = color_style()
    class BaseCommand(object): 
    199208        stderr.
    200209
    201210        """
     211        verbosity = options.get('verbosity', 1)
     212        logger.setLevel(self.log_levels[int(verbosity)])
    202213        # Switch to English, because django-admin.py creates database content
    203214        # like permissions, and those shouldn't contain any translations.
    204215        # But only do this if we can assume we have a working settings file,
    class AppCommand(BaseCommand): 
    297308        """
    298309        raise NotImplementedError()
    299310
     311class OptionalAppCommand(BaseCommand):
     312    """
     313    A management command which optionally takes one or more installed
     314    application names as arguments, and does something with each of them.
     315
     316    If no application names are provided, all the applications are used.
     317
     318    The order in which applications are processed is determined by the order
     319    given in INSTALLED_APPS. This differs from Django's AppCommand (it uses the
     320    order the apps are given in the management command).
     321
     322    Rather than implementing ``handle()``, subclasses must implement
     323    ``handle_app()``, which will be called once for each application.
     324
     325    Subclasses can also optionally implement ``excluded_app()`` to run
     326    processes on apps which were excluded.
     327
     328    """
     329    args = '[appname appname ...]'
     330    option_list = BaseCommand.option_list + (
     331        make_option('-e', '--exclude', dest='exclude', action='append',
     332            default=[], help='App to exclude (use multiple --exclude to '
     333            'exclude multiple apps).'),
     334    )
     335
     336    def handle(self, *app_labels, **options):
     337        from django.db import models
     338        # Get all the apps, checking for common errors.
     339        try:
     340            all_apps = models.get_apps()
     341        except (ImproperlyConfigured, ImportError), e:
     342            raise CommandError("%s. Are you sure your INSTALLED_APPS setting "
     343                               "is correct?" % e)
     344        # Build the app_list.
     345        app_list = []
     346        used = 0
     347        for app in all_apps:
     348            app_label = app.__name__.split('.')[-2]
     349            if not app_labels or app_label in app_labels:
     350                used += 1
     351                if app_label not in options['exclude']:
     352                    app_list.append(app)
     353        # Check that all app_labels were used.
     354        if app_labels and used != len(app_labels):
     355            raise CommandError('Could not find the following app(s): %s' %
     356                               ', '.join(app_labels))
     357        # Handle all the apps (either via handle_app or excluded_app),
     358        # collating any output.
     359        output = []
     360        pre_output = self.pre_handle_apps(**options)
     361        if pre_output:
     362            output.append(pre_output)
     363        for app in all_apps:
     364            if app in app_list:
     365                handle_method = self.handle_app
     366            else:
     367                handle_method = self.excluded_app
     368            app_output = handle_method(app, **options)
     369            if app_output:
     370                output.append(app_output)
     371        post_output = self.post_handle_apps(**options)
     372        if post_output:
     373            output.append(post_output)
     374        return '\n'.join(output)
     375
     376    def handle_app(self, app, **options):
     377        """
     378        Perform the command's actions for ``app``, which will be the
     379        Python module corresponding to an application name given on
     380        the command line.
     381
     382        """
     383        raise NotImplementedError()
     384
     385    def excluded_app(self, app, **options):
     386        """
     387        A hook for commands to parse apps which were excluded.
     388
     389        """
     390
     391    def pre_handle_apps(self, **options):
     392        """
     393        A hook for commands to do something before the applications are
     394        processed.
     395
     396        """
     397
     398    def post_handle_apps(self, **options):
     399        """
     400        A hook for commands to do something after all applications have been
     401        processed.
     402
     403        """
     404
     405
    300406class LabelCommand(BaseCommand):
    301407    """
    302408    A management command which takes one or more arbitrary arguments
  • django/core/management/commands/runserver.py

    diff --git a/django/core/management/commands/runserver.py b/django/core/management/commands/runserver.py
    index fc2c694..21391e8 100644
    a b  
    1 from django.core.management.base import BaseCommand, CommandError
    21from optparse import make_option
    32import os
    43import sys
     4import warnings
     5
     6from django.core.management.base import BaseCommand, CommandError
    57
    68class Command(BaseCommand):
    79    option_list = BaseCommand.option_list + (
    class Command(BaseCommand): 
    2022        import django
    2123        from django.core.servers.basehttp import run, AdminMediaHandler, WSGIServerException
    2224        from django.core.handlers.wsgi import WSGIHandler
     25        from django.contrib.staticfiles.handlers import StaticFilesHandler
    2326        if args:
    2427            raise CommandError('Usage is runserver %s' % self.args)
    2528        if not addrport:
    class Command(BaseCommand): 
    5659            translation.activate(settings.LANGUAGE_CODE)
    5760
    5861            try:
    59                 handler = AdminMediaHandler(WSGIHandler(), admin_media_path)
     62                handler = WSGIHandler()
     63                handler = StaticFilesHandler(handler)
     64                # serve admin media like old-school (deprecation pending)
     65                handler = AdminMediaHandler(handler, admin_media_path)
    6066                run(addr, int(port), handler)
    6167            except WSGIServerException, e:
    6268                # Use helpful error messages instead of ugly tracebacks.
  • django/core/servers/basehttp.py

    diff --git a/django/core/servers/basehttp.py b/django/core/servers/basehttp.py
    index dae4297..2da05de 100644
    a b been reviewed for security issues. Don't use it for production use. 
    88"""
    99
    1010from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
    11 import mimetypes
    1211import os
    1312import re
    14 import stat
    1513import sys
    1614import urllib
     15import warnings
    1716
    1817from django.core.management.color import color_style
    1918from django.utils.http import http_date
    2019from django.utils._os import safe_join
     20from django.contrib.staticfiles.handlers import StaticFilesHandler
     21from django.views import static
    2122
    2223__version__ = "0.1"
    2324__all__ = ['WSGIServer','WSGIRequestHandler']
    class WSGIRequestHandler(BaseHTTPRequestHandler): 
    633634
    634635        sys.stderr.write(msg)
    635636
    636 class AdminMediaHandler(object):
     637
     638class AdminMediaHandler(StaticFilesHandler):
    637639    """
    638640    WSGI middleware that intercepts calls to the admin media directory, as
    639641    defined by the ADMIN_MEDIA_PREFIX setting, and serves those images.
    640642    Use this ONLY LOCALLY, for development! This hasn't been tested for
    641643    security and is not super efficient.
    642644    """
    643     def __init__(self, application, media_dir=None):
     645
     646    @property
     647    def media_dir(self):
     648        import django
     649        return os.path.join(django.__path__[0], 'contrib', 'admin', 'media')
     650
     651    @property
     652    def media_url(self):
    644653        from django.conf import settings
    645         self.application = application
    646         if not media_dir:
    647             import django
    648             self.media_dir = \
    649                 os.path.join(django.__path__[0], 'contrib', 'admin', 'media')
    650         else:
    651             self.media_dir = media_dir
    652         self.media_url = settings.ADMIN_MEDIA_PREFIX
     654        return settings.ADMIN_MEDIA_PREFIX
     655
     656    def __init__(self, application, media_dir=None):
     657        warnings.warn('The AdminMediaHandler handler is deprecated; use the '
     658            '`django.contrib.staticfiles.handlers.StaticFilesHandler` instead.',
     659            PendingDeprecationWarning)
     660        super(AdminMediaHandler, self).__init__(application, media_dir)
    653661
    654662    def file_path(self, url):
    655663        """
    656664        Returns the path to the media file on disk for the given URL.
    657665
    658         The passed URL is assumed to begin with ADMIN_MEDIA_PREFIX.  If the
     666        The passed URL is assumed to begin with ``media_url``.  If the
    659667        resultant file path is outside the media directory, then a ValueError
    660668        is raised.
    661669        """
    662         # Remove ADMIN_MEDIA_PREFIX.
     670        # Remove ``media_url``.
    663671        relative_url = url[len(self.media_url):]
    664672        relative_path = urllib.url2pathname(relative_url)
    665673        return safe_join(self.media_dir, relative_path)
    666674
    667     def __call__(self, environ, start_response):
    668         import os.path
    669 
    670         # Ignore requests that aren't under ADMIN_MEDIA_PREFIX. Also ignore
    671         # all requests if ADMIN_MEDIA_PREFIX isn't a relative URL.
    672         if self.media_url.startswith('http://') or self.media_url.startswith('https://') \
    673             or not environ['PATH_INFO'].startswith(self.media_url):
    674             return self.application(environ, start_response)
     675    def serve(self, request, path):
     676        document_root, path = os.path.split(path)
     677        return static.serve(request, path, document_root=document_root)
    675678
    676         # Find the admin file and serve it up, if it exists and is readable.
    677         try:
    678             file_path = self.file_path(environ['PATH_INFO'])
    679         except ValueError: # Resulting file path was not valid.
    680             status = '404 NOT FOUND'
    681             headers = {'Content-type': 'text/plain'}
    682             output = ['Page not found: %s' % environ['PATH_INFO']]
    683             start_response(status, headers.items())
    684             return output
    685         if not os.path.exists(file_path):
    686             status = '404 NOT FOUND'
    687             headers = {'Content-type': 'text/plain'}
    688             output = ['Page not found: %s' % environ['PATH_INFO']]
    689         else:
    690             try:
    691                 fp = open(file_path, 'rb')
    692             except IOError:
    693                 status = '401 UNAUTHORIZED'
    694                 headers = {'Content-type': 'text/plain'}
    695                 output = ['Permission denied: %s' % environ['PATH_INFO']]
    696             else:
    697                 # This is a very simple implementation of conditional GET with
    698                 # the Last-Modified header. It makes media files a bit speedier
    699                 # because the files are only read off disk for the first
    700                 # request (assuming the browser/client supports conditional
    701                 # GET).
    702                 mtime = http_date(os.stat(file_path)[stat.ST_MTIME])
    703                 headers = {'Last-Modified': mtime}
    704                 if environ.get('HTTP_IF_MODIFIED_SINCE', None) == mtime:
    705                     status = '304 NOT MODIFIED'
    706                     output = []
    707                 else:
    708                     status = '200 OK'
    709                     mime_type = mimetypes.guess_type(file_path)[0]
    710                     if mime_type:
    711                         headers['Content-Type'] = mime_type
    712                     output = [fp.read()]
    713                     fp.close()
    714         start_response(status, headers.items())
    715         return output
    716679
    717680def run(addr, port, wsgi_handler):
    718681    server_address = (addr, port)
  • tests/regressiontests/servers/tests.py

    diff --git a/tests/regressiontests/servers/tests.py b/tests/regressiontests/servers/tests.py
    index 4763982..29f169c 100644
    a b from django.test import TestCase 
    99from django.core.handlers.wsgi import WSGIHandler
    1010from django.core.servers.basehttp import AdminMediaHandler
    1111
     12from django.conf import settings
    1213
    1314class AdminMediaHandlerTests(TestCase):
    1415
    class AdminMediaHandlerTests(TestCase): 
    2526        """
    2627        # Cases that should work on all platforms.
    2728        data = (
    28             ('/media/css/base.css', ('css', 'base.css')),
     29            ('%scss/base.css' % settings.ADMIN_MEDIA_PREFIX, ('css', 'base.css')),
    2930        )
    3031        # Cases that should raise an exception.
    3132        bad_data = ()
    class AdminMediaHandlerTests(TestCase): 
    3435        if os.sep == '/':
    3536            data += (
    3637                # URL, tuple of relative path parts.
    37                 ('/media/\\css/base.css', ('\\css', 'base.css')),
     38                ('%s\\css/base.css' % settings.ADMIN_MEDIA_PREFIX, ('\\css', 'base.css')),
    3839            )
    3940            bad_data += (
    40                 '/media//css/base.css',
    41                 '/media////css/base.css',
    42                 '/media/../css/base.css',
     41                '%s/css/base.css' % settings.ADMIN_MEDIA_PREFIX,
     42                '%s///css/base.css' % settings.ADMIN_MEDIA_PREFIX,
     43                '%s../css/base.css' % settings.ADMIN_MEDIA_PREFIX,
    4344            )
    4445        elif os.sep == '\\':
    4546            bad_data += (
    46                 '/media/C:\css/base.css',
    47                 '/media//\\css/base.css',
    48                 '/media/\\css/base.css',
    49                 '/media/\\\\css/base.css'
     47                '%sC:\css/base.css' % settings.ADMIN_MEDIA_PREFIX,
     48                '%s/\\css/base.css' % settings.ADMIN_MEDIA_PREFIX,
     49                '%s\\css/base.css' % settings.ADMIN_MEDIA_PREFIX,
     50                '%s\\\\css/base.css' % settings.ADMIN_MEDIA_PREFIX
    5051            )
    5152        for url, path_tuple in data:
    5253            try:
  • new file tests/regressiontests/staticfiles_tests/apps/no_label/media/file2.txt

    diff --git a/tests/regressiontests/staticfiles_tests/__init__.py b/tests/regressiontests/staticfiles_tests/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/regressiontests/staticfiles_tests/apps/__init__.py b/tests/regressiontests/staticfiles_tests/apps/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/regressiontests/staticfiles_tests/apps/no_label/__init__.py b/tests/regressiontests/staticfiles_tests/apps/no_label/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/regressiontests/staticfiles_tests/apps/no_label/media/file2.txt b/tests/regressiontests/staticfiles_tests/apps/no_label/media/file2.txt
    new file mode 100644
    index 0000000..aa264ca
    - +  
     1file2 in no_label_app
  • new file tests/regressiontests/staticfiles_tests/apps/test/media/test/.hidden

    diff --git a/tests/regressiontests/staticfiles_tests/apps/no_label/models.py b/tests/regressiontests/staticfiles_tests/apps/no_label/models.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/regressiontests/staticfiles_tests/apps/test/__init__.py b/tests/regressiontests/staticfiles_tests/apps/test/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/regressiontests/staticfiles_tests/apps/test/media/test/.hidden b/tests/regressiontests/staticfiles_tests/apps/test/media/test/.hidden
    new file mode 100644
    index 0000000..cef6c23
    - +  
     1This file should be ignored.
  • new file tests/regressiontests/staticfiles_tests/apps/test/media/test/CVS

    diff --git a/tests/regressiontests/staticfiles_tests/apps/test/media/test/CVS b/tests/regressiontests/staticfiles_tests/apps/test/media/test/CVS
    new file mode 100644
    index 0000000..cef6c23
    - +  
     1This file should be ignored.
  • new file tests/regressiontests/staticfiles_tests/apps/test/media/test/backup~

    diff --git a/tests/regressiontests/staticfiles_tests/apps/test/media/test/backup~ b/tests/regressiontests/staticfiles_tests/apps/test/media/test/backup~
    new file mode 100644
    index 0000000..cef6c23
    - +  
     1This file should be ignored.
  • new file tests/regressiontests/staticfiles_tests/apps/test/media/test/file.txt

    diff --git a/tests/regressiontests/staticfiles_tests/apps/test/media/test/file.txt b/tests/regressiontests/staticfiles_tests/apps/test/media/test/file.txt
    new file mode 100644
    index 0000000..169a206
    - +  
     1In app media directory.
  • new file tests/regressiontests/staticfiles_tests/apps/test/media/test/file1.txt

    diff --git a/tests/regressiontests/staticfiles_tests/apps/test/media/test/file1.txt b/tests/regressiontests/staticfiles_tests/apps/test/media/test/file1.txt
    new file mode 100644
    index 0000000..9f9a8d9
    - +  
     1file1 in the app dir
     2 No newline at end of file
  • new file tests/regressiontests/staticfiles_tests/apps/test/media/test/test.ignoreme

    diff --git a/tests/regressiontests/staticfiles_tests/apps/test/media/test/test.ignoreme b/tests/regressiontests/staticfiles_tests/apps/test/media/test/test.ignoreme
    new file mode 100644
    index 0000000..d7df09c
    - +  
     1This file should be ignored.
     2 No newline at end of file
  • new file tests/regressiontests/staticfiles_tests/apps/test/otherdir/odfile.txt

    diff --git a/tests/regressiontests/staticfiles_tests/apps/test/models.py b/tests/regressiontests/staticfiles_tests/apps/test/models.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/regressiontests/staticfiles_tests/apps/test/otherdir/odfile.txt b/tests/regressiontests/staticfiles_tests/apps/test/otherdir/odfile.txt
    new file mode 100644
    index 0000000..c62c93d
    - +  
     1File in otherdir.
  • new file tests/regressiontests/staticfiles_tests/project/documents/subdir/test.txt

    diff --git a/tests/regressiontests/staticfiles_tests/models.py b/tests/regressiontests/staticfiles_tests/models.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/regressiontests/staticfiles_tests/project/documents/subdir/test.txt b/tests/regressiontests/staticfiles_tests/project/documents/subdir/test.txt
    new file mode 100644
    index 0000000..04326a2
    - +  
     1Can we find this file?
  • new file tests/regressiontests/staticfiles_tests/project/documents/test.txt

    diff --git a/tests/regressiontests/staticfiles_tests/project/documents/test.txt b/tests/regressiontests/staticfiles_tests/project/documents/test.txt
    new file mode 100644
    index 0000000..04326a2
    - +  
     1Can we find this file?
  • new file tests/regressiontests/staticfiles_tests/project/documents/test/file.txt

    diff --git a/tests/regressiontests/staticfiles_tests/project/documents/test/file.txt b/tests/regressiontests/staticfiles_tests/project/documents/test/file.txt
    new file mode 100644
    index 0000000..fdeaa23
    - +  
     1In STATICFILES_DIRS directory.
     2
  • new file tests/regressiontests/staticfiles_tests/project/site_media/media/media-file.txt

    diff --git a/tests/regressiontests/staticfiles_tests/project/site_media/media/media-file.txt b/tests/regressiontests/staticfiles_tests/project/site_media/media/media-file.txt
    new file mode 100644
    index 0000000..466922d
    - +  
     1Media file.
  • new file tests/regressiontests/staticfiles_tests/project/site_media/static/test/storage.txt

    diff --git a/tests/regressiontests/staticfiles_tests/project/site_media/static/test/storage.txt b/tests/regressiontests/staticfiles_tests/project/site_media/static/test/storage.txt
    new file mode 100644
    index 0000000..2eda9ce
    - +  
     1Yeah!
     2 No newline at end of file
  • new file tests/regressiontests/staticfiles_tests/tests.py

    diff --git a/tests/regressiontests/staticfiles_tests/tests.py b/tests/regressiontests/staticfiles_tests/tests.py
    new file mode 100644
    index 0000000..0f29d60
    - +  
     1import tempfile
     2import shutil
     3import os
     4import sys
     5from cStringIO import StringIO
     6import posixpath
     7
     8from django.test import TestCase, Client
     9from django.conf import settings
     10from django.core.exceptions import ImproperlyConfigured
     11from django.core.management import call_command
     12from django.db.models.loading import load_app
     13
     14from django.contrib.staticfiles import finders, storage
     15
     16TEST_ROOT = os.path.dirname(__file__)
     17
     18
     19class StaticFilesTestCase(TestCase):
     20    """
     21    Test case with a couple utility assertions.
     22    """
     23    def setUp(self):
     24        self.old_staticfiles_url = settings.STATICFILES_URL
     25        self.old_staticfiles_root = settings.STATICFILES_ROOT
     26        self.old_staticfiles_dirs = settings.STATICFILES_DIRS
     27        self.old_installed_apps = settings.INSTALLED_APPS
     28        self.old_media_root = settings.MEDIA_ROOT
     29        self.old_media_url = settings.MEDIA_URL
     30        self.old_admin_media_prefix = settings.ADMIN_MEDIA_PREFIX
     31
     32        # We have to load these apps to test staticfiles.
     33        load_app('regressiontests.staticfiles_tests.apps.test')
     34        load_app('regressiontests.staticfiles_tests.apps.no_label')
     35        site_media = os.path.join(TEST_ROOT, 'project', 'site_media')
     36        settings.MEDIA_ROOT =  os.path.join(site_media, 'media')
     37        settings.MEDIA_URL = '/media/'
     38        settings.ADMIN_MEDIA_PREFIX = posixpath.join(settings.STATICFILES_URL, 'admin/')
     39        settings.STATICFILES_ROOT = os.path.join(site_media, 'static')
     40        settings.STATICFILES_URL = '/static/'
     41        settings.STATICFILES_DIRS = (
     42            os.path.join(TEST_ROOT, 'project', 'documents'),
     43        )
     44
     45    def tearDown(self):
     46        settings.MEDIA_ROOT = self.old_media_root
     47        settings.MEDIA_URL = self.old_media_url
     48        settings.ADMIN_MEDIA_PREFIX = self.old_admin_media_prefix
     49        settings.STATICFILES_ROOT = self.old_staticfiles_root
     50        settings.STATICFILES_URL = self.old_staticfiles_url
     51        settings.STATICFILES_DIRS = self.old_staticfiles_dirs
     52        settings.INSTALLED_APPS = self.old_installed_apps
     53
     54    def assertFileContains(self, filepath, text):
     55        self.failUnless(text in self._get_file(filepath),
     56                        "'%s' not in '%s'" % (text, filepath))
     57
     58    def assertFileNotFound(self, filepath):
     59        self.assertRaises(IOError, self._get_file, filepath)
     60
     61
     62class TestingStaticFilesStorage(storage.StaticFilesStorage):
     63
     64    def __init__(self, location=None, base_url=None, *args, **kwargs):
     65        location = tempfile.mkdtemp()
     66        settings.STATICFILES_ROOT = location
     67        super(TestingStaticFilesStorage, self).__init__(location, base_url, *args, **kwargs)
     68
     69
     70class BuildStaticTestCase(StaticFilesTestCase):
     71    """
     72    Tests shared by all file-resolving features (build_static,
     73    find_static, and static serve view).
     74
     75    This relies on the asserts defined in UtilityAssertsTestCase, but
     76    is separated because some test cases need those asserts without
     77    all these tests.
     78    """
     79    def setUp(self):
     80        super(BuildStaticTestCase, self).setUp()
     81        self.old_staticfiles_storage = settings.STATICFILES_STORAGE
     82        self.old_root = settings.STATICFILES_ROOT
     83        settings.STATICFILES_STORAGE = 'regressiontests.staticfiles_tests.tests.TestingStaticFilesStorage'
     84        self.run_build_static()
     85
     86    def tearDown(self):
     87        shutil.rmtree(settings.STATICFILES_ROOT)
     88        settings.STATICFILES_ROOT = self.old_root
     89        settings.STATICFILES_STORAGE = self.old_staticfiles_storage
     90        super(BuildStaticTestCase, self).tearDown()
     91
     92    def run_build_static(self, **kwargs):
     93        call_command('build_static', interactive=False, verbosity='0',
     94                     ignore_patterns=['*.ignoreme'], **kwargs)
     95
     96    def _get_file(self, filepath):
     97        assert filepath, 'filepath is empty.'
     98        filepath = os.path.join(settings.STATICFILES_ROOT, filepath)
     99        return open(filepath).read()
     100
     101
     102class TestDefaults(object):
     103    def test_staticfiles_dirs(self):
     104        """
     105        Can find a file in a STATICFILES_DIRS directory.
     106
     107        """
     108        self.assertFileContains('test.txt', 'Can we find')
     109
     110    def test_staticfiles_dirs_subdir(self):
     111        """
     112        Can find a file in a subdirectory of a STATICFILES_DIRS
     113        directory.
     114
     115        """
     116        self.assertFileContains('subdir/test.txt', 'Can we find')
     117
     118    def test_staticfiles_dirs_priority(self):
     119        """
     120        File in STATICFILES_DIRS has priority over file in app.
     121
     122        """
     123        self.assertFileContains('test/file.txt', 'STATICFILES_DIRS')
     124
     125    def test_app_files(self):
     126        """
     127        Can find a file in an app media/ directory.
     128
     129        """
     130        self.assertFileContains('test/file1.txt', 'file1 in the app dir')
     131
     132
     133class TestBuildStatic(BuildStaticTestCase, TestDefaults):
     134    """
     135    Test ``build_static`` management command.
     136    """
     137    def test_ignore(self):
     138        """
     139        Test that -i patterns are ignored.
     140        """
     141        self.assertFileNotFound('test/test.ignoreme')
     142
     143    def test_common_ignore_patterns(self):
     144        """
     145        Common ignore patterns (*~, .*, CVS) are ignored.
     146        """
     147        self.assertFileNotFound('test/.hidden')
     148        self.assertFileNotFound('test/backup~')
     149        self.assertFileNotFound('test/CVS')
     150
     151
     152class TestBuildStaticExcludeNoDefaultIgnore(BuildStaticTestCase):
     153    """
     154    Test ``--exclude-dirs`` and ``--no-default-ignore`` options for
     155    ``build_static`` management command.
     156    """
     157    def run_build_static(self):
     158        super(TestBuildStaticExcludeNoDefaultIgnore, self).run_build_static(
     159            exclude_dirs=True, use_default_ignore_patterns=False)
     160
     161    def test_exclude_dirs(self):
     162        """
     163        With --exclude-dirs, cannot find file in
     164        STATICFILES_DIRS.
     165
     166        """
     167        self.assertFileNotFound('test.txt')
     168
     169    def test_no_common_ignore_patterns(self):
     170        """
     171        With --no-default-ignore, common ignore patterns (*~, .*, CVS)
     172        are not ignored.
     173
     174        """
     175        self.assertFileContains('test/.hidden', 'should be ignored')
     176        self.assertFileContains('test/backup~', 'should be ignored')
     177        self.assertFileContains('test/CVS', 'should be ignored')
     178
     179
     180class TestBuildStaticDryRun(BuildStaticTestCase):
     181    """
     182    Test ``--dry-run`` option for ``build_static`` management command.
     183    """
     184    def run_build_static(self):
     185        super(TestBuildStaticDryRun, self).run_build_static(dry_run=True)
     186
     187    def test_no_files_created(self):
     188        """
     189        With --dry-run, no files created in destination dir.
     190        """
     191        self.assertEquals(os.listdir(settings.STATICFILES_ROOT), [])
     192
     193
     194if sys.platform != 'win32':
     195    class TestBuildStaticLinks(BuildStaticTestCase, TestDefaults):
     196        """
     197        Test ``--link`` option for ``build_static`` management command.
     198
     199        Note that by inheriting ``BaseFileResolutionTests`` we repeat all
     200        the standard file resolving tests here, to make sure using
     201        ``--link`` does not change the file-selection semantics.
     202        """
     203        def run_build_static(self):
     204            super(TestBuildStaticLinks, self).run_build_static(link=True)
     205
     206        def test_links_created(self):
     207            """
     208            With ``--link``, symbolic links are created.
     209
     210            """
     211            self.failUnless(os.path.islink(os.path.join(settings.STATICFILES_ROOT, 'test.txt')))
     212
     213
     214class TestServeStatic(StaticFilesTestCase):
     215    """
     216    Test static asset serving view.
     217    """
     218    urls = "regressiontests.staticfiles_tests.urls"
     219
     220    def _response(self, filepath):
     221        return self.client.get(
     222            posixpath.join(settings.STATICFILES_URL, filepath))
     223
     224    def assertFileContains(self, filepath, text):
     225        self.assertContains(self._response(filepath), text)
     226
     227    def assertFileNotFound(self, filepath):
     228        self.assertEquals(self._response(filepath).status_code, 404)
     229
     230
     231class TestServeAdminMedia(TestServeStatic):
     232    """
     233    Test serving media from django.contrib.admin.
     234    """
     235    def test_serve_admin_media(self):
     236        media_file = posixpath.join(
     237            settings.ADMIN_MEDIA_PREFIX, 'css/base.css')
     238        response = self.client.get(media_file)
     239        self.assertContains(response, 'body')
     240
     241
     242class FinderTestCase(object):
     243    """
     244    Base finder test mixin
     245    """
     246    def test_find_first(self):
     247        src, dst = self.find_first
     248        self.assertEquals(self.finder.find(src), dst)
     249
     250    def test_find_all(self):
     251        src, dst = self.find_all
     252        self.assertEquals(self.finder.find(src, all=True), dst)
     253
     254
     255class TestFileSystemFinder(StaticFilesTestCase, FinderTestCase):
     256    """
     257    Test FileSystemFinder.
     258    """
     259    def setUp(self):
     260        super(TestFileSystemFinder, self).setUp()
     261        self.finder = finders.FileSystemFinder()
     262        test_file_path = os.path.join(TEST_ROOT, 'project/documents/test/file.txt')
     263        self.find_first = ("test/file.txt", test_file_path)
     264        self.find_all = ("test/file.txt", [test_file_path])
     265
     266
     267class TestAppDirectoriesFinder(StaticFilesTestCase, FinderTestCase):
     268    """
     269    Test AppDirectoriesFinder.
     270    """
     271    def setUp(self):
     272        super(TestAppDirectoriesFinder, self).setUp()
     273        self.finder = finders.AppDirectoriesFinder()
     274        test_file_path = os.path.join(TEST_ROOT, 'apps/test/media/test/file1.txt')
     275        self.find_first = ("test/file1.txt", test_file_path)
     276        self.find_all = ("test/file1.txt", [test_file_path])
     277
     278
     279class TestStorageFinder(StaticFilesTestCase, FinderTestCase):
     280    """
     281    Test StorageFinder.
     282    """
     283    def setUp(self):
     284        super(TestStorageFinder, self).setUp()
     285        self.finder = finders.StorageFinder(
     286            storage=storage.StaticFilesStorage(location=settings.MEDIA_ROOT))
     287        test_file_path = os.path.join(settings.MEDIA_ROOT, 'media-file.txt')
     288        self.find_first = ("media-file.txt", test_file_path)
     289        self.find_all = ("media-file.txt", [test_file_path])
     290
     291
     292class TestMiscFinder(TestCase):
     293    """
     294    A few misc finder tests.
     295    """
     296    def test_get_finder(self):
     297        self.assertTrue(isinstance(finders.get_finder(
     298            "django.contrib.staticfiles.finders.FileSystemFinder"),
     299            finders.FileSystemFinder))
     300        self.assertRaises(ImproperlyConfigured,
     301            finders.get_finder, "django.contrib.staticfiles.finders.FooBarFinder")
     302        self.assertRaises(ImproperlyConfigured,
     303            finders.get_finder, "foo.bar.FooBarFinder")
     304
  • new file tests/regressiontests/staticfiles_tests/urls.py

    diff --git a/tests/regressiontests/staticfiles_tests/urls.py b/tests/regressiontests/staticfiles_tests/urls.py
    new file mode 100644
    index 0000000..061ec64
    - +  
     1from django.conf import settings
     2from django.conf.urls.defaults import *
     3
     4urlpatterns = patterns('',
     5    url(r'^static/(?P<path>.*)$', 'django.contrib.staticfiles.views.serve'),
     6)
  • tests/runtests.py

    diff --git a/tests/runtests.py b/tests/runtests.py
    index a5f7479..055c910 100755
    a b ALWAYS_INSTALLED_APPS = [ 
    2727    'django.contrib.comments',
    2828    'django.contrib.admin',
    2929    'django.contrib.admindocs',
     30    'django.contrib.staticfiles',
    3031]
    3132
    3233def get_test_models():
  • tests/urls.py

    diff --git a/tests/urls.py b/tests/urls.py
    index 01d6408..e06dc33 100644
    a b urlpatterns = patterns('', 
    4141
    4242    # special headers views
    4343    (r'special_headers/', include('regressiontests.special_headers.urls')),
     44
     45    # static files handling
     46    (r'^', include('regressiontests.staticfiles_tests.urls')),
    4447)
Back to Top