74 lines
2.5 KiB
Python
74 lines
2.5 KiB
Python
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
|
|
from django.db.models.signals import post_save
|
|
from django.dispatch import receiver
|
|
from django.templatetags.static import static
|
|
from django.urls import reverse
|
|
from django.utils.text import slugify
|
|
|
|
from config.cache_durations import *
|
|
from gallery.cache import cached_or_set
|
|
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")
|
|
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):
|
|
"""
|
|
Returns the number of photos in the album.
|
|
Result is cached for PHOTO_COUNT_DURATION.
|
|
"""
|
|
key = self._cache_key('photo_count')
|
|
return cached_or_set(
|
|
key,
|
|
ALBUM_PHOTO_DURATION,
|
|
lambda: self.photos.count()
|
|
)
|
|
|
|
@property
|
|
def photos_views(self):
|
|
"""
|
|
Returns the total number of views for all photos in the album.
|
|
Result is cached for PHOTO_VIEWS_DURATION.
|
|
"""
|
|
key = self._cache_key('photo_views')
|
|
return cached_or_set(
|
|
key,
|
|
ALBUM_PHOTO_VIEWS_DURATION,
|
|
lambda: self.photos.aggregate(total_views=Sum('views'))['total_views'] or 0
|
|
)
|
|
|
|
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"
|
|
ordering = ('album_date',)
|
|
|
|
def __str__(self):
|
|
return '{}'.format(self.name)
|
|
|
|
|
|
@receiver(post_save, sender=Album)
|
|
def delete_album_cover_cache(sender, instance, **kwargs):
|
|
cache.delete(f'photo_md_image_data_{instance.pk}')
|