Specifically, I'd like to use this validation in the admin. Is the
best way to do this by overriding the save() method?

On May 12, 3:32 pm, Brandon Taylor <[EMAIL PROTECTED]> wrote:
> Hi everyone,
>
> I'm attempting to do some image validation. Here is my model:
>
> from django.db import models
>
> GROUP_CHOICES = (
>              ('Home Page', 'Home Page'),
>              ('Tenets Sidebar', 'Tenets Sidebar'),
>     )
>
> class Image(models.Model):
>     group = models.CharField(max_length=20, choices=GROUP_CHOICES)
>     name = models.CharField('Friendly Name', max_length=50)
>     image = models.ImageField(upload_to='images', help_text='Please
> use an image no wider than 692px x 238px for heroes.<br />226px wide
> for sidebar images.')
>     alt_text = models.CharField('Alt Text', max_length=100,
> help_text='Text representation of the image.')
>
>     def __unicode__(self):
>         return self.name
>
>     def thumbnail_display(self):
>         dimensions = [(self.get_image_width() / 4),
> (self.get_image_height() / 4)]
>         return '<img src="%s" width="%s" height="%s" alt="%s" />' %
> (self.get_image_url(), dimensions[0], dimensions[1], self.alt_text)
>     thumbnail_display.allow_tags=True
>
>     def clean_image(self):
>         if self.clean_data.get('image'):
>             image_data = self.clean_data['image']
>             if 'error' in image_data:
>                 raise forms.ValidationError(_('Please upload a valid
> image. The file you uploaded was either not an image or a corrupted
> image.'))
>                 content_type = image_data.get('content-type')
>                 if content_type:
>                     main,sub = content_type.split('/')
>                     if not (main == 'image' and sub in
> ['jpeg','jpg']):
>                         raise forms.ValidationError(_('JPEG images
> only please.'))
>
>                 size = len(image_data['content'])
>                 if size > 512:
>                     raise forms.ValidationError(_('Image is too big.
> 512k maximum filesize.'))
>
>                 width, height = image_data['dimensions']
>
>                 if self.group == 'Home Page':
>                     valid_dimensions = [692,400]
>                 elif self.group == 'Tenets Sidebar':
>                     valid_dimensions = [226,300]
>
>                 if width > valid_dimensions[0]:
>                     raise forms.ValidationError(_('Image max width is
> ' + valid_dimensions[0] + 'px.'))
>
>                 if height > valid_dimensions[1]:
>                     raise forms.ValidationError(_('Image max height is
> ' + valid_dimensions[1] + 'px.'))
>         return self.clean_data['image'].replace('\\', '/')
>
>     class Admin:
>         list_display = ('name','thumbnail_display')
>         list_filter = ('group',)
>
>     class Meta:
>         verbose_name_plural = 'Images'
>
> I tried uploading a large image, 800 x 800 pixels, which should have
> failed validation, but it didn't. What did I do wrong?
>
> Thanks,
> Brandon
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to