muistox/gallery/models/photo.py

60 lines
2.6 KiB
Python
Raw Normal View History

2025-01-05 17:26:09 +02:00
import os
2025-01-09 20:18:43 +02:00
from datetime import datetime
2024-12-29 23:25:03 +02:00
2024-12-28 12:21:29 +02:00
from django.db import models
2025-01-04 20:04:20 +02:00
from django.urls import reverse
2025-01-05 17:26:09 +02:00
from imagekit.models import ImageSpecField
from imagekit.processors import ResizeToFill
from gallery.exif import Exif
2025-01-09 20:18:43 +02:00
from gallery.models import Album
2025-01-04 20:04:20 +02:00
2025-01-05 17:26:09 +02:00
class Photo(models.Model):
def _get_upload_path(instance, filename):
return os.path.join('albums', str(instance.album.slug), filename)
2025-01-09 15:07:32 +02:00
def _generate_unique_slug(self, datetime_taken=datetime.now()):
slug = int(datetime_taken.strftime('%y%m%d%H%M%S'))
2025-01-05 17:26:09 +02:00
while Photo.objects.filter(album=self.album, slug=slug).exists():
2025-01-09 15:07:32 +02:00
slug += 1
return str(slug)
2025-01-05 17:26:09 +02:00
album = models.ForeignKey(Album, on_delete=models.CASCADE, related_name='photos', verbose_name="Album")
slug = models.CharField(max_length=15, editable=False, verbose_name="Photo Slug")
photo = models.ImageField(upload_to=_get_upload_path, height_field='height', width_field='width', verbose_name="Photo")
2025-01-13 21:09:29 +02:00
photo_sm = ImageSpecField(source='photo', processors=[ResizeToFill(320, 320)], format='JPEG', options={'quality': 70})
photo_md = ImageSpecField(source='photo', processors=[ResizeToFill(720, 720)], format='JPEG', options={'quality': 80})
photo_bg = ImageSpecField(source='photo', processors=[ResizeToFill(1920, 1920)], format='JPEG', options={'quality': 90})
2025-01-05 17:26:09 +02:00
width = models.PositiveIntegerField(default=0, editable=False, verbose_name="Photo Width")
height = models.PositiveIntegerField(default=0, editable=False, verbose_name="Photo Height")
taken_at = models.DateTimeField(blank=True, null=True, editable=False, verbose_name="Taken at")
exif = models.JSONField(blank=True, null=True, editable=False, verbose_name="Exif Metadata")
2025-01-09 20:18:43 +02:00
is_favorite = models.BooleanField(default=False, verbose_name="Is Favorite")
views = models.PositiveIntegerField(default=0, verbose_name="Views")
2025-01-13 22:49:12 +02:00
likes = models.PositiveIntegerField(default=0, verbose_name="Likes")
def add_like(self):
self.likes += 1
self.save()
def add_view(self):
self.views += 1
self.save()
2025-01-05 17:26:09 +02:00
def save(self, *args, **kwargs):
2025-01-13 21:09:29 +02:00
self.exif_data = Exif(self.photo.path)
2025-01-05 17:26:09 +02:00
datetime_taken = getattr(self.exif_data, 'datetimeoriginal', datetime.now)()
self.slug = self.slug or self._generate_unique_slug(datetime_taken)
self.taken_at = self.taken_at or datetime_taken
self.exif = getattr(self.exif_data, 'data', None)
super().save(*args, **kwargs)
def get_absolute_url(self):
return reverse('gallery:photo_url', kwargs={'album_path': self.album.slug, 'photo_slug': self.slug})
def __str__(self):
return f'{self.slug} ({self.album.name})'
2025-01-13 21:09:29 +02:00