--
Ticket URL: <https://code.djangoproject.com/ticket/18543>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* cc: niwi@… (added)
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
Comment:
I tried to reproduce the bug with this test:
{{{
#!python
@skipUnless(test_images, "PIL not installed")
def test_model_image_field(self):
from django.core.files.base import ContentFile
f = ContentFile(b'not an image')
with open(os.path.join(os.path.dirname(__file__), "test.png"), 'rb')
as fp:
image_data = fp.read()
instance = ImageFile()
instance.image.save("foo.png", SimpleUploadedFile("foo.png",
image_data))
instance2 = ImageFile()
instance2.image.save("foo.png", f)
}}}
And I get is this:
{{{
======================================================================
ERROR: test_model_image_field
(modeltests.model_forms.tests.OldFormForXTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/niwi/django/tests/modeltests/model_forms/tests.py", line
1250, in test_model_image_field
instance2.image.save("foo.png", f)
File "../django/db/models/fields/files.py", line 348, in save
super(ImageFieldFile, self).save(name, content, save)
File "../django/db/models/fields/files.py", line 89, in save
setattr(self.instance, self.field.name, self.name)
File "../django/db/models/fields/files.py", line 334, in __set__
self.field.update_dimension_fields(instance, force=True)
File "../django/db/models/fields/files.py", line 412, in
update_dimension_fields
width = file.width
File "../django/core/files/images.py", line 15, in _get_width
return self._get_image_dimensions()[0]
TypeError: 'NoneType' object has no attribute '__getitem__'
----------------------------------------------------------------------
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/18543#comment:1>
* stage: Unreviewed => Accepted
Comment:
Currently, the verification that the content is an image is done by the
`ImageField` form field (see its `to_python` method), not in the model
field.
Accepted on the base that we should at least:
* Improve the error message as shown in the previous comment.
* Clarify the documentation
Validating that the file is really an image file at the model level might
be something to evaluate, but with great care, because of the performance
issue involved by this operation. Opinions/design decision needed here.
--
Ticket URL: <https://code.djangoproject.com/ticket/18543#comment:2>
Comment (by Adam DePrince <deprince@…>):
What constitutes a valid image will depend on how the image is processed
--- Imagemagick, netpnm and PIL all disagree slightly on the definition of
"an image". We should also consider what it means to "validate" the
image --- basic magic number checking will catch the common cases where a
user uploads something decidedly not an image, like an mp3 file, but we
should stress in our documentation that just because the image passed our
"validation" does not mean what was uploaded is actually an image or even
safe to process.
What if ImageField accepted anything for which "file -b --mime-type"
responds with a match to "$image/" ?
--
Ticket URL: <https://code.djangoproject.com/ticket/18543#comment:3>
* cc: deprince@… (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/18543#comment:4>
* owner: nobody => deprince
Comment:
A quick survey shows that the python options for file seem to be lib magic
based. I don't think we really want to see yet another c dependencey in
django so I'll spend the rest of my djangocon 2012 sprint time banging out
a pure python port.
--
Ticket URL: <https://code.djangoproject.com/ticket/18543#comment:5>
* owner: deprince => (none)
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/18543#comment:6>
* status: assigned => new
--
Ticket URL: <https://code.djangoproject.com/ticket/18543#comment:7>