form file field upload error

351 views
Skip to first unread message

Josh Cartmell

unread,
Jan 31, 2013, 10:17:11 PM1/31/13
to mezzani...@googlegroups.com
I'm getting the following error when I try to submit a Mezzanine form that includes two larger files (the files are about 1.3mb each):

Traceback (most recent call last):

  File ".../lib/python2.7/site-packages/django/core/handlers/base.py", line 105, in get_response
    response = middleware_method(request, callback, callback_args, callback_kwargs)

  File "...lib/python2.7/site-packages/Mezzanine-1.2.3-py2.7.egg/mezzanine/pages/middleware.py", line 80, in process_view
    processor_response = processor(request, page)

  File "...lib/python2.7/site-packages/Mezzanine-1.2.3-py2.7.egg/mezzanine/forms/page_processors.py", line 58, in form_processor
    f.seek(0)

ValueError: I/O operation on closed file

After submitting the serving produces a 500 error.  The form entries, and files end up in the database, but no emails are sent to people who should get copies.

Anyone have any tips or pointers?

Thanks,
Josh

Stephen McDonald

unread,
Feb 3, 2013, 3:08:30 PM2/3/13
to mezzani...@googlegroups.com
That seek code is from django-forms-builder and is very old but I remember it clearly - it was needed at the time in order to do the email attachments, Django would have read the files already, and we wanted to re-read them in order to attach them.

I'm not sure if it's still needed, I'd lean towards leaving it in there - but perhaps we could just catch the exception you get and if that solves the problem, we should have an approach that works.

--
You received this message because you are subscribed to the Google Groups "Mezzanine Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mezzanine-use...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Stephen McDonald
http://jupo.org

Josh Cartmell

unread,
Feb 4, 2013, 2:40:06 PM2/4/13
to mezzani...@googlegroups.com
I tried catching the exception around f.seek(0) and that just causes the exception to be thrown on the next line where f.read() is called.  Do you know of any way to get the absolute path to the file so that I could re open it if the exception is caught on f.seek(0)?

I've tried:
try:
    f.seek(0)
except ValueError:
    f = open(f.name)

and
try:
    f.seek(0)
except ValueError:
    f = open(os.path.abspath(f.name))

Both of which fail.

I still am a bit confused as to why the file is closed at all.

Thanks for the help.

Josh Cartmell

unread,
Feb 14, 2013, 5:50:01 PM2/14/13
to mezzani...@googlegroups.com
Bump =)

At the point in the page processor where f.seek is called, is there any way to access the files location on disk?  That way I could just re open it.  I spent a little while digging around and I can't even figure out where the forms store file uploads at all.

Thanks!

Stephen McDonald

unread,
Feb 14, 2013, 5:54:32 PM2/14/13
to mezzani...@googlegroups.com

Josh Cartmell

unread,
Feb 14, 2013, 6:35:14 PM2/14/13
to mezzani...@googlegroups.com
Thanks for the pointer Steve, I'll see if I can figure out a fix, would you want me to submit it back in case anyone else has similar trouble?

Josh Cartmell

unread,
Feb 14, 2013, 7:20:08 PM2/14/13
to mezzani...@googlegroups.com
I found them, they are getting stored outside of the project directory in a folder called forms directly beneath the users' home directory.  Is that normal/expected behavior?

Josh Cartmell

unread,
Feb 14, 2013, 8:06:58 PM2/14/13
to mezzani...@googlegroups.com
Ok, I think I have a fix.  I change the attachment appending here:
https://github.com/stephenmcd/mezzanine/blob/master/mezzanine/forms/page_processors.py#L57

to this:

try:
    attachments = []
    for f in form.files.values():
        f.seek(0)
        attachments.append((f.name, f.read()))
except ValueError:
    attachments = []
    for field_entry in entry.fields.all():
        try:
            f = open(join(fs.location, field_entry.value))
        except IOError:
            pass
        else:
            f.seek(0)
            attachments.append((f.name.split('/')[-1], f.read()))

It tries to add the attachments the normal way, but if any of them produces a ValueError (in my case I believe this is due to the file no longer existing in /tmp) it looks through all the entries fields, checking if they are files and attaching them if so.

What do you think?

Stephen McDonald

unread,
Feb 14, 2013, 8:51:17 PM2/14/13
to mezzani...@googlegroups.com
Maybe we can just nuke the old way and only use the new one you've got? Also I guess the new way should use the storages API instead of assuming a local filesystem?

Josh Cartmell

unread,
Feb 15, 2013, 12:22:15 AM2/15/13
to mezzani...@googlegroups.com

That would work, I left the old way because I thought my new one might be a bit less efficient. You are probably right about using the storages API (I haven't really kept up with that bit of Django). I basically copied the way the admin opens the files when when you download them from viewing entries, so that may need updating too.

Josh Cartmell

unread,
Feb 20, 2013, 4:19:49 PM2/20/13
to mezzani...@googlegroups.com
Hey Steve sorry it's taken me a bit to get back to this.  I'm not really up on the storages API so it might be awhile before I get to this.  If you easily know how to take what I've done and make it use the storages API I don't mind at all.

I still also think it's a bit strange that the way things work right now uploaded files aren't even being saved in the project directory but are being stuck in a directory called "forms" which is a child of the home directory of the linux user running the site.

Stephen McDonald

unread,
Feb 21, 2013, 1:18:36 PM2/21/13
to mezzani...@googlegroups.com
On Thu, Feb 21, 2013 at 8:19 AM, Josh Cartmell <joshc...@gmail.com> wrote:
Hey Steve sorry it's taken me a bit to get back to this.  I'm not really up on the storages API so it might be awhile before I get to this.  If you easily know how to take what I've done and make it use the storages API I don't mind at all.

I still also think it's a bit strange that the way things work right now uploaded files aren't even being saved in the project directory but are being stuck in a directory called "forms" which is a child of the home directory of the linux user running the site.

The idea is that the files shouldn't be stored in the static directory or anywhere publicly accessible - there's a FORMS_UPLOAD_ROOT setting for configuring where they should go.

Josh Cartmell

unread,
Feb 21, 2013, 1:46:48 PM2/21/13
to mezzani...@googlegroups.com
That makes sense, I guess I wasn't thinking about it from a security standpoint, thanks!

Alexander Kominek

unread,
Dec 23, 2013, 12:40:13 AM12/23/13
to mezzani...@googlegroups.com
Hi guys,

I'm currently running into the same issue on my site - is going to have a permanent fix?

Thanks,

 - Alex

Stephen McDonald

unread,
Mar 28, 2014, 10:28:14 PM3/28/14
to mezzani...@googlegroups.com

Alexander Kominek

unread,
Apr 21, 2014, 1:05:59 PM4/21/14
to mezzani...@googlegroups.com
Thanks, Stephen!


--
You received this message because you are subscribed to a topic in the Google Groups "Mezzanine Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mezzanine-users/eyaEfiCfuB8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mezzanine-use...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages