#35326 closed Bug (fixed)
OverwritingStorageTests fail if a TemporaryUploadedFile is used
Reported by: | bcail | Owned by: | bcail |
---|---|---|---|
Component: | File uploads/storage | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | bcail, Tim Graham | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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)
Change History (19)
comment:1 by , 8 months ago
comment:2 by , 8 months ago
@alexialg05, yes, you can assign yourself to this issue, but we should probably see if others confirm this as an issue. If it is confirmed as an issue, there may be design questions to figure out.
comment:3 by , 8 months ago
Cc: | added |
---|---|
Triage Stage: | Unreviewed → Accepted |
Hello bcail, thank you for your report!
This definitely seems like a valid issue, file_move_safe
(from django.core.files.move
) requires the allow_overwrite
flag to be passed to perform overwrites. I don't have clarity on the possible fix, I agree that it requires a design decision. A forum post seeking advice would be the best next step, I think.
comment:6 by , 7 months ago
Has patch: | set |
---|---|
Owner: | changed from | to
Status: | new → assigned |
comment:7 by , 7 months ago
Patch needs improvement: | set |
---|
comment:8 by , 6 months ago
Patch needs improvement: | unset |
---|
comment:9 by , 6 months ago
Patch needs improvement: | set |
---|
comment:10 by , 6 months ago
Patch needs improvement: | unset |
---|
comment:11 by , 6 months ago
Patch needs improvement: | set |
---|
comment:12 by , 6 months ago
Patch needs improvement: | unset |
---|
comment:13 by , 6 months ago
Triage Stage: | Accepted → Ready for checkin |
---|
can i be assigned on this issue?