muistox/gallery/models/photo.py

73 lines
3.2 KiB
Python
Raw Permalink 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 ResizeToFit
2025-01-05 17:26:09 +02:00
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")
photo_sm = ImageSpecField(source='photo', processors=[ResizeToFit(320, 320)], format='JPEG', options={'quality': 70})
photo_md = ImageSpecField(source='photo', processors=[ResizeToFit(720, 720)], format='JPEG', options={'quality': 80})
photo_bg = ImageSpecField(source='photo', processors=[ResizeToFit(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")
@property
def orientation(self):
return "Portrait" if self.height > self.width else "Landscape"
2025-01-13 22:49:12 +02:00
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
2025-01-15 20:31:41 +02:00
def get_next(self):
return self.__class__.objects.filter(taken_at__gt=self.taken_at, album=self.album.id).order_by('taken_at').first()
def get_prev(self):
return self.__class__.objects.filter(taken_at__lt=self.taken_at, album=self.album.id).order_by('-taken_at').first()
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):
2025-01-15 09:48:34 +02:00
return reverse('gallery:photo_url', kwargs={'album_slug': self.album.slug, 'photo_slug': self.slug})
class Meta:
verbose_name_plural = "Photos"
ordering = ('-taken_at',)
2025-01-05 17:26:09 +02:00
def __str__(self):
return f'{self.slug} ({self.orientation}) {self.is_favorite}'