Django

Code

Ticket #8501 (new)

Opened 1 year ago

Last modified 1 year ago

file storage should allow saving of basic file-like objects

Reported by: jaboja@gmail.com Assigned to: anonymous
Milestone: Component: File uploads/storage
Version: SVN Keywords: urllib, file
Cc: Triage Stage: Accepted
Has patch: 1 Needs documentation: 1
Needs tests: 0 Patch needs improvement: 1

Description

It is not possible to save file opened by urllib2.urlopen into field of a model. When trying to do that following way:

f = File(urlopen("http://something.com/somefile.ext").fp) MODEL.FIELD.save("name.ext", f, save=False)

Django throws AttributeError?: '_fileobject' object has no attribute 'seek'

Probeably django\core\files\base.py on line checks if class has attribute seek, but it is being definied at line 125 even when constructor gets an object that does not have such attribute (like urllib2.addinfourl.fp which is socket._fileobject).

Attachments

8501v1.diff (4.5 kB) - added by daonb <bennydaon@gmail.com> on 09/17/08 15:03:31.

Change History

09/16/08 05:49:05 changed by daonb <bennydaon@gmail.com>

  • status changed from new to assigned.
  • needs_better_patch changed.
  • needs_tests changed.
  • milestone set to post-1.0.
  • owner changed from nobody to anonymous.
  • needs_docs changed.

09/17/08 15:03:31 changed by daonb <bennydaon@gmail.com>

  • attachment 8501v1.diff added.

09/17/08 15:12:02 changed by daonb <bennydaon@gmail.com>

  • status changed from assigned to new.
  • needs_docs set to 1.
  • has_patch set to 1.

Patch 8501v1.diff supports passing the result of a urlopen as the content parameter for the save method of FileField? and ImageField?. Here's the included testing code:

>>> obj5 = Storage()
>>> image_data_len = len(urlopen('http://media.djangoproject.com/img/badges/djangosite80x15.gif').read())
>>> obj5.image.save('django_logo.gif',urlopen('http://media.djangoproject.com/img/badges/djangosite80x15.gif'))
>>> obj5.image
<ImageFieldFile: tests/django_logo.gif>
>>> image_data_len == obj5.image.size
True
>>> obj5.image.width
80L
>>> obj5.image.height
15L

10/02/08 18:39:43 changed by SmileyChris

Interesting. It seems this is more useful than just for urlopen - it allows saving file objects (and stringio streams) directly too. In fact, I'd write urlopen off as a side-effect and rejigg the patch tests to just use open()

10/14/08 22:38:17 changed by SmileyChris

  • needs_better_patch set to 1.
  • summary changed from It is not possible to save file opened by urllib2.urlopen to file storage should allow saving of basic file-like objects.
  • stage changed from Unreviewed to Accepted.

02/25/09 13:51:44 changed by

  • milestone deleted.

Milestone post-1.0 deleted


Add/Change #8501 (file storage should allow saving of basic file-like objects)




Change Properties
Action