Ticket #9610: 9610.diff

File 9610.diff, 2.2 KB (added by Andy Durdin, 10 years ago)

Added tests, and updated the patch to unified diff format.

  • django/core/files/storage.py

     
    6363        Returns a filename that's free on the target storage system, and
    6464        available for new content to be written to.
    6565        """
     66
     67        dir_name, file_name = os.path.split(name)
    6668        # If the filename already exists, keep adding an underscore to the name
    6769        # of the file until the filename doesn't exist.
    6870        while self.exists(name):
    6971            try:
    70                 dot_index = name.rindex('.')
     72                dot_index = file_name.rindex('.')
    7173            except ValueError: # filename has no dot
    72                 name += '_'
     74                file_name += '_'
    7375            else:
    74                 name = name[:dot_index] + '_' + name[dot_index:]
     76                file_name = file_name[:dot_index] + '_' + file_name[dot_index:]
     77            name = os.path.join(dir_name, file_name)
    7578        return name
    7679
    7780    def path(self, name):
  • tests/regressiontests/file_storage/tests.py

     
    141141        actual_mode = os.stat(self.storage.path(name))[0] & 0777
    142142        self.assertEqual(actual_mode, 0666)
    143143
     144
     145class FileStorageDirectoryNameParsing(TestCase):
     146    """Regression test for #9610: Using a dotted pathname would mangle
     147    the directory name instead of the filename if the file already
     148    existed."""
     149    def setUp(self):
     150        self.storage_dir = tempfile.mkdtemp()
     151        self.storage = FileSystemStorage(self.storage_dir)
     152
     153    def tearDown(self):
     154        shutil.rmtree(self.storage_dir)
     155
     156    def test_directory_name_parsing(self):
     157        self.storage.save('dotted.path/test', ContentFile("1"))
     158        self.storage.save('dotted.path/test', ContentFile("2"))
     159
     160        self.assertFalse(os.path.exists(os.path.join(self.storage_dir, 'dotted_.path')))
     161        self.assertTrue(os.path.exists(os.path.join(self.storage_dir, 'dotted.path/test_')))
Back to Top