Ticket #28144 added the option of using custom flags on a storage object to allow overwriting files in storage. However, this doesn't seem to work for temporary uploaded files, since the alternate path is taken in the _save method.
Here is an example test that fails for me - it loops forever:
diff --git a/tests/file_storage/tests.py b/tests/file_storage/tests.py
index 420314573d..d404300708 100644
--- a/tests/file_storage/tests.py
+++ b/tests/file_storage/tests.py
@@ -648,6 +648,34 @@ class OverwritingStorageTests(FileStorageTests):
finally:
self.storage.delete(name)
+ def test_save_overwrite_behavior_temp_file(self):
+ """Saving to same file name twice overwrites the first file."""
+ name = "test.file"
+ self.assertFalse(self.storage.exists(name))
+ content_1 = b"content one"
+ content_2 = b"second content"
+ f_1 = TemporaryUploadedFile('tmp1', 'text/plain', 11, 'utf8')
+ f_1.write(content_1)
+ f_1.seek(0)
+ f_2 = TemporaryUploadedFile('tmp2', 'text/plain', 14, 'utf8')
+ f_2.write(content_2)
+ f_2.seek(0)
+ stored_name_1 = self.storage.save(name, f_1)
+ try:
+ self.assertEqual(stored_name_1, name)
+ self.assertTrue(self.storage.exists(name))
+ self.assertTrue(os.path.exists(os.path.join(self.temp_dir, name)))
+ with self.storage.open(name) as fp:
+ self.assertEqual(fp.read(), content_1)
+ stored_name_2 = self.storage.save(name, f_2)
+ self.assertEqual(stored_name_2, name)
+ self.assertTrue(self.storage.exists(name))
+ self.assertTrue(os.path.exists(os.path.join(self.temp_dir, name)))
+ with self.storage.open(name) as fp:
+ self.assertEqual(fp.read(), content_2)
+ finally:
+ self.storage.delete(name)
can i be assigned on this issue?