summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2014-03-21 03:38:25 -0400
committerSimon Charette <charette.s@gmail.com>2014-03-22 17:48:12 -0400
commitfa8b4735287c92b03d23823de8a4e1168d1951d9 (patch)
tree732ae802655b98460785200f25c255dbfc1031bd
parentb66e85342b6ab28b7f48b47fe3fcfbccd5d9943f (diff)
Fixed #22299 -- Cleanup wizard temp files when possible.
Thanks to @erikr for the review.
-rw-r--r--django/contrib/formtools/tests/wizard/storage.py23
-rw-r--r--django/contrib/formtools/wizard/storage/base.py5
-rw-r--r--docs/ref/contrib/formtools/form-wizard.txt6
3 files changed, 30 insertions, 4 deletions
diff --git a/django/contrib/formtools/tests/wizard/storage.py b/django/contrib/formtools/tests/wizard/storage.py
index c54ed9a058..5e2d234558 100644
--- a/django/contrib/formtools/tests/wizard/storage.py
+++ b/django/contrib/formtools/tests/wizard/storage.py
@@ -1,10 +1,17 @@
from datetime import datetime
from importlib import import_module
+import os
+import tempfile
from django.http import HttpRequest
from django.conf import settings
-
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():
@@ -85,3 +92,17 @@ class TestStorage(object):
storage.extra_data['test'] = True
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))
diff --git a/django/contrib/formtools/wizard/storage/base.py b/django/contrib/formtools/wizard/storage/base.py
index 2f73e766eb..d58cab0039 100644
--- a/django/contrib/formtools/wizard/storage/base.py
+++ b/django/contrib/formtools/wizard/storage/base.py
@@ -26,6 +26,11 @@ class BaseStorage(object):
}
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()
def _get_current_step(self):
diff --git a/docs/ref/contrib/formtools/form-wizard.txt b/docs/ref/contrib/formtools/form-wizard.txt
index c8caed746a..1506b3c189 100644
--- a/docs/ref/contrib/formtools/form-wizard.txt
+++ b/docs/ref/contrib/formtools/form-wizard.txt
@@ -629,9 +629,9 @@ storage class <builtin-fs-storage>`)::
.. warning::
- Please remember to take care of removing old files as the
- :class:`WizardView` won't remove any files, whether the wizard gets
- finished correctly or not.
+ Please remember to take care of removing old temporary files, as the
+ :class:`WizardView` will only remove these files if the wizard finishes
+ correctly.
Conditionally view/skip specific steps
======================================