From f734e2d4b2fc4391a4d097b80357724815c1d414 Mon Sep 17 00:00:00 2001 From: Adam Chidlow Date: Fri, 14 Oct 2016 11:13:43 +0800 Subject: Fixed #27334 -- Allowed FileField to move rather than copy a file. When a FileField is set to an instance of File that is not also an instance of FieldFile, pre_save() passes that object as the contents to Storage.save(). This allows the file to be moved rather than copied to the upload destination. --- tests/model_fields/test_filefield.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'tests/model_fields') diff --git a/tests/model_fields/test_filefield.py b/tests/model_fields/test_filefield.py index 19d414161d..e67b6dd515 100644 --- a/tests/model_fields/test_filefield.py +++ b/tests/model_fields/test_filefield.py @@ -1,4 +1,10 @@ -from django.test import TestCase +import os +import sys +import unittest + +from django.core.files import temp +from django.core.files.uploadedfile import TemporaryUploadedFile +from django.test import TestCase, override_settings from .models import Document @@ -54,3 +60,16 @@ class FileFieldTests(TestCase): def test_defer(self): Document.objects.create(myfile='something.txt') self.assertEqual(Document.objects.defer('myfile')[0].myfile, 'something.txt') + + @unittest.skipIf(sys.platform.startswith('win'), "Windows doesn't support moving open files.") + # The file's source and destination must be on the same filesystem. + @override_settings(MEDIA_ROOT=temp.gettempdir()) + def test_move_temporary_file(self): + """ + The temporary uploaded file is moved rather than copied to the + destination. + """ + with TemporaryUploadedFile('something.txt', 'text/plain', 0, 'UTF-8') as tmp_file: + tmp_file_path = tmp_file.temporary_file_path() + Document.objects.create(myfile=tmp_file) + self.assertFalse(os.path.exists(tmp_file_path), 'Temporary file still exists') -- cgit v1.3