diff options
author | luxagraf <sng@luxagraf.net> | 2020-12-04 11:29:57 -0500 |
---|---|---|
committer | luxagraf <sng@luxagraf.net> | 2020-12-04 11:29:57 -0500 |
commit | edf63a6a55897b7df19b216de1db6d114a6c15c2 (patch) | |
tree | 57f1d9f1fbb7b1152b74687ac790200d0ae70c19 /app/media | |
parent | e708681c15708c65a5b6ef4b811b0fde937ded4c (diff) |
fixed a bug in image save signal
Diffstat (limited to 'app/media')
-rw-r--r-- | app/media/models.py | 72 |
1 files changed, 38 insertions, 34 deletions
diff --git a/app/media/models.py b/app/media/models.py index 1c2037a..f2a74bc 100644 --- a/app/media/models.py +++ b/app/media/models.py @@ -166,7 +166,7 @@ class LuxImage(models.Model): return self.get_image_url_by_size("tn") def admin_thumbnail(self): - return format_html('<a href="%s"><img src="%s"></a>' % (self.get_image_url_by_size(), self.get_image_url_by_size("admin-thumbnail"))) + return format_html('<a href="%s"><img src="%s"></a>' % (self.get_image_url_by_size(), self.get_image_url_by_size("tn"))) admin_thumbnail.short_description = 'Thumbnail' @property @@ -349,38 +349,42 @@ class LuxAudio(models.Model): @receiver(m2m_changed, sender=LuxImage.sizes.through) def update_photo_sizes(sender, instance, **kwargs): # update the local cache of sizes - instance.sizes_cache = ",".join(s.slug for s in instance.sizes.all()) - for size in instance.get_sizes: - # check each size and see if there's an image there already - my_file = Path(instance.get_image_path_by_size(size)) - if not my_file.is_file(): - print("creating new image file") - #file doesn't exist, so create it - new_size = LuxImageSize.objects.get(slug=size) - if new_size.width: - img = Image.open(instance.image.path) - try: - if new_size.width <= img.width: - resize_image(img, new_size.width, None, new_size.quality, instance.get_image_path_by_size(size)) - else: - raise ValidationError({'items': ["Size is larger than source image"]}) - except ImageSizeError: - m2m_changed.disconnect(update_photo_sizes, sender=LuxImage.sizes.through) - instance.sizes.remove(new_size) - m2m_changed.connect(update_photo_sizes, sender=LuxImage.sizes.through) - if new_size.height: - try: - if new_size.height <= img.height: - resize_image(img, None, new_size.height, new_size.quality, instance.get_image_path_by_size(size)) - else: - pass - except ImageSizeError: - m2m_changed.disconnect(update_photo_sizes, sender=LuxImage.sizes.through) - instance.sizes.remove(new_size) - m2m_changed.connect(update_photo_sizes, sender=LuxImage.sizes.through) - else: - # file exists, might add something here to force it to do the above when I want - print("file %s exists" % size) - pass + sizes = instance.sizes.all() + if sizes: + instance.sizes_cache = ",".join(s.slug for s in sizes) + print(instance.sizes.all()) + for size in instance.get_sizes: + print("SIZE is: %s" % size) + # check each size and see if there's an image there already + my_file = Path(instance.get_image_path_by_size(size)) + if not my_file.is_file(): + #file doesn't exist, so create it + new_size = LuxImageSize.objects.get(slug=size) + if new_size.width: + img = Image.open(instance.image.path) + try: + if new_size.width <= img.width: + resize_image(img, new_size.width, None, new_size.quality, instance.get_image_path_by_size(size)) + else: + raise ValidationError({'items': ["Size is larger than source image"]}) + except ImageSizeError: + m2m_changed.disconnect(update_photo_sizes, sender=LuxImage.sizes.through) + instance.sizes.remove(new_size) + m2m_changed.connect(update_photo_sizes, sender=LuxImage.sizes.through) + if new_size.height: + img = Image.open(instance.image.path) + try: + if new_size.height <= img.height: + resize_image(img, None, new_size.height, new_size.quality, instance.get_image_path_by_size(size)) + else: + pass + except ImageSizeError: + m2m_changed.disconnect(update_photo_sizes, sender=LuxImage.sizes.through) + instance.sizes.remove(new_size) + m2m_changed.connect(update_photo_sizes, sender=LuxImage.sizes.through) + else: + # file exists, might add something here to force it to do the above when I want + print("file %s exists" % size) + pass instance.save() |