muistox/gallery/models/album.py

52 lines
1.8 KiB
Python
Raw Permalink Normal View History

2025-01-09 20:18:43 +02:00
from datetime import datetime
2025-01-18 09:40:47 +02:00
from django.core.exceptions import ValidationError
2025-01-09 20:18:43 +02:00
from django.db import models
2025-01-15 20:31:41 +02:00
from django.templatetags.static import static
2025-01-09 20:18:43 +02:00
from django.urls import reverse
from django.utils.text import slugify
from gallery.models.location import Location
class Album(models.Model):
name = models.CharField(max_length=150, unique=True, verbose_name="Album")
slug = models.SlugField(max_length=150, unique=True, verbose_name="Slug")
location = models.ForeignKey(Location, blank=True, null=True, on_delete=models.SET_NULL, related_name='albums', verbose_name="Location")
album_date = models.DateField(default=datetime.now, verbose_name="Album Date")
2025-01-15 20:31:41 +02:00
cover = models.ForeignKey("Photo", blank=True, null=True, on_delete=models.SET_NULL, related_name='cover_to', verbose_name="Album cover")
2025-01-09 20:18:43 +02:00
is_public = models.BooleanField(default=False, verbose_name="Published")
2025-01-13 21:09:29 +02:00
@property
def photos_in_album(self):
return self.photos.count()
2025-01-19 21:30:23 +02:00
@property
def photos_views(self):
total_views = sum(self.photos.views())
return total_views
2025-01-15 20:31:41 +02:00
@property
def cover_url(self):
if self.cover:
2025-01-19 21:30:23 +02:00
return self.cover.photo_md.url
2025-01-15 20:31:41 +02:00
random_cover = self.photos.order_by('-width').first()
if random_cover:
2025-01-19 21:30:23 +02:00
return random_cover.photo_md.url
2025-01-15 20:31:41 +02:00
return static('img/placeholder.png')
2025-01-13 21:09:29 +02:00
2025-01-09 20:18:43 +02:00
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.name)
super(Album, self).save(*args, **kwargs)
def get_absolute_url(self):
return reverse('gallery:album_url', kwargs={'album_slug': self.slug})
class Meta:
verbose_name_plural = "Albums"
2025-01-18 09:40:47 +02:00
ordering = ('album_date',)
2025-01-09 20:18:43 +02:00
def __str__(self):
return '{}'.format(self.name)