diff options
author | luxagraf <sng@luxagraf.net> | 2020-12-02 14:34:15 -0500 |
---|---|---|
committer | luxagraf <sng@luxagraf.net> | 2020-12-02 14:34:15 -0500 |
commit | 87f692178a6e30719c564076f00c206642f36ce6 (patch) | |
tree | fe554090adc12aff3ca404cbee7a1e82972796e8 /app/media | |
parent | 14487689c2f968346fc4ca27d0d80637ec8422ba (diff) |
fixed some bugs in templates and rendering
Diffstat (limited to 'app/media')
-rw-r--r-- | app/media/models.py | 33 |
1 files changed, 17 insertions, 16 deletions
diff --git a/app/media/models.py b/app/media/models.py index 883d4df..bc27758 100644 --- a/app/media/models.py +++ b/app/media/models.py @@ -148,16 +148,15 @@ class LuxImage(models.Model): return self.image.url else: luximagesize = LuxImageSize.objects.get(name=size) - if luximagesize not in self.get_sizes: - print("new size is "+luximagesize.name) - self.sizes.add(luximagesize) + #if luximagesize not in self.get_sizes: + #self.sizes.add(luximagesize) return "%s%s/%s_%s.%s" % (settings.IMAGES_URL, self.pub_date.strftime("%Y"), self.image_name, luximagesize.slug, self.image_ext) def get_image_path_by_size(self, size="original"): if size == "original": return self.image.path else: - luximagesize = LuxImageSize.objects.get(name=size) + luximagesize = LuxImageSize.objects.get(slug=size) return "%s%s/%s_%s.%s" % (settings.IMAGES_ROOT, self.pub_date.strftime("%Y"), self.image_name, luximagesize.slug, self.image_ext) def get_thumbnail_url(self): @@ -167,7 +166,7 @@ class LuxImage(models.Model): return format_html('<a href="%s"><img src="%s"></a>' % (self.get_image_url_by_size(), self.get_image_url_by_size("admin-thumbnail"))) admin_thumbnail.short_description = 'Thumbnail' - @cached_property + @property def get_sizes(self): return self.sizes_cache.split(",") @@ -202,7 +201,7 @@ class LuxImage(models.Model): def save(self, *args, **kwargs): created = self.pk is None if created: - self.sizes.add(LuxImageSize.objects.get(name="admin-thumbnail")) + self.sizes.add(LuxImageSize.objects.get(name="tn")) img = Image.open(self.image.path) self.height = img.height self.width = img.width @@ -346,32 +345,34 @@ 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()) instance.save() - for size in instance.sizes.all(): + print(instance.get_sizes) + 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.name)) + 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 - if size.width: + new_size = LuxImageSize.objects.get(slug=size) + if new_size.width: img = Image.open(instance.image.path) try: - if size.width <= img.width: - resize_image(img, size.width, None, size.quality, instance.get_image_path_by_size(size.name)) + 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(size) + instance.sizes.remove(new_size) m2m_changed.connect(update_photo_sizes, sender=LuxImage.sizes.through) - if size.height: + if new_size.height: try: - if size.height <= img.height: - resize_image(img, None, size.height, size.quality, instance.get_image_path_by_size(size.name)) + 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(size) + 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 |