from django.contrib import admin
from django.utils.html import format_html
from django.urls import reverse
from django.utils.safestring import mark_safe
from ..models.images import ImageLibrary
@admin.register(ImageLibrary)
class ImageLibraryAdmin(admin.ModelAdmin):
"""
Admin interface for the Image Library.
"""
list_display = [
"image_thumbnail",
"name",
"category",
"get_dimensions",
"get_file_size_display",
"usage_count",
"uploaded_by",
"uploaded_at",
]
list_filter = [
"category",
"uploaded_at",
"uploaded_by",
]
search_fields = [
"name",
"description",
"alt_text",
"tags",
]
readonly_fields = [
"width",
"height",
"file_size",
"usage_count",
"uploaded_at",
"updated_at",
"image_preview",
]
prepopulated_fields = {"slug": ("name",)}
fieldsets = (
("Image Information", {"fields": ("name", "slug", "description", "alt_text")}),
("Image File", {"fields": ("image", "image_preview")}),
("Categorization", {"fields": ("category", "tags")}),
(
"Metadata",
{
"fields": ("width", "height", "file_size", "usage_count"),
"classes": ("collapse",),
},
),
(
"Timestamps",
{
"fields": ("uploaded_by", "uploaded_at", "updated_at"),
"classes": ("collapse",),
},
),
)
def image_thumbnail(self, obj):
"""
Display small thumbnail in list view.
"""
if obj.image:
# Use img tag for all images in list view to maintain clickability
# SVG files will still display correctly with img tag
return format_html(
'',
obj.image.url,
)
return "No Image"
image_thumbnail.short_description = "Thumbnail"
def image_preview(self, obj):
"""
Display larger preview in detail view.
"""
if obj.image:
if obj.is_svg():
# For SVG files in detail view, use object tag for better rendering
# This is only for display, not for clickable elements
return format_html(
'