From c95a3fadb7356e766fe48fae69ce81355180f46a Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Fri, 21 Mar 2014 03:38:25 -0400 Subject: [PATCH] [1.7.x] Fixed #22299 -- Cleanup wizard temp files when possible. Thanks to @erikr for the review. Backport of fa8b4735287c92b03d23823de8a4e1168d1951d9 from master. --- django/contrib/formtools/tests/wizard/storage.py | 9 ++++++++- django/contrib/formtools/wizard/storage/base.py | 5 +++++ docs/ref/contrib/formtools/form-wizard.txt | 6 +++--- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/django/contrib/formtools/tests/wizard/storage.py b/django/contrib/formtools/tests/wizard/storage.py index 4ebc64c4f2d..cec2c015997 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(): diff --git a/django/contrib/formtools/wizard/storage/base.py b/django/contrib/formtools/wizard/storage/base.py index 78bdd163c1a..f9ef9d6e497 100644 --- a/django/contrib/formtools/wizard/storage/base.py +++ b/django/contrib/formtools/wizard/storage/base.py @@ -27,6 +27,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 c559296f07e..d41b025f2e9 100644 --- a/docs/ref/contrib/formtools/form-wizard.txt +++ b/docs/ref/contrib/formtools/form-wizard.txt @@ -634,9 +634,9 @@ storage class `):: .. 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 ======================================