#16396: Validation Problem Reading Files When Using Custom Storage Module
---------------------------------------+-------------------------------
 Reporter:  dadealeus@…                |          Owner:  nobody
     Type:  Bug                        |         Status:  new
Milestone:                             |      Component:  Documentation
  Version:  1.3                        |       Severity:  Normal
 Keywords:  validation storage custom  |   Triage Stage:  Unreviewed
Has patch:  0                          |  Easy pickings:  0
    UI/UX:  0                          |
---------------------------------------+-------------------------------
 Using a custom storage module I wrote using the Rackspace CloudFiles API,
 if I perform any sort of reads on a file object during custom validation
 of uploaded files, I need to seek back the beginning of the file before I
 return it as cleaned_data.

 Example form class:

 {{{
 class UploadImageForm(forms.Form):
         """ Form to handle design uploads """
         design = forms.ImageField(error_messages={'required' : "Please
 select an image to upload."})

         def clean_design(self):

             from generic.media import utils

             # Verify uploaded image is an image
             data = self.cleaned_data['design']

             # Open the image so we can autocrop before checking dimensions
             image = PIL.open(StringIO(data.read()))

             # Autocrop the image and then check the dimensions
             image = utils.autoCrop(image)

             # Minimum image size
             min_image_size = 90

             # Verify the image size
             width, height = image.size
             width = float(width)
             height = float(height)
             ratio = height / width

             # Perform various validation checks... Example:
             if (ratio > 16) or (ratio < 0.0625):
                 raise forms.ValidationError("The uploaded image's aspect
 ratio is too extreme. Please use an image that looks more square.")

             return data
 }}}

 Upon trying to save an image with the cleaned data (while using my custom
 storage module) via a custom view, the file object has no data associated
 with it unless I seek back to 0 before returning the cleaned data.

 Please note that this only occurs when using my custom storage module and
 when performing custom validation on the uplodaded data. Using the native
 django storage, the exact same code functions without any issues.

 I would like to see a note added to the custom storage documentation that
 specifies something like:

 "If performing custom validation on a file object while using a custom
 storage module, be sure to seek back to zero before passing back the
 cleaned data as the file object is returned in its current state.

 Example:

 {{{
 class UploadImageForm(forms.Form):
         """ Form to handle image uploads """
         image = forms.ImageField(error_messages={'required' : "Please
 select an image to upload."})

         def clean_image(self):

             data = self.cleaned_data['image']
             stringIO = data.read()

             # Perform some custom validation

             # Reset the file object before returning it as cleaned data
             data.seek(0)
             return data
 }}}


 "

-- 
Ticket URL: <https://code.djangoproject.com/ticket/16396>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

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

Reply via email to