diff --git a/gallery/admin.py b/gallery/admin.py
index 2b7040f..8cec1db 100644
--- a/gallery/admin.py
+++ b/gallery/admin.py
@@ -1,4 +1,5 @@
from django.contrib import admin
+from django.utils.html import format_html
from imagekit import ImageSpec
from imagekit.admin import AdminThumbnail
from imagekit.cachefiles import ImageCacheFile
@@ -36,16 +37,34 @@ class LocationAdmin(admin.ModelAdmin):
class AlbumAdmin(admin.ModelAdmin):
prepopulated_fields = {'slug': ('name',)}
- list_display = ('__str__', 'location', 'album_date', 'is_public', )
+ list_display = ('__str__', 'location', 'album_date', 'is_public', 'thumbnail', )
search_fields = ('name',)
ordering = ('name',)
list_per_page = 30
list_editable = ('is_public', 'location')
+ def formfield_for_foreignkey(self, db_field, request, **kwargs):
+ if db_field.name == "cover":
+ if hasattr(request, 'resolver_match') and request.resolver_match.kwargs.get('object_id'):
+ album_id = request.resolver_match.kwargs['object_id']
+ kwargs["queryset"] = Photo.objects.filter(album_id=album_id)
+ return super().formfield_for_foreignkey(db_field, request, **kwargs)
+
+ def thumbnail(self, obj):
+ if obj.cover and obj.cover.photo:
+ return format_html(
+ '',
+ obj.cover.photo.url,
+ )
+ return "-"
+ thumbnail.short_description = "Thumbnail"
+
+
class PhotoAdmin(admin.ModelAdmin):
list_display = ('slug', 'album', 'admin_thumbnail',)
list_display_links = ('slug',)
+ search_fields = ('slug',)
readonly_fields = ['slug', 'taken_at', 'height', 'width', 'exif', ]
admin_thumbnail = AdminThumbnail(image_field=cached_admin_thumb)
diff --git a/gallery/migrations/0001_initial.py b/gallery/migrations/0001_initial.py
index e817847..0a71445 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-13 20:45
+# Generated by Django 5.1.4 on 2025-01-15 18:22
import datetime
import django.db.models.deletion
@@ -43,7 +43,6 @@ class Migration(migrations.Migration):
('name', models.CharField(max_length=150, unique=True, verbose_name='Album')),
('slug', models.SlugField(max_length=150, unique=True, verbose_name='Slug')),
('album_date', models.DateField(default=datetime.datetime.now, verbose_name='Album Date')),
- ('cover', models.ImageField(blank=True, null=True, upload_to='covers/', verbose_name='Album Cover')),
('is_public', models.BooleanField(default=False, verbose_name='Published')),
('location', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='albums', to='gallery.location', verbose_name='Location')),
],
@@ -67,4 +66,9 @@ class Migration(migrations.Migration):
('album', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='photos', to='gallery.album', verbose_name='Album')),
],
),
+ migrations.AddField(
+ model_name='album',
+ name='cover',
+ field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='cover_to', to='gallery.photo', verbose_name='Album cover'),
+ ),
]
diff --git a/gallery/models/album.py b/gallery/models/album.py
index 8c3267b..c084f5d 100644
--- a/gallery/models/album.py
+++ b/gallery/models/album.py
@@ -1,23 +1,35 @@
from datetime import datetime
from django.db import models
+from django.templatetags.static import static
from django.urls import reverse
from django.utils.text import slugify
from gallery.models.location import Location
+#from gallery.models.photo import Photo
+
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.ImageField(upload_to="covers/", blank=True, null=True, verbose_name="Album Cover")
+ 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")
@property
def photos_in_album(self):
return self.photos.count()
+
+ @property
+ def cover_url(self):
+ if self.cover:
+ return self.cover.photo.url
+ random_cover = self.photos.order_by('-width').first()
+ if random_cover:
+ return random_cover.photo.url
+ return static('img/placeholder.png')
def save(self, *args, **kwargs):
if not self.slug:
diff --git a/gallery/models/photo.py b/gallery/models/photo.py
index 867ad1f..c49759e 100644
--- a/gallery/models/photo.py
+++ b/gallery/models/photo.py
@@ -43,6 +43,13 @@ class Photo(models.Model):
self.views += 1
self.save()
+ def get_next(self):
+ return self.__class__.objects.filter(taken_at__gt=self.taken_at, album=self.album.id).order_by('taken_at').first()
+
+ def get_prev(self):
+ return self.__class__.objects.filter(taken_at__lt=self.taken_at, album=self.album.id).order_by('-taken_at').first()
+
+
def save(self, *args, **kwargs):
self.exif_data = Exif(self.photo.path)
datetime_taken = getattr(self.exif_data, 'datetimeoriginal', datetime.now)()
diff --git a/gallery/templates/gallery/album_list.html b/gallery/templates/gallery/album_list.html
index 3d93c1b..5b92ae2 100644
--- a/gallery/templates/gallery/album_list.html
+++ b/gallery/templates/gallery/album_list.html
@@ -1,4 +1,5 @@
{% extends "base.html" %}
+{% load static %}
{% block title %} Gallery : Albums {% endblock %}
@@ -18,7 +19,7 @@