diff options
Diffstat (limited to 'app/photos/models.py')
-rw-r--r-- | app/photos/models.py | 78 |
1 files changed, 73 insertions, 5 deletions
diff --git a/app/photos/models.py b/app/photos/models.py index 50ae43b..46c659e 100644 --- a/app/photos/models.py +++ b/app/photos/models.py @@ -10,14 +10,34 @@ from django.conf import settings from taggit.managers import TaggableManager from locations.models import Location, Region +from .utils import resize_image +from .readexif import readexif +from django.db.models.signals import post_save +from django.dispatch import receiver +from django.db.models.signals import m2m_changed + def get_upload_path(self, filename): - return "images/galleries/original/%s/%s" % (datetime.datetime.today().strftime("%Y"), filename) + return "images/original/%s/%s" % (datetime.datetime.today().strftime("%Y"), filename) + + +class LuxImageSize(models.Model): + width = models.IntegerField() + quality = models.IntegerField() + + class Meta: + verbose_name_plural = 'Image Sizes' + + def __str__(self): + return str(self.width) class LuxImage(models.Model): image = models.FileField(blank=True, null=True, upload_to=get_upload_path) title = models.CharField(null=True, blank=True, max_length=300) + alt = models.CharField(null=True, blank=True, max_length=300) + photo_credit_source = models.CharField(null=True, blank=True, max_length=300) + photo_credit_url = models.CharField(null=True, blank=True, max_length=300) caption = models.TextField(blank=True, null=True) pub_date = models.DateTimeField() exif_raw = models.TextField(blank=True, null=True) @@ -35,6 +55,7 @@ class LuxImage(models.Model): location = models.ForeignKey(Location, null=True, blank=True) is_public = models.BooleanField(default=True) is_video = models.BooleanField(default=False) + sizes = models.ManyToManyField(LuxImageSize) flickr_id = models.CharField(null=True, blank=True, max_length=80) class Meta: @@ -48,15 +69,27 @@ class LuxImage(models.Model): else: return "%s" % self.pk + def get_admin_image(self): + for size in self.sizes.all(): + if size.width <= 800: + return self.get_image_by_size(size) + + def get_image_name(self): - return self.image.url.split("galleries/original/")[1] + return self.image.url.split("original/")[1][5:-4] - def get_image_size(self, size="original"): + def get_image_ext(self): + return self.image.url[-3:] + + def get_image_by_size(self, size="original"): base = self.get_image_name() - return "%sgalleries/%s/%s" % (settings.IMAGES_URL, size, base) + if size == "original": + return "%soriginal/%s/%s.%s" % (settings.IMAGES_URL, self.pub_date.strftime("%Y"), base, self.get_image_ext()) + else: + return "%s%s/%s_%s.%s" % (settings.IMAGES_URL, self.pub_date.strftime("%Y"), base, size, self.get_image_ext()) def admin_thumbnail(self): - return force_text('<a href="%s"><img src="%s"></a>' % (self.get_image_size(), self.get_image_size("thumb"))) + return force_text('<a href="%s"><img src="%s"></a>' % (self.get_image_by_size(), self.get_image_by_size("tn"))) admin_thumbnail.allow_tags = True admin_thumbnail.short_description = 'Thumbnail' @@ -79,6 +112,25 @@ class LuxImage(models.Model): return "/admin/photos/luximage/%s/change/" % n.pk +@receiver(post_save, sender=LuxImage) +def post_save_events(sender, update_fields, created, instance, **kwargs): + instance = readexif(instance) + post_save.disconnect(post_save_events, sender=LuxImage) + instance.save() + post_save.connect(post_save_events, sender=LuxImage) + + +@receiver(m2m_changed, sender=LuxImage.sizes.through) +def update_photo_sizes(sender, instance, **kwargs): + print("hellow world") + base_path = "%s/%s/" % (settings.IMAGES_ROOT, instance.pub_date.strftime("%Y")) + img = Image.open(instance.image.path) + resize_image(img, 160, None, 65, base_path, "%s_tn.%s" % (instance.get_image_name(), instance.get_image_ext())) + for size in instance.sizes.all(): + print(size.width) + resize_image(img, size.width, None, size.quality, base_path, "%s_%s.%s" % (instance.get_image_name(), size.width, instance.get_image_ext())) + + class LuxGallery(models.Model): title = models.CharField(blank=True, max_length=300) description = models.TextField(blank=True, null=True) @@ -317,3 +369,19 @@ class PhotoGallerySitemap(Sitemap): def lastmod(self, obj): return obj.pub_date + + +def resize_luximage(self, image): + image.save() + img = Image.open(image.image.path) + base_path = "%s/galleries/" % settings.IMAGES_ROOT + if img.size[0] > img.size[1]: + resize_image(img, 2280, None, 65, base_path+'large/', image.get_image_name()) + resize_image(img, 1140, None, 72, base_path+'medium/', image.get_image_name()) + resize_image(img, 720, None, 68, base_path+'small/', image.get_image_name()) + if img.size[1] > img.size[0]: + resize_image(img, None, 1600, 65, base_path+'large/', image.get_image_name()) + resize_image(img, None, 800, 72, base_path+'medium/', image.get_image_name()) + resize_image(img, None, 460, 60, base_path+'small/', image.get_image_name()) + + resize_image(img, 160, None, 68, base_path+'thumb/', image.get_image_name()) |