Fixed #26896 -- Allowed a lazy base_url for FileSystemStorage.

This commit is contained in:
Tomas Pazderka 2016-07-28 13:37:07 +02:00 committed by Tim Graham
parent 0850236a8c
commit b820b6108a
2 changed files with 12 additions and 3 deletions

View File

@ -254,8 +254,6 @@ class FileSystemStorage(Storage):
def __init__(self, location=None, base_url=None, file_permissions_mode=None, def __init__(self, location=None, base_url=None, file_permissions_mode=None,
directory_permissions_mode=None): directory_permissions_mode=None):
self._location = location self._location = location
if base_url is not None and not base_url.endswith('/'):
base_url += '/'
self._base_url = base_url self._base_url = base_url
self._file_permissions_mode = file_permissions_mode self._file_permissions_mode = file_permissions_mode
self._directory_permissions_mode = directory_permissions_mode self._directory_permissions_mode = directory_permissions_mode
@ -286,6 +284,8 @@ class FileSystemStorage(Storage):
@cached_property @cached_property
def base_url(self): def base_url(self):
if self._base_url is not None and not self._base_url.endswith('/'):
self._base_url += '/'
return self._value_or_setting(self._base_url, settings.MEDIA_URL) return self._value_or_setting(self._base_url, settings.MEDIA_URL)
@cached_property @cached_property

View File

@ -24,6 +24,7 @@ from django.test import (
override_settings, override_settings,
) )
from django.test.utils import requires_tz_support from django.test.utils import requires_tz_support
from django.urls import NoReverseMatch, reverse_lazy
from django.utils import six, timezone from django.utils import six, timezone
from django.utils._os import upath from django.utils._os import upath
from django.utils.deprecation import RemovedInDjango20Warning from django.utils.deprecation import RemovedInDjango20Warning
@ -73,7 +74,7 @@ class GetStorageClassTests(SimpleTestCase):
get_storage_class('django.core.files.non_existing_storage.NonExistingStorage') get_storage_class('django.core.files.non_existing_storage.NonExistingStorage')
class FileStorageDeconstructionTests(unittest.TestCase): class FileSystemStorageTests(unittest.TestCase):
def test_deconstruction(self): def test_deconstruction(self):
path, args, kwargs = temp_storage.deconstruct() path, args, kwargs = temp_storage.deconstruct()
@ -89,6 +90,14 @@ class FileStorageDeconstructionTests(unittest.TestCase):
path, args, kwargs = storage.deconstruct() path, args, kwargs = storage.deconstruct()
self.assertEqual(kwargs, kwargs_orig) self.assertEqual(kwargs, kwargs_orig)
def test_lazy_base_url_init(self):
"""
FileSystemStorage.__init__() shouldn't evaluate base_url.
"""
storage = FileSystemStorage(base_url=reverse_lazy('app:url'))
with self.assertRaises(NoReverseMatch):
storage.url(storage.base_url)
# Tests for TZ-aware time methods need pytz. # Tests for TZ-aware time methods need pytz.
requires_pytz = unittest.skipIf(pytz is None, "this test requires pytz") requires_pytz = unittest.skipIf(pytz is None, "this test requires pytz")