Friday, August 22, 2014

Re: Handling temporary file storage with WizardView

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

On Friday, September 13, 2013 8:44:13 AM UTC+1, dspruell wrote:
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

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/00bc2a6a-3e96-47f1-ba48-e402bea2f140%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment