Add views count

This commit is contained in:
Nyymix 2025-01-13 22:49:12 +02:00
parent e19776aae3
commit dee6dcef3b
4 changed files with 29 additions and 10 deletions

View file

@ -1,4 +1,4 @@
# Generated by Django 5.1.4 on 2025-01-09 18:18 # Generated by Django 5.1.4 on 2025-01-13 20:45
import datetime import datetime
import django.db.models.deletion import django.db.models.deletion
@ -63,6 +63,7 @@ class Migration(migrations.Migration):
('exif', models.JSONField(blank=True, editable=False, null=True, verbose_name='Exif Metadata')), ('exif', models.JSONField(blank=True, editable=False, null=True, verbose_name='Exif Metadata')),
('is_favorite', models.BooleanField(default=False, verbose_name='Is Favorite')), ('is_favorite', models.BooleanField(default=False, verbose_name='Is Favorite')),
('views', models.PositiveIntegerField(default=0, verbose_name='Views')), ('views', models.PositiveIntegerField(default=0, verbose_name='Views')),
('likes', models.PositiveIntegerField(default=0, verbose_name='Likes')),
('album', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='photos', to='gallery.album', verbose_name='Album')), ('album', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='photos', to='gallery.album', verbose_name='Album')),
], ],
), ),

View file

@ -33,6 +33,15 @@ class Photo(models.Model):
exif = models.JSONField(blank=True, null=True, editable=False, verbose_name="Exif Metadata") exif = models.JSONField(blank=True, null=True, editable=False, verbose_name="Exif Metadata")
is_favorite = models.BooleanField(default=False, verbose_name="Is Favorite") is_favorite = models.BooleanField(default=False, verbose_name="Is Favorite")
views = models.PositiveIntegerField(default=0, verbose_name="Views") views = models.PositiveIntegerField(default=0, verbose_name="Views")
likes = models.PositiveIntegerField(default=0, verbose_name="Likes")
def add_like(self):
self.likes += 1
self.save()
def add_view(self):
self.views += 1
self.save()
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
self.exif_data = Exif(self.photo.path) self.exif_data = Exif(self.photo.path)

View file

@ -15,6 +15,7 @@
{% block content %} {% block content %}
<h1>{{ photo.slug }}</h1> <h1>{{ photo.slug }}</h1>
<p> Views: {{ photo.views }}</p>
<img src="{{ photo.photo.url }}" alt="{{ photo.slug }}" class="height='100%'" /> <img src="{{ photo.photo.url }}" alt="{{ photo.slug }}" class="height='100%'" />
{% endblock %} {% endblock %}

View file

@ -41,15 +41,23 @@ class AlbumDetail(DetailView):
return context return context
class PhotoDetail(DetailView):
model = Photo
template_name = 'gallery/photo_detail.html'
def get_object(self, queryset=None):
return get_object_or_404(Photo, slug=self.kwargs.get('photo_slug'))
class PhotosList(ListView): class PhotosList(ListView):
model = Photo model = Photo
paginate_by = 30 paginate_by = 30
ordering = ['-taken_at'] ordering = ['-taken_at']
class PhotoDetail(DetailView):
model = Photo
slug_url_kwarg = 'photo_slug'
template_name = 'gallery/photo_detail.html'
def get_object(self, queryset=None):
photo = get_object_or_404(Photo, slug=self.kwargs.get(self.slug_url_kwarg))
photo_key = f'photo_{photo.slug}'
if photo_key not in self.request.session:
photo.add_view()
self.request.session[photo_key] = 0
return photo