diff --git a/gallery/migrations/0001_initial.py b/gallery/migrations/0001_initial.py index 9dbdc01..f5178c5 100644 --- a/gallery/migrations/0001_initial.py +++ b/gallery/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 5.1.7 on 2025-03-31 18:01 +# Generated by Django 5.1.7 on 2025-04-11 19:37 import datetime import django.db.models.deletion @@ -70,7 +70,6 @@ class Migration(migrations.Migration): options={ 'verbose_name_plural': 'Photos', 'ordering': ('-taken_at',), - 'unique_together': {('album', 'slug')}, }, ), migrations.AddField( @@ -89,4 +88,16 @@ class Migration(migrations.Migration): 'verbose_name_plural': 'Redirs', }, ), + migrations.AddIndex( + model_name='photo', + index=models.Index(fields=['views'], name='gallery_pho_views_cec6da_idx'), + ), + migrations.AddIndex( + model_name='photo', + index=models.Index(fields=['likes'], name='gallery_pho_likes_6955ed_idx'), + ), + migrations.AlterUniqueTogether( + name='photo', + unique_together={('album', 'slug')}, + ), ] diff --git a/gallery/models/album.py b/gallery/models/album.py index f7ab1a8..b6db339 100644 --- a/gallery/models/album.py +++ b/gallery/models/album.py @@ -3,6 +3,7 @@ from math import floor from django.core.exceptions import ValidationError from django.db import models +from django.db.models import Sum from django.templatetags.static import static from django.urls import reverse from django.utils.text import slugify @@ -24,8 +25,8 @@ class Album(models.Model): @property def photos_views(self): - total_views = sum(self.photos.views()) - return total_views + return self.photos.aggregate(total_views=Sum('views'))['total_views'] or 0 + @property def cover_image_data(self): diff --git a/gallery/models/photo.py b/gallery/models/photo.py index 8e5550d..c65209f 100644 --- a/gallery/models/photo.py +++ b/gallery/models/photo.py @@ -69,6 +69,10 @@ class Photo(models.Model): unique_together = ('album', "slug") verbose_name_plural = "Photos" ordering = ('-taken_at',) + indexes = [ + models.Index(fields=['views']), + models.Index(fields=['likes']), + ] def __str__(self): return f'{self.slug} ({self.orientation}) {self.is_favorite}' diff --git a/gallery/templates/gallery/album_list.html b/gallery/templates/gallery/album_list.html index c204fb6..260f6c3 100644 --- a/gallery/templates/gallery/album_list.html +++ b/gallery/templates/gallery/album_list.html @@ -45,7 +45,8 @@ {{ album }}
- {{ album.album_date|date:"d.m.Y" }} • {{ album.photos_in_album }} photos + {{ album.photos_in_album }} photos • + {{ album.photos_views }} views
diff --git a/gallery/templates/gallery/search.html b/gallery/templates/gallery/search.html index 530ff11..df64ade 100644 --- a/gallery/templates/gallery/search.html +++ b/gallery/templates/gallery/search.html @@ -45,7 +45,8 @@ {{ album }}- {{ album.album_date|date:"d.m.Y" }} • {{ album.photos_in_album }} photos + {{ album.photos_in_album }} photos • + {{ album.photos_views }} views