diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2018-02-27 15:31:44 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2018-02-27 15:32:09 +0100 |
commit | 96dab3e5402589d9537df776bbcd7df816ae54f2 (patch) | |
tree | 46c2b4423867df24d2077c9fc9bce000b67a7681 | |
parent | 20ddc8d20f94d2381447c29f204f5b48841b6980 (diff) | |
download | Ishtar-96dab3e5402589d9537df776bbcd7df816ae54f2.tar.bz2 Ishtar-96dab3e5402589d9537df776bbcd7df816ae54f2.zip |
Remove thumbnail when image is removed (refs #3737)
-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): |