mirror of
https://github.com/django/django.git
synced 2025-09-18 06:59:12 +00:00
Refs #22712 -- Removed all parameter from django.contrib.staticfiles.finders.find().
Per deprecation timeline.
This commit is contained in:
parent
00a84fc6f3
commit
a146fe2930
@ -1,6 +1,5 @@
|
|||||||
import functools
|
import functools
|
||||||
import os
|
import os
|
||||||
import warnings
|
|
||||||
|
|
||||||
from django.apps import apps
|
from django.apps import apps
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
@ -9,7 +8,6 @@ from django.core.checks import Error, Warning
|
|||||||
from django.core.exceptions import ImproperlyConfigured
|
from django.core.exceptions import ImproperlyConfigured
|
||||||
from django.core.files.storage import FileSystemStorage, Storage, default_storage
|
from django.core.files.storage import FileSystemStorage, Storage, default_storage
|
||||||
from django.utils._os import safe_join
|
from django.utils._os import safe_join
|
||||||
from django.utils.deprecation import RemovedInDjango61Warning
|
|
||||||
from django.utils.functional import LazyObject, empty
|
from django.utils.functional import LazyObject, empty
|
||||||
from django.utils.module_loading import import_string
|
from django.utils.module_loading import import_string
|
||||||
|
|
||||||
@ -17,32 +15,6 @@ from django.utils.module_loading import import_string
|
|||||||
searched_locations = []
|
searched_locations = []
|
||||||
|
|
||||||
|
|
||||||
# RemovedInDjango61Warning: When the deprecation ends, remove completely.
|
|
||||||
def _check_deprecated_find_param(class_name="", find_all=False, stacklevel=3, **kwargs):
|
|
||||||
method_name = "find" if not class_name else f"{class_name}.find"
|
|
||||||
if "all" in kwargs:
|
|
||||||
legacy_all = kwargs.pop("all")
|
|
||||||
msg = (
|
|
||||||
"Passing the `all` argument to find() is deprecated. Use `find_all` "
|
|
||||||
"instead."
|
|
||||||
)
|
|
||||||
warnings.warn(msg, RemovedInDjango61Warning, stacklevel=stacklevel)
|
|
||||||
|
|
||||||
# If both `find_all` and `all` were given, raise TypeError.
|
|
||||||
if find_all is not False:
|
|
||||||
raise TypeError(
|
|
||||||
f"{method_name}() got multiple values for argument 'find_all'"
|
|
||||||
)
|
|
||||||
|
|
||||||
find_all = legacy_all
|
|
||||||
|
|
||||||
if kwargs: # any remaining kwargs must be a TypeError
|
|
||||||
first = list(kwargs.keys()).pop()
|
|
||||||
raise TypeError(f"{method_name}() got an unexpected keyword argument '{first}'")
|
|
||||||
|
|
||||||
return find_all
|
|
||||||
|
|
||||||
|
|
||||||
class BaseFinder:
|
class BaseFinder:
|
||||||
"""
|
"""
|
||||||
A base file finder to be used for custom staticfiles finder classes.
|
A base file finder to be used for custom staticfiles finder classes.
|
||||||
@ -54,25 +26,6 @@ class BaseFinder:
|
|||||||
"configured correctly."
|
"configured correctly."
|
||||||
)
|
)
|
||||||
|
|
||||||
# RemovedInDjango61Warning: When the deprecation ends, remove completely.
|
|
||||||
def _check_deprecated_find_param(self, **kwargs):
|
|
||||||
return _check_deprecated_find_param(
|
|
||||||
class_name=self.__class__.__qualname__, stacklevel=4, **kwargs
|
|
||||||
)
|
|
||||||
|
|
||||||
# RemovedInDjango61Warning: When the deprecation ends, replace with:
|
|
||||||
# def find(self, path, find_all=False):
|
|
||||||
def find(self, path, find_all=False, **kwargs):
|
|
||||||
"""
|
|
||||||
Given a relative file path, find an absolute file path.
|
|
||||||
|
|
||||||
If the ``find_all`` parameter is False (default) return only the first
|
|
||||||
found file path; if True, return a list of all found files paths.
|
|
||||||
"""
|
|
||||||
raise NotImplementedError(
|
|
||||||
"subclasses of BaseFinder must provide a find() method"
|
|
||||||
)
|
|
||||||
|
|
||||||
def list(self, ignore_patterns):
|
def list(self, ignore_patterns):
|
||||||
"""
|
"""
|
||||||
Given an optional list of paths to ignore, return a two item iterable
|
Given an optional list of paths to ignore, return a two item iterable
|
||||||
@ -149,15 +102,10 @@ class FileSystemFinder(BaseFinder):
|
|||||||
)
|
)
|
||||||
return errors
|
return errors
|
||||||
|
|
||||||
# RemovedInDjango61Warning: When the deprecation ends, replace with:
|
def find(self, path, find_all=False):
|
||||||
# def find(self, path, find_all=False):
|
|
||||||
def find(self, path, find_all=False, **kwargs):
|
|
||||||
"""
|
"""
|
||||||
Look for files in the extra locations as defined in STATICFILES_DIRS.
|
Look for files in the extra locations as defined in STATICFILES_DIRS.
|
||||||
"""
|
"""
|
||||||
# RemovedInDjango61Warning.
|
|
||||||
if kwargs:
|
|
||||||
find_all = self._check_deprecated_find_param(find_all=find_all, **kwargs)
|
|
||||||
matches = []
|
matches = []
|
||||||
for prefix, root in self.locations:
|
for prefix, root in self.locations:
|
||||||
if root not in searched_locations:
|
if root not in searched_locations:
|
||||||
@ -232,15 +180,10 @@ class AppDirectoriesFinder(BaseFinder):
|
|||||||
for path in utils.get_files(storage, ignore_patterns):
|
for path in utils.get_files(storage, ignore_patterns):
|
||||||
yield path, storage
|
yield path, storage
|
||||||
|
|
||||||
# RemovedInDjango61Warning: When the deprecation ends, replace with:
|
def find(self, path, find_all=False):
|
||||||
# def find(self, path, find_all=False):
|
|
||||||
def find(self, path, find_all=False, **kwargs):
|
|
||||||
"""
|
"""
|
||||||
Look for files in the app directories.
|
Look for files in the app directories.
|
||||||
"""
|
"""
|
||||||
# RemovedInDjango61Warning.
|
|
||||||
if kwargs:
|
|
||||||
find_all = self._check_deprecated_find_param(find_all=find_all, **kwargs)
|
|
||||||
matches = []
|
matches = []
|
||||||
for app in self.apps:
|
for app in self.apps:
|
||||||
app_location = self.storages[app].location
|
app_location = self.storages[app].location
|
||||||
@ -287,15 +230,10 @@ class BaseStorageFinder(BaseFinder):
|
|||||||
self.storage = self.storage()
|
self.storage = self.storage()
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
# RemovedInDjango61Warning: When the deprecation ends, replace with:
|
def find(self, path, find_all=False):
|
||||||
# def find(self, path, find_all=False):
|
|
||||||
def find(self, path, find_all=False, **kwargs):
|
|
||||||
"""
|
"""
|
||||||
Look for files in the default file storage, if it's local.
|
Look for files in the default file storage, if it's local.
|
||||||
"""
|
"""
|
||||||
# RemovedInDjango61Warning.
|
|
||||||
if kwargs:
|
|
||||||
find_all = self._check_deprecated_find_param(find_all=find_all, **kwargs)
|
|
||||||
try:
|
try:
|
||||||
self.storage.path("")
|
self.storage.path("")
|
||||||
except NotImplementedError:
|
except NotImplementedError:
|
||||||
@ -336,18 +274,13 @@ class DefaultStorageFinder(BaseStorageFinder):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
# RemovedInDjango61Warning: When the deprecation ends, replace with:
|
def find(path, find_all=False):
|
||||||
# def find(path, find_all=False):
|
|
||||||
def find(path, find_all=False, **kwargs):
|
|
||||||
"""
|
"""
|
||||||
Find a static file with the given path using all enabled finders.
|
Find a static file with the given path using all enabled finders.
|
||||||
|
|
||||||
If ``find_all`` is ``False`` (default), return the first matching
|
If ``find_all`` is ``False`` (default), return the first matching
|
||||||
absolute path (or ``None`` if no match). Otherwise return a list.
|
absolute path (or ``None`` if no match). Otherwise return a list.
|
||||||
"""
|
"""
|
||||||
# RemovedInDjango61Warning.
|
|
||||||
if kwargs:
|
|
||||||
find_all = _check_deprecated_find_param(find_all=find_all, **kwargs)
|
|
||||||
searched_locations[:] = []
|
searched_locations[:] = []
|
||||||
matches = []
|
matches = []
|
||||||
for finder in get_finders():
|
for finder in get_finders():
|
||||||
|
@ -266,4 +266,5 @@ in Django 6.1.
|
|||||||
See :ref:`deprecated-features-5.2` for details on these changes, including how
|
See :ref:`deprecated-features-5.2` for details on these changes, including how
|
||||||
to remove usage of these features.
|
to remove usage of these features.
|
||||||
|
|
||||||
* ...
|
* The ``all`` parameter for the ``django.contrib.staticfiles.finders.find()``
|
||||||
|
function is removed in favor of the ``find_all`` parameter.
|
||||||
|
@ -4,15 +4,10 @@ from django.conf import settings
|
|||||||
from django.contrib.staticfiles import finders, storage
|
from django.contrib.staticfiles import finders, storage
|
||||||
from django.core.exceptions import ImproperlyConfigured
|
from django.core.exceptions import ImproperlyConfigured
|
||||||
from django.test import SimpleTestCase, override_settings
|
from django.test import SimpleTestCase, override_settings
|
||||||
from django.utils.deprecation import RemovedInDjango61Warning
|
|
||||||
|
|
||||||
from .cases import StaticFilesTestCase
|
from .cases import StaticFilesTestCase
|
||||||
from .settings import TEST_ROOT
|
from .settings import TEST_ROOT
|
||||||
|
|
||||||
DEPRECATION_MSG = (
|
|
||||||
"Passing the `all` argument to find() is deprecated. Use `find_all` instead."
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class TestFinders:
|
class TestFinders:
|
||||||
"""
|
"""
|
||||||
@ -35,47 +30,6 @@ class TestFinders:
|
|||||||
dst = [os.path.normcase(d) for d in dst]
|
dst = [os.path.normcase(d) for d in dst]
|
||||||
self.assertEqual(found, dst)
|
self.assertEqual(found, dst)
|
||||||
|
|
||||||
def test_find_all_deprecated_param(self):
|
|
||||||
src, dst = self.find_all
|
|
||||||
with self.assertWarnsMessage(RemovedInDjango61Warning, DEPRECATION_MSG) as ctx:
|
|
||||||
found = self.finder.find(src, all=True)
|
|
||||||
found = [os.path.normcase(f) for f in found]
|
|
||||||
dst = [os.path.normcase(d) for d in dst]
|
|
||||||
self.assertEqual(found, dst)
|
|
||||||
self.assertEqual(ctx.filename, __file__)
|
|
||||||
|
|
||||||
def test_find_all_conflicting_params(self):
|
|
||||||
src, dst = self.find_all
|
|
||||||
msg = (
|
|
||||||
f"{self.finder.__class__.__qualname__}.find() got multiple values for "
|
|
||||||
"argument 'find_all'"
|
|
||||||
)
|
|
||||||
with (
|
|
||||||
self.assertWarnsMessage(RemovedInDjango61Warning, DEPRECATION_MSG) as ctx,
|
|
||||||
self.assertRaisesMessage(TypeError, msg),
|
|
||||||
):
|
|
||||||
self.finder.find(src, find_all=True, all=True)
|
|
||||||
self.assertEqual(ctx.filename, __file__)
|
|
||||||
|
|
||||||
def test_find_all_unexpected_params(self):
|
|
||||||
src, dst = self.find_all
|
|
||||||
msg = (
|
|
||||||
f"{self.finder.__class__.__qualname__}.find() got an unexpected keyword "
|
|
||||||
"argument 'wrong'"
|
|
||||||
)
|
|
||||||
with (
|
|
||||||
self.assertWarnsMessage(RemovedInDjango61Warning, DEPRECATION_MSG) as ctx,
|
|
||||||
self.assertRaisesMessage(TypeError, msg),
|
|
||||||
):
|
|
||||||
self.finder.find(src, all=True, wrong=1)
|
|
||||||
self.assertEqual(ctx.filename, __file__)
|
|
||||||
|
|
||||||
with self.assertRaisesMessage(TypeError, msg):
|
|
||||||
self.finder.find(src, find_all=True, wrong=1)
|
|
||||||
|
|
||||||
with self.assertRaisesMessage(TypeError, msg):
|
|
||||||
self.finder.find(src, wrong=1)
|
|
||||||
|
|
||||||
|
|
||||||
class TestFileSystemFinder(TestFinders, StaticFilesTestCase):
|
class TestFileSystemFinder(TestFinders, StaticFilesTestCase):
|
||||||
"""
|
"""
|
||||||
@ -167,39 +121,6 @@ class TestMiscFinder(SimpleTestCase):
|
|||||||
[os.path.join(TEST_ROOT, "project", "documents")],
|
[os.path.join(TEST_ROOT, "project", "documents")],
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_searched_locations_deprecated_all(self):
|
|
||||||
with self.assertWarnsMessage(RemovedInDjango61Warning, DEPRECATION_MSG) as ctx:
|
|
||||||
finders.find("spam", all=True)
|
|
||||||
self.assertEqual(
|
|
||||||
finders.searched_locations,
|
|
||||||
[os.path.join(TEST_ROOT, "project", "documents")],
|
|
||||||
)
|
|
||||||
self.assertEqual(ctx.filename, __file__)
|
|
||||||
|
|
||||||
def test_searched_locations_conflicting_params(self):
|
|
||||||
msg = "find() got multiple values for argument 'find_all'"
|
|
||||||
with (
|
|
||||||
self.assertWarnsMessage(RemovedInDjango61Warning, DEPRECATION_MSG) as ctx,
|
|
||||||
self.assertRaisesMessage(TypeError, msg),
|
|
||||||
):
|
|
||||||
finders.find("spam", find_all=True, all=True)
|
|
||||||
self.assertEqual(ctx.filename, __file__)
|
|
||||||
|
|
||||||
def test_searched_locations_unexpected_params(self):
|
|
||||||
msg = "find() got an unexpected keyword argument 'wrong'"
|
|
||||||
with (
|
|
||||||
self.assertWarnsMessage(RemovedInDjango61Warning, DEPRECATION_MSG) as ctx,
|
|
||||||
self.assertRaisesMessage(TypeError, msg),
|
|
||||||
):
|
|
||||||
finders.find("spam", all=True, wrong=1)
|
|
||||||
self.assertEqual(ctx.filename, __file__)
|
|
||||||
|
|
||||||
with self.assertRaisesMessage(TypeError, msg):
|
|
||||||
finders.find("spam", find_all=True, wrong=1)
|
|
||||||
|
|
||||||
with self.assertRaisesMessage(TypeError, msg):
|
|
||||||
finders.find("spam", wrong=1)
|
|
||||||
|
|
||||||
@override_settings(MEDIA_ROOT="")
|
@override_settings(MEDIA_ROOT="")
|
||||||
def test_location_empty(self):
|
def test_location_empty(self):
|
||||||
msg = (
|
msg = (
|
||||||
|
Loading…
x
Reference in New Issue
Block a user