On 29 mar, 18:59, pjmorse <flashesofpa...@gmail.com> wrote:
> On Mar 29, 8:22 am, bruno desthuilliers
>
> <bruno.desthuilli...@gmail.com> wrote:
> > On 19 mar, 20:05,pjmorse<flashesofpa...@gmail.com> wrote:
>
> > > In my application's admin console, there's a tiny form for updating
> > > the image associated with a specific model.
>
> > > When a file is uploaded, the application reaches this code:
>
> > >         if request.method == 'POST':
> > >                 file = request.FILES.copy()
>
> > Err... Do you really need this line ??? I don't know what your
> > remaining code (the one you didn't show) is doing exactly, but as far
> > as i'm concerned, I never needed to try & copy request.FILES...
>
> That's a good question, and one I hadn't been prepared to answer; I
> didn't write this code, I just try to maintain it.
>
> The next steps in the code are to manipulate the file name. Trying to
> do this directly on request.FILES results in the error,
> "'InMemoryUploadedFile' object is unsubscriptable".

Uh. Oh, well - then you indeed have a problem :(

> Here's a wider chunk of the code. I've reformatted the comments to
> make things a bit more compact.
>
>         if request.method == 'POST':
>                 file = request.FILES.copy() # Handle profile image upload
>
>                 if 'image' in file: # Profile image has been submitted
>                         img = file['image']
>
>                         # Compute filename
>                         lst = split( img['filename'] )
>                         random.seed()
>                         filename = 'wmm_%d%s' %
> ( random.randrange(1,10000000000000,2),str( splitext( lst[1] )[1] ))
>                         open('/tmp/' + filename, 'wb').write(img['content'])


Hum... Is that legacy code ? Looks pretty ugly to me - wouldn't pass a
code review here.
Well, assuming you're using at least Django 1.0, I suggest you get rid
of this mess and make appropriate use of FileField /
ImageField.upload_to:

http://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.FileField.upload_to


> I think this tells me that what I need to figure out is how to safely
> copy an InMemoryUploadedFile object.

The above code is NOT safe in anyway.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to