django-imagekit icon indicating copy to clipboard operation
django-imagekit copied to clipboard

Add SafeImageSpecField

Open voronind opened this issue 11 years ago • 2 comments

Add SafeImageSpecField that will not raise exception if source file is absent.

voronind avatar Jun 28 '14 17:06 voronind

Can you show me an example of code that's raising an exception?

matthewwithanm avatar Sep 23 '14 22:09 matthewwithanm

Use objects inherit for that:

class SafeProcessedImageField(ProcessedImageField):
    def clean(self, data, initial=None):
        data = super(ProcessedImageField, self).clean(data, initial)

        if data and data != initial:
            spec = self.get_spec(source=data)
            try:
                data = generate(spec)
            except (IndexError, IOError, ValueError, OverflowError) as e:
                pass

        return data

And use it your forms:

class PhotoForm(forms.Form):
    photo = SafeProcessedImageField(spec_id='model:photo')

nex2hex avatar Dec 25 '14 12:12 nex2hex