Fixed #22299 -- Cleanup wizard temp files when possible.

Thanks to @erikr for the review.
This commit is contained in:
Simon Charette 2014-03-21 03:38:25 -04:00
parent b66e85342b
commit fa8b473528
3 changed files with 30 additions and 4 deletions

View File

@ -1,10 +1,17 @@
from datetime import datetime from datetime import datetime
from importlib import import_module from importlib import import_module
import os
import tempfile
from django.http import HttpRequest from django.http import HttpRequest
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.core.files.storage import FileSystemStorage
from django.core.files.uploadedfile import SimpleUploadedFile
temp_storage_location = tempfile.mkdtemp(dir=os.environ.get('DJANGO_TEST_TEMP_DIR'))
temp_storage = FileSystemStorage(location=temp_storage_location)
def get_request(): def get_request():
@ -85,3 +92,17 @@ class TestStorage(object):
storage.extra_data['test'] = True storage.extra_data['test'] = True
self.assertTrue('test' in storage.extra_data) self.assertTrue('test' in storage.extra_data)
def test_reset_deletes_tmp_files(self):
request = get_request()
storage = self.get_storage()('wizard1', request, temp_storage)
step = 'start'
file_ = SimpleUploadedFile('file.txt', b'content')
storage.set_step_files(step, {'file': file_})
tmp_name = storage.get_step_files(step)['file'].name
self.assertTrue(storage.file_storage.exists(tmp_name))
storage.reset()
self.assertFalse(storage.file_storage.exists(tmp_name))

View File

@ -26,6 +26,11 @@ class BaseStorage(object):
} }
def reset(self): def reset(self):
# Delete temporary files before breaking reference to them.
wizard_files = self.data[self.step_files_key]
for step_files in six.itervalues(wizard_files):
for step_file in six.itervalues(step_files):
self.file_storage.delete(step_file['tmp_name'])
self.init_data() self.init_data()
def _get_current_step(self): def _get_current_step(self):

View File

@ -629,9 +629,9 @@ storage class <builtin-fs-storage>`)::
.. warning:: .. warning::
Please remember to take care of removing old files as the Please remember to take care of removing old temporary files, as the
:class:`WizardView` won't remove any files, whether the wizard gets :class:`WizardView` will only remove these files if the wizard finishes
finished correctly or not. correctly.
Conditionally view/skip specific steps Conditionally view/skip specific steps
====================================== ======================================