diff --git a/gallery/models/album.py b/gallery/models/album.py index b6db339..94766c3 100644 --- a/gallery/models/album.py +++ b/gallery/models/album.py @@ -1,6 +1,7 @@ from datetime import datetime from math import floor +from django.core.cache import cache from django.core.exceptions import ValidationError from django.db import models from django.db.models import Sum @@ -19,47 +20,30 @@ class Album(models.Model): cover = models.ForeignKey("Photo", blank=True, null=True, on_delete=models.SET_NULL, related_name='cover_to', verbose_name="Album cover") is_public = models.BooleanField(default=False, verbose_name="Published") + def _cache_key(self, suffix): + return f'album_{self.pk}_{suffix}' + @property def photos_in_album(self): - return self.photos.count() + key = self._cache_key('photo_count') + count = cache.get(key) + + if count is None: + count = self.photos.count() + cache.set(key, count, 60 * 60 * 24) # Cache 24 h + + return count @property def photos_views(self): - return self.photos.aggregate(total_views=Sum('views'))['total_views'] or 0 + key = self._cache_key('photo_views') + views = cache.get(key) + if views is None: + views = self.photos.aggregate(total_views=Sum('views'))['total_views'] or 0 + cache.set(key, views, 60 * 5) # Cache 5 min - @property - def cover_image_data(self): - def compute_size(photo, max_w=720, max_h=720): - if not photo.width or not photo.height: - return (None, None) - aspect = photo.aspect_ratio - if photo.width > photo.height: - w = min(photo.width, max_w) - h = floor(w / aspect) - else: - h = min(photo.height, max_h) - w = floor(h * aspect) - return (w, h) - - photo = self.cover or self.photos.order_by('-width').first() - if photo: - url = photo.photo_md.url - w, h = compute_size(photo) - return { - "url": url, - "width": w, - "height": h, - "aspect_ratio": round(photo.aspect_ratio, 3) - } - - return { - "url": static('img/placeholder.png'), - "width": 1200, - "height": 800, - "aspect_ratio": 1.5 - } - + return views def save(self, *args, **kwargs): if not self.slug: diff --git a/gallery/models/photo.py b/gallery/models/photo.py index c65209f..5db49d1 100644 --- a/gallery/models/photo.py +++ b/gallery/models/photo.py @@ -1,6 +1,7 @@ import os from datetime import datetime +from django.core.cache import cache from django.db import models from django.db.models.signals import post_save from django.dispatch import receiver @@ -47,7 +48,6 @@ class Photo(models.Model): self.slug = os.path.basename(self.photo.name) super().save(*args, **kwargs) - def add_like(self): self.likes += 1 self.save(update_fields=['likes']) @@ -58,7 +58,7 @@ class Photo(models.Model): def get_next(self): return self.__class__.objects.filter(taken_at__gt=self.taken_at, album=self.album).order_by('taken_at').first() - + def get_prev(self): return self.__class__.objects.filter(taken_at__lt=self.taken_at, album=self.album).order_by('-taken_at').first() @@ -79,13 +79,19 @@ class Photo(models.Model): -def create_photo(sender, instance, created, **kwargs): - if created: - """ Add exif metadata """ +@receiver(post_save, sender=Photo) +def handle_photo_creation(sender, instance, created, **kwargs): + if not created: + return + + # Lisää EXIF metadata + if instance.photo and instance.photo.path: exif = Exif(instance.photo.path) instance.exif = exif.data - """ Update taken datetime """ instance.taken_at = exif.datetimeoriginal('Europe/Helsinki') - instance.save() + instance.save(update_fields=['exif', 'taken_at']) -post_save.connect(create_photo, sender=Photo) \ No newline at end of file + # Aseta cover, jos albumilla ei ole vielä sellaista + if instance.album and not instance.album.cover: + instance.album.cover = instance + instance.album.save(update_fields=['cover']) \ No newline at end of file diff --git a/gallery/templates/gallery/album_list.html b/gallery/templates/gallery/album_list.html index 260f6c3..2c47d64 100644 --- a/gallery/templates/gallery/album_list.html +++ b/gallery/templates/gallery/album_list.html @@ -1,5 +1,6 @@ {% extends "base.html" %} {% load static %} +{% load image_tags %} {% block title %} Albums {% endblock %} @@ -29,13 +30,16 @@
diff --git a/gallery/templates/gallery/search.html b/gallery/templates/gallery/search.html index df64ade..1026350 100644 --- a/gallery/templates/gallery/search.html +++ b/gallery/templates/gallery/search.html @@ -1,5 +1,6 @@ {% extends "base.html" %} {% load static %} +{% load image_tags %} {% block title %} Search {% endblock %} @@ -21,6 +22,7 @@Not found: "{{ query }}".
{% endif %} +