diff options
| -rw-r--r-- | ishtar_common/models.py | 72 | 
1 files changed, 39 insertions, 33 deletions
| diff --git a/ishtar_common/models.py b/ishtar_common/models.py index fa1556658..f482cf61d 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -878,41 +878,47 @@ class ImageModel(models.Model):              super(ImageModel, self).save(*args, **kwargs)              return          # manage images -        if self.has_changed('image') and self.image: -            # convert to jpg -            filename = os.path.splitext(os.path.split(self.image.name)[-1])[0] -            old_path = self.image.path -            filename = "%s.jpg" % filename -            try: -                image = Image.open(self.image.file) -                # convert to RGB -                if image.mode not in ('L', 'RGB'): -                    image = image.convert('RGB') +        if not self.has_changed('image'): +            return super(ImageModel, self).save(*args, **kwargs) +        if not self.image: +            self.thumbnail = None +            return super(ImageModel, self).save(*args, **kwargs) + +        # # generate thumbnail +        # convert to jpg +        filename = os.path.splitext(os.path.split(self.image.name)[-1])[0] +        old_path = self.image.path +        filename = "%s.jpg" % filename +        try: +            image = Image.open(self.image.file) +            # convert to RGB +            if image.mode not in ('L', 'RGB'): +                image = image.convert('RGB') -                # resize if necessary -                self.image.save(filename, -                                self.create_thumb(image, self.IMAGE_MAX_SIZE), -                                save=False) +            # resize if necessary +            self.image.save(filename, +                            self.create_thumb(image, self.IMAGE_MAX_SIZE), +                            save=False) -                if old_path != self.image.path: -                    try: -                        os.remove(old_path) -                    except OSError: -                        # already clean -                        pass - -                # save the thumbnail -                splited = filename.split('.') -                thumb_filename = u"{}-thumb.{}".format( -                    u".".join(splited[:-1]), splited[-1] -                ) -                self.thumbnail.save( -                    thumb_filename, -                    self.create_thumb(image, self.THUMB_MAX_SIZE), -                    save=False) -            except IOError: -                pass -        super(ImageModel, self).save(*args, **kwargs) +            if old_path != self.image.path: +                try: +                    os.remove(old_path) +                except OSError: +                    # already clean +                    pass + +            # save the thumbnail +            splited = filename.split('.') +            thumb_filename = u"{}-thumb.{}".format( +                u".".join(splited[:-1]), splited[-1] +            ) +            self.thumbnail.save( +                thumb_filename, +                self.create_thumb(image, self.THUMB_MAX_SIZE), +                save=False) +        except IOError: +            pass +        return super(ImageModel, self).save(*args, **kwargs)  class HistoryError(Exception): | 
