Opened 7 years ago

Closed 4 months ago

#7808 closed New feature (fixed)

Form Preview does not work with file uploads

Reported by: ian_brasil Owned by: nobody
Component: contrib.formtools Version: master
Severity: Normal Keywords: FormPreview
Cc: Fak3 Triage Stage: Accepted
Has patch: no Needs documentation: yes
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by gabrielhurley)

There seems to be no way to bind a form to a Form Preview (using request.FILES)...
i tried ignoring the specific warning not to override in preview.py and tried to override the preview_post() method but this did not work.

Additionally this is not documented on http://docs.djangoproject.com/en/dev/ref/contrib/formtools/form-preview/

Change History (11)

comment:1 Changed 7 years ago by ericholscher

  • milestone set to 1.0
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Design decision needed

comment:2 Changed 7 years ago by oyvind

  • Resolution set to wontfix
  • Status changed from new to closed

You can't preview files since you can't pass on filefields as a hidden field or fill in filefields in any browser. Filefields will always have to be filled again.

I would not consider this a bug, just something out of scope for formpreview.

comment:3 Changed 5 years ago by anonymous

  • Resolution wontfix deleted
  • Status changed from closed to reopened

This should be mentioned in the FormPreview documentation.

comment:4 Changed 5 years ago by adamnelson

  • Component changed from Uncategorized to Documentation
  • milestone 1.0 deleted

Moving out of version milestone:1.0 for triage based on the anonymous comment.

comment:5 Changed 4 years ago by gabrielhurley

  • Description modified (diff)
  • Triage Stage changed from Design decision needed to Accepted

My this is an old ticket. I had to update the link to the docs in the ticket description, it was that old.

Anyhow, It seems like a reasonable admonition to add to the Form Preview docs as suggested by anonymous.

comment:6 Changed 4 years ago by ijstokes

  • Component changed from Documentation to django.contrib.formtools
  • Keywords FormPreview added
  • Needs documentation set

This is not an impossible mission, having FormPreview support files. I made some reasonable progress in about an hour, but it would probably be 1-3 days to get something really working. Below are my notes on how this could be done. I'm going to try and move this to django.contrib.formtools, although a Documentation update would be appreciated -- I wasted several hours reading through FormPreview and trying to get it setup before I realized this wasn't supported, and then put in the extra time to see if I could make it work. Just saying somewhere "This currently doesn't work for Forms with FileFields" would be great.

Ian

I've spent about an hour on this and realized that it isn't an easy fix, so I'll have to leave it. FormPreview doesn't support forms with files attached. Search for FormPreview in the django-users list and you'll see more of my comments on this. The couple of things I had to do to get part way to making this work:

  1. Need hashing algorithm to skip FileFields. I did this by adding a class attribute "has_exclude" to the ModelForm and then when looping through the form members I added:

exclude = getattr(form,'hash_exclude', [])
for bp in form:

if bp.name in exclude:

continue

In fact, this should be more sophisticated. It should include the *filename* in the hash, but not the file itself. This way the filename forms part of the hash which can be used to recover the "temporary" uploaded file location from the preview stage, and place it into the correct final location. If it isn't part of the hash then the selected file could be manipulated.

  1. Default FormPreview templates need to include enctype="multipart/form-data" attribute on the <form> element.
  1. Need to test form.is_multipart() to see if form should be constructed by:

f = form(request.POST, auto_id=...)

or

f = form(request.POST, request.FILES, auto_id=...)

  1. And now the tricky bit I didn't get to: how to handle uploaded forms. My suggestion would be to place these in a temporary holding location using a directory taken from the hash. The preview stage would then show the results of any file upload validation (since the files would be submitted with the original submit), and if they are OK, just include the filename. If they are not OK, then the form returns to the original submission page with the errors displayed. If the user confirms everything is alright then the second stage submit sends some hidden field which is associated with the original upload file field and this is used to fetch the originally uploaded file from the hash location and then move it to the final location.

Anyway, that is just the general idea. Maybe there is an easier/better way to do this.

If file uploads aren't OK for FormPreview, then the documentation should reflect this so people don't spend time trying it out only to realize this kind of complicated and fancy handling isn't yet in place.

Ian

comment:7 Changed 4 years ago by ijstokes

Another quick point on what would need to be done in the event of a "partial" submitted form where the file is uploaded but then the form isn't finalized/saved/second-stage-submitted. It would be necessary to clean up these old files. My best idea of how to do this would be to run some kind of "tmpwatch" on the temporary parent directory with a 1-48 hour time limit. This would build it into the operation of the system. Otherwise the developer would need to actually include a tmpwatch cronjob or similar to clean "abandoned" files and directories.

comment:8 Changed 4 years ago by lukeplant

  • Severity set to Normal
  • Type set to New feature

comment:9 Changed 3 years ago by Fak3

  • Cc Fak3 added
  • Easy pickings unset
  • UI/UX unset

comment:10 Changed 2 years ago by aaugustin

  • Status changed from reopened to new

comment:11 Changed 4 months ago by gchp

  • Resolution set to fixed
  • Status changed from new to closed

formtools has been extracted into its own repository (​​​​https://github.com/django/django-formtools/). Because of this, the issue tracking for this package has been moved to GitHub issues. I'm going to close this ticket, but I've created a GitHub issue to replace it where the conversation can continue: ​​​​https://github.com/django/django-formtools/issues/23. Thanks!

Note: See TracTickets for help on using tickets.
Back to Top