mirror of
https://github.com/django/django.git
synced 2025-08-04 19:08:28 +00:00
Fixed #26297 -- Fixed collectstatic --clear
crash if storage doesn't implement path().
This commit is contained in:
parent
ecb59cc657
commit
28bcff82c5
4 changed files with 54 additions and 5 deletions
|
@ -1,5 +1,8 @@
|
|||
import errno
|
||||
import os
|
||||
from datetime import datetime
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib.staticfiles.storage import CachedStaticFilesStorage
|
||||
from django.core.files import storage
|
||||
from django.utils import timezone
|
||||
|
@ -22,6 +25,40 @@ class DummyStorage(storage.Storage):
|
|||
return datetime.datetime(1970, 1, 1, tzinfo=timezone.utc)
|
||||
|
||||
|
||||
class PathNotImplementedStorage(storage.Storage):
|
||||
|
||||
def _save(self, name, content):
|
||||
return 'dummy'
|
||||
|
||||
def _path(self, name):
|
||||
return os.path.join(settings.STATIC_ROOT, name)
|
||||
|
||||
def exists(self, name):
|
||||
return os.path.exists(self._path(name))
|
||||
|
||||
def listdir(self, path):
|
||||
path = self._path(path)
|
||||
directories, files = [], []
|
||||
for entry in os.listdir(path):
|
||||
if os.path.isdir(os.path.join(path, entry)):
|
||||
directories.append(entry)
|
||||
else:
|
||||
files.append(entry)
|
||||
return directories, files
|
||||
|
||||
def delete(self, name):
|
||||
name = self._path(name)
|
||||
if os.path.exists(name):
|
||||
try:
|
||||
os.remove(name)
|
||||
except OSError as e:
|
||||
if e.errno != errno.ENOENT:
|
||||
raise
|
||||
|
||||
def path(self, name):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
class SimpleCachedStaticFilesStorage(CachedStaticFilesStorage):
|
||||
|
||||
def file_hash(self, name, content=None):
|
||||
|
|
|
@ -170,6 +170,11 @@ class TestCollectionClear(CollectionTestCase):
|
|||
shutil.rmtree(six.text_type(settings.STATIC_ROOT))
|
||||
super(TestCollectionClear, self).run_collectstatic(clear=True)
|
||||
|
||||
@override_settings(STATICFILES_STORAGE='staticfiles_tests.storage.PathNotImplementedStorage')
|
||||
def test_handle_path_notimplemented(self):
|
||||
self.run_collectstatic()
|
||||
self.assertFileNotFound('cleared.txt')
|
||||
|
||||
|
||||
class TestCollectionExcludeNoDefaultIgnore(CollectionTestCase, TestDefaults):
|
||||
"""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue