diff --git a/gallery/migrations/0001_initial.py b/gallery/migrations/0001_initial.py index d4fd931..e817847 100644 --- a/gallery/migrations/0001_initial.py +++ b/gallery/migrations/0001_initial.py @@ -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 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')), ('is_favorite', models.BooleanField(default=False, verbose_name='Is Favorite')), ('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')), ], ), diff --git a/gallery/models/photo.py b/gallery/models/photo.py index c6b27de..5f45b63 100644 --- a/gallery/models/photo.py +++ b/gallery/models/photo.py @@ -33,6 +33,15 @@ class Photo(models.Model): exif = models.JSONField(blank=True, null=True, editable=False, verbose_name="Exif Metadata") is_favorite = models.BooleanField(default=False, verbose_name="Is Favorite") 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): self.exif_data = Exif(self.photo.path) diff --git a/gallery/templates/gallery/photo_detail.html b/gallery/templates/gallery/photo_detail.html index 820c4ff..5a34f7a 100644 --- a/gallery/templates/gallery/photo_detail.html +++ b/gallery/templates/gallery/photo_detail.html @@ -14,7 +14,8 @@ {% block content %} -
Views: {{ photo.views }}
{% endblock %} \ No newline at end of file diff --git a/gallery/views.py b/gallery/views.py index 7f07356..aaf161f 100644 --- a/gallery/views.py +++ b/gallery/views.py @@ -41,15 +41,23 @@ class AlbumDetail(DetailView): 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): model = Photo paginate_by = 30 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