diff options
Diffstat (limited to 'app/jrnl/models.py')
-rw-r--r-- | app/jrnl/models.py | 31 |
1 files changed, 23 insertions, 8 deletions
diff --git a/app/jrnl/models.py b/app/jrnl/models.py index bcfd0b8..19f5417 100644 --- a/app/jrnl/models.py +++ b/app/jrnl/models.py @@ -1,6 +1,9 @@ import datetime import os import re +from PIL import Image +from django.db.models.signals import post_save +from django.dispatch import receiver from django.contrib.gis.db import models from django.utils.html import format_html @@ -18,7 +21,8 @@ from django.template.defaultfilters import slugify import markdown from bs4 import BeautifulSoup -from photos.models import PhotoGallery, LuxImage +from photos.models import PhotoGallery, LuxImage, LuxImageSize +from photos.utils import resize_image from locations.models import Location from sketches.models import Sketch from books.models import Book @@ -73,7 +77,7 @@ class Entry(models.Model): status = models.IntegerField(choices=PUB_STATUS, default=0) photo_gallery = models.ForeignKey(PhotoGallery, on_delete=models.CASCADE, blank=True, null=True, verbose_name='photo set') image = models.FileField(upload_to=get_upload_path, null=True, blank=True, help_text="should be 520 by 290") - thumbnail = models.FileField(upload_to=get_tn_path, null=True, blank=True, help_text="should be 160 wide") + #thumbnail = models.FileField(upload_to=get_tn_path, null=True, blank=True, help_text="should be 160 wide") meta_description = models.CharField(max_length=256, null=True, blank=True) TEMPLATES = ( (0, 'single'), @@ -108,7 +112,7 @@ class Entry(models.Model): return self.enable_comments and datetime.datetime.today() - datetime.timedelta(30) <= self.pub_date def get_thumbnail_url(self): - image_dir, img = self.thumbnail.url.split('post-thumbnail/')[1].split('/') + image_dir, img = self.image.url.split('post-thumbnail/')[1].split('/') return '%spost-thumbnail/%s/%s' % (settings.IMAGES_URL, image_dir, img) def get_image_url(self): @@ -175,8 +179,15 @@ class Entry(models.Model): except model.DoesNotExist: return '' - def save(self): - if self.pk: + def get_image_name(self): + return self.image.url.split("post-images/")[1][5:-4] + + def get_image_ext(self): + return self.image.url[-3:] + + def save(self, *args, **kwargs): + created = self.pk is None + if not created: md = render_images(self.body_markdown) self.body_html = markdown_to_html(md) self.has_video = parse_video(self.body_html) @@ -184,9 +195,13 @@ class Entry(models.Model): self.location = Location.objects.filter(geometry__contains=self.point).get() except Location.DoesNotExist: raise forms.ValidationError("There is no location associated with that point, add it: %sadmin/locations/location/add/" % (settings.BASE_URL)) - - super(Entry, self).save() - + old = type(self).objects.get(pk=self.pk) if self.pk else None + print("self.image.path: ", self.image.path) + if old and old.featured_image != self.featured_image or created: # Field has changed + s = LuxImageSize.objects.get(name="featured_jrnl") + self.featured_image.sizes.add(s) + self.featured_image.save() + super(Entry, self).save(*args, **kwargs) class HomepageCurrator(models.Model): |