Commit f56c88a8 authored by Carl Meyer's avatar Carl Meyer
Browse files

Fixed #21867 -- Removed AppStaticStorage; app paths are now AppConfig's job.

AppStaticStorage only provided one thing over FileSystemStorage, which was
taking an app name (import path) and translating it into a filesystem
path. This is now something that should be done via app_config.path instead,
leaving AppStaticStorage with no reason for existence. It should be safe to
remove, as it was undocumented internal API.

There was some kind of feature in the AppDirectoriesFinder code related to a
"prefix" attribute on the storage class used by AppDirectoriesFinder. Since
this feature was undocumented, untested, and of unclear purpose, I removed it
as well.
parent 06bd181f
Loading
Loading
Loading
Loading
+13 −16
Original line number Diff line number Diff line
@@ -11,7 +11,6 @@ from django.utils._os import safe_join
from django.utils import six, lru_cache

from django.contrib.staticfiles import utils
from django.contrib.staticfiles.storage import AppStaticStorage


class BaseFinder(object):
@@ -110,24 +109,27 @@ class FileSystemFinder(BaseFinder):
class AppDirectoriesFinder(BaseFinder):
    """
    A static files finder that looks in the directory of each app as
    specified in the source_dir attribute of the given storage class.
    specified in the source_dir attribute.
    """
    storage_class = AppStaticStorage
    storage_class = FileSystemStorage
    source_dir = 'static'

    def __init__(self, app_names=None, *args, **kwargs):
        # The list of apps that are handled
        self.apps = []
        # Mapping of app names to storage instances
        self.storages = OrderedDict()
        if app_names is None:
        app_configs = apps.get_app_configs()
            app_names = [app_config.name for app_config in app_configs]
        for app in app_names:
            app_storage = self.storage_class(app)
        if app_names:
            app_names = set(app_names)
            app_configs = [ac for ac in app_configs if ac.name in app_names]
        for app_config in app_configs:
            app_storage = self.storage_class(
                os.path.join(app_config.path, self.source_dir))
            if os.path.isdir(app_storage.location):
                self.storages[app] = app_storage
                if app not in self.apps:
                    self.apps.append(app)
                self.storages[app_config.name] = app_storage
                if app_config.name not in self.apps:
                    self.apps.append(app_config.name)
        super(AppDirectoriesFinder, self).__init__(*args, **kwargs)

    def list(self, ignore_patterns):
@@ -158,11 +160,6 @@ class AppDirectoriesFinder(BaseFinder):
        """
        storage = self.storages.get(app, None)
        if storage:
            if storage.prefix:
                prefix = '%s%s' % (storage.prefix, os.sep)
                if not path.startswith(prefix):
                    return None
                path = path[len(prefix):]
            # only try to find a file if the source dir actually exists
            if storage.exists(path):
                matched_path = storage.path(path)
+0 −21
Original line number Diff line number Diff line
from __future__ import unicode_literals
from collections import OrderedDict
import hashlib
from importlib import import_module
import os
import posixpath
import re
@@ -16,7 +15,6 @@ from django.core.files.storage import FileSystemStorage, get_storage_class
from django.utils.encoding import force_bytes, force_text
from django.utils.functional import LazyObject
from django.utils.six.moves.urllib.parse import unquote, urlsplit, urlunsplit, urldefrag
from django.utils._os import upath

from django.contrib.staticfiles.utils import check_settings, matches_patterns

@@ -383,25 +381,6 @@ class ManifestStaticFilesStorage(ManifestFilesMixin, StaticFilesStorage):
    pass


class AppStaticStorage(FileSystemStorage):
    """
    A file system storage backend that takes an app module and works
    for the ``static`` directory of it.
    """
    prefix = None
    source_dir = 'static'

    def __init__(self, app, *args, **kwargs):
        """
        Returns a static file storage if available in the given app.
        """
        # app is the actual app module
        mod = import_module(app)
        mod_path = os.path.dirname(upath(mod.__file__))
        location = os.path.join(mod_path, self.source_dir)
        super(AppStaticStorage, self).__init__(location, *args, **kwargs)


class ConfiguredStorage(LazyObject):
    def _setup(self):
        self._wrapped = get_storage_class(settings.STATICFILES_STORAGE)()
+0 −31
Original line number Diff line number Diff line
@@ -824,37 +824,6 @@ class TestTemplateTag(StaticFilesTestCase):
        self.assertStaticRenders("testfile.txt", "/static/testfile.txt")


class TestAppStaticStorage(TestCase):
    def setUp(self):
        # Creates a python module foo_module in a directory with non ascii
        # characters
        self.search_path = 'search_path_\xc3\xbc'
        os.mkdir(self.search_path)
        module_path = os.path.join(self.search_path, 'foo_module')
        os.mkdir(module_path)
        self.init_file = open(os.path.join(module_path, '__init__.py'), 'w')
        sys.path.append(os.path.abspath(self.search_path))

    def tearDown(self):
        self.init_file.close()
        sys.path.remove(os.path.abspath(self.search_path))
        shutil.rmtree(self.search_path)

    def test_app_with_non_ascii_characters_in_path(self):
        """
        Regression test for #18404 - Tests AppStaticStorage with a module that
        has non ascii characters in path and a non utf8 file system encoding
        """
        # set file system encoding to a non unicode encoding
        old_enc_func = sys.getfilesystemencoding
        sys.getfilesystemencoding = lambda: 'ISO-8859-1'
        try:
            st = storage.AppStaticStorage('foo_module')
            st.path('bar')
        finally:
            sys.getfilesystemencoding = old_enc_func


class CustomStaticFilesStorage(storage.StaticFilesStorage):
    """
    Used in TestStaticFilePermissions