Fix views & update templates

This commit is contained in:
Nyymix 2025-04-11 22:38:20 +03:00
parent 3029eea1a0
commit bd12a0abef
5 changed files with 24 additions and 6 deletions

View file

@ -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')},
),
]

View file

@ -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):

View file

@ -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}'

View file

@ -45,7 +45,8 @@
<a href="{{ album.get_absolute_url }}">{{ album }}</a>
</h3>
<p class="uk-text-small uk-margin-remove-top">
{{ album.album_date|date:"d.m.Y" }} &bull; {{ album.photos_in_album }} photos
{{ album.photos_in_album }} photos &bull;
{{ album.photos_views }} views
</p>
</div>

View file

@ -45,7 +45,8 @@
<a href="{{ album.get_absolute_url }}">{{ album }}</a>
</h3>
<p class="uk-text-small uk-margin-remove-top">
{{ album.album_date|date:"d.m.Y" }} &bull; {{ album.photos_in_album }} photos
{{ album.photos_in_album }} photos &bull;
{{ album.photos_views }} views
</p>
</div>