summaryrefslogtreecommitdiff
path: root/app/photos/models.py
diff options
context:
space:
mode:
authorluxagraf <sng@luxagraf.net>2016-11-01 14:42:20 -0400
committerluxagraf <sng@luxagraf.net>2016-11-01 14:42:20 -0400
commit541a4616c06449311cae1a793fef11bfa1ca7ace (patch)
tree498404416935822a09de181d6c82f24d79916468 /app/photos/models.py
parentd061b4393663ddf81c281f51fef63b5d11beb0df (diff)
widened site, created new photo layouts and prepped homepage redesign
Diffstat (limited to 'app/photos/models.py')
-rw-r--r--app/photos/models.py50
1 files changed, 39 insertions, 11 deletions
diff --git a/app/photos/models.py b/app/photos/models.py
index 6940110..4207ad4 100644
--- a/app/photos/models.py
+++ b/app/photos/models.py
@@ -1,10 +1,13 @@
import os.path
+import io
import datetime
from PIL import Image
+from django.core.exceptions import ValidationError
from django.contrib.gis.db import models
from django.contrib.sitemaps import Sitemap
from django.utils.encoding import force_text
+from django.utils.text import slugify
from django.conf import settings
from taggit.managers import TaggableManager
@@ -24,15 +27,20 @@ def get_upload_path(self, filename):
class LuxImageSize(models.Model):
- width = models.IntegerField()
+ name = models.CharField(null=True, blank=True, max_length=30)
+ width = models.IntegerField(null=True, blank=True)
+ height = models.IntegerField(null=True, blank=True)
quality = models.IntegerField()
class Meta:
verbose_name_plural = 'Image Sizes'
def __str__(self):
- return str(self.width)
-
+ if self.width:
+ size = self.width
+ if self.height:
+ size = self.height
+ return "%s - %s" %(self.name, str(size))
class LuxImage(models.Model):
image = models.FileField(blank=True, null=True, upload_to=get_upload_path)
@@ -75,8 +83,9 @@ class LuxImage(models.Model):
def get_admin_image(self):
for size in self.sizes.all():
- if size.width <= 800:
- return self.get_image_by_size(size)
+ if size.width and size.width <= 820 or size.height and size.height<=800:
+ print("Size name" +size.name)
+ return self.get_image_by_size(size.name)
def get_largest_image(self):
t = []
@@ -139,12 +148,21 @@ class LuxImage(models.Model):
n = self.get_next_by_pub_date()
return "/admin/photos/luximage/%s/change/" % n.pk
+ @property
+ def is_portait(self):
+ if int(self.height) > int(self.width):
+ return True
+ else:
+ return False
@receiver(post_save, sender=LuxImage)
def post_save_events(sender, update_fields, created, instance, **kwargs):
if instance.exif_raw == '':
filename, file_extension = os.path.splitext(instance.image.path)
if file_extension != ".mp4":
+ img = Image.open(instance.image.path)
+ instance.height = img.height
+ instance.width = img.width
instance = readexif(instance)
post_save.disconnect(post_save_events, sender=LuxImage)
instance.save()
@@ -159,16 +177,24 @@ def update_photo_sizes(sender, instance, **kwargs):
img = Image.open(instance.image.path)
resize_image(img, 160, None, 78, base_path, "%s_tn.%s" % (instance.get_image_name(), instance.get_image_ext()))
for size in instance.sizes.all():
- if img.size[0] > img.size[1]:
+ if size.width:
+ print("Image width is:"+str(img.width))
try:
- resize_image(img, size.width, None, size.quality, base_path, "%s_%s.%s" % (instance.get_image_name(), size.width, instance.get_image_ext()))
+ if size.width <= img.width:
+ resize_image(img, size.width, None, size.quality, base_path, "%s_%s.%s" % (instance.get_image_name(), slugify(size.name), instance.get_image_ext()))
+ 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)
m2m_changed.connect(update_photo_sizes, sender=LuxImage.sizes.through)
- if img.size[1] > img.size[0]:
+ if size.height:
try:
- resize_image(img, None, size.width, size.quality, base_path, "%s_%s.%s" % (instance.get_image_name(), size.width, instance.get_image_ext()))
+ if size.height <= img.height:
+ resize_image(img, None, size.height, size.quality, base_path, "%s_%s.%s" % (instance.get_image_name(), slugify(size.name), instance.get_image_ext()))
+
+ else:
+ pass
except ImageSizeError:
m2m_changed.disconnect(update_photo_sizes, sender=LuxImage.sizes.through)
instance.sizes.remove(size)
@@ -205,10 +231,12 @@ class LuxGallery(models.Model):
return "/photos/galleries/private/%s" % (self.slug)
def latitude(self):
- return self.point.y
+ if self.point:
+ return self.point.y
def longitude(self):
- return self.point.x
+ if self.point:
+ return self.point.x
def thumbs(self):
lst = [x.image.name for x in self.images.all()]