Ticket #15641: 15641.diff

File 15641.diff, 2.7 KB (added by Harm Geerts <hgeerts@…>, 14 years ago)
  • tests/regressiontests/file_storage/tests.py

     
    8888
    8989class FileStorageTests(unittest.TestCase):
    9090    storage_class = FileSystemStorage
     91    base_url = '/test_media_url/'
    9192
    9293    def setUp(self):
    9394        self.temp_dir = tempfile.mktemp()
    9495        os.makedirs(self.temp_dir)
    9596        self.storage = self.storage_class(location=self.temp_dir,
    96             base_url='/test_media_url/')
     97            base_url=self.base_url)
    9798
    9899    def tearDown(self):
    99100        shutil.rmtree(self.temp_dir)
     
    207208        # should encode special chars except ~!*()'
    208209        # like encodeURIComponent() JavaScript function do
    209210        self.assertEqual(self.storage.url(r"""~!*()'@#$%^&*abc`+=.file"""),
    210             """/test_media_url/~!*()'%40%23%24%25%5E%26*abc%60%2B%3D.file""")
     211            self.base_url + """~!*()'%40%23%24%25%5E%26*abc%60%2B%3D.file""")
    211212
    212213        # should stanslate os path separator(s) to the url path separator
    213214        self.assertEqual(self.storage.url("""a/b\\c.file"""),
    214             """/test_media_url/a/b/c.file""")
     215            self.base_url + """a/b/c.file""")
    215216
    216217        self.storage.base_url = None
    217218        self.assertRaises(ValueError, self.storage.url, 'test.file')
     
    265266        self.assertRaises(SuspiciousOperation, self.storage.exists, '..')
    266267        self.assertRaises(SuspiciousOperation, self.storage.exists, '/etc/passwd')
    267268
     269class NoUrlFileStorageTests(FileStorageTests):
     270    base_url = None
     271
     272    def test_file_url(self):
     273        """
     274        File storage raises ValueError if initialized with base_url=None
     275        """
     276        self.assertRaises(ValueError, self.storage.url, 'test.file')
     277
     278
    268279class CustomStorage(FileSystemStorage):
    269280    def get_available_name(self, name):
    270281        """
  • django/core/files/storage.py

     
    142142        """
    143143        raise NotImplementedError()
    144144
     145UNUSABLE_BASE_URL = object()
     146
    145147class FileSystemStorage(Storage):
    146148    """
    147149    Standard filesystem storage
    148150    """
    149151
    150     def __init__(self, location=None, base_url=None):
     152    def __init__(self, location=None, base_url=UNUSABLE_BASE_URL):
    151153        if location is None:
    152154            location = settings.MEDIA_ROOT
    153         if base_url is None:
     155        if base_url is UNUSABLE_BASE_URL:
    154156            base_url = settings.MEDIA_URL
    155157        self.location = os.path.abspath(location)
    156158        self.base_url = base_url
Back to Top