Handling temporary file storage with WizardView

133 views
Skip to first unread message

Darren Spruell

unread,
Sep 13, 2013, 3:44:13 AM9/13/13
to django...@googlegroups.com
I'm working on a WizardView (django.contrib.formtools) and have a step
that handles a file upload, so I'm setting the file_storage attribute
as required. I'd like the file storage to be a securely created
temporary directory under /tmp so I'm trying to set it to an instance
of tempfile.mkdtemp as below, but I'm stumping myself:


class FileSubmissionWizardView(SessionWizardView):
"Form wizard implementation to handle uploaded samples"

# XXX required. Also, need to take care of removing dir/file in the end.
file_storage = FileSystemStorage(location=mkdtemp(prefix='avsubmit_'))

def done(self, form_list, **kwargs):
# do_something_with_the_form_data(form_list)

# Remove temp upload directory. Eh?
shutil.rmtree(self.file_storage.location)

# Set a message, do a log
return redirect('avsubmit_index')

def process_step_files(self, form):
# do something like set storage extra data
return self.get_form_step_files(form)


My fu is weak, so I'm not certain the right approach here.
file_storage is a class attribute (right?), so I'm not sure this meets
my envisioned goal of having a new temporary directory created for
each uploaded file which I can then delete when finished. Would like
this workflow:

1. User uploads file
2. View invoked to process form, creates _new_ temporary directory for
FileSystemStorage and writes uploaded file to dir. Variable is
populated containing path from mkdtemp() to delete later
3. View processes form
4. View calls shutil.rmtree to delete temporary directory and file
5. View complete

How far off of target am I? Is there a good way to do the above file
resource management using WizardView?

--
Darren Spruell
phatb...@gmail.com

zubair alam

unread,
Aug 22, 2014, 10:03:21 AM8/22/14
to django...@googlegroups.com
although this post older, you might have found a solution for your problem...how you did that.
I think if file path is set to something where you want to save it finally would be much better than cleaning tmp directory.
destination folder can be specified to FileSystemStorage
Reply all
Reply to author
Forward
0 new messages