2024-12-28 12:21:29 +02:00
|
|
|
from django.contrib import admin
|
2025-02-24 20:23:07 +02:00
|
|
|
from django.http import JsonResponse
|
|
|
|
from django.shortcuts import get_object_or_404, render
|
|
|
|
from django.urls import path, reverse
|
2025-01-15 20:31:41 +02:00
|
|
|
from django.utils.html import format_html
|
2025-01-11 23:07:53 +02:00
|
|
|
from imagekit import ImageSpec
|
2025-01-05 17:26:09 +02:00
|
|
|
from imagekit.admin import AdminThumbnail
|
2025-01-11 23:07:53 +02:00
|
|
|
from imagekit.cachefiles import ImageCacheFile
|
2025-01-19 21:26:03 +02:00
|
|
|
from imagekit.processors import ResizeToFit
|
2024-12-28 12:21:29 +02:00
|
|
|
|
2025-01-20 21:21:09 +02:00
|
|
|
from gallery.models import Album, City, Location, Photo, Redir
|
2024-12-29 17:35:10 +02:00
|
|
|
|
|
|
|
|
2025-01-11 23:07:53 +02:00
|
|
|
class AdminThumbnailSpec(ImageSpec):
|
2025-01-19 21:26:03 +02:00
|
|
|
processors = [ResizeToFit(100, 100)]
|
2025-01-11 23:07:53 +02:00
|
|
|
format = 'JPEG'
|
|
|
|
options = {'quality': 60}
|
|
|
|
|
|
|
|
|
|
|
|
def cached_admin_thumb(instance):
|
2025-02-24 20:23:07 +02:00
|
|
|
if instance.photo:
|
|
|
|
cached = ImageCacheFile(AdminThumbnailSpec(instance.photo))
|
|
|
|
cached.generate()
|
|
|
|
return cached
|
|
|
|
return None
|
2025-01-11 23:07:53 +02:00
|
|
|
|
|
|
|
|
2025-01-20 21:21:09 +02:00
|
|
|
class RedirAdmin(admin.ModelAdmin):
|
|
|
|
list_display = ('path', 'album', 'test_url')
|
|
|
|
search_fields = ('path',)
|
|
|
|
ordering = ('path',)
|
2025-02-24 20:23:07 +02:00
|
|
|
list_per_page = 30
|
2025-01-20 21:21:09 +02:00
|
|
|
list_editable = ('album',)
|
|
|
|
|
|
|
|
def formfield_for_foreignkey(self, db_field, request, **kwargs):
|
|
|
|
if db_field.name == "album":
|
2025-02-24 20:23:07 +02:00
|
|
|
kwargs["queryset"] = Album.objects.all().order_by('name')
|
2025-01-20 21:21:09 +02:00
|
|
|
return super().formfield_for_foreignkey(db_field, request, **kwargs)
|
|
|
|
|
|
|
|
def test_url(self, obj):
|
|
|
|
return format_html(
|
|
|
|
'<a href="/{}" target="_blank">http://nyymix.net/{}</a>',
|
|
|
|
obj.path, obj.path
|
|
|
|
)
|
|
|
|
test_url.short_description = "Test redirection"
|
|
|
|
|
|
|
|
|
2024-12-29 17:35:10 +02:00
|
|
|
class CityAdmin(admin.ModelAdmin):
|
|
|
|
list_display = ('name',)
|
|
|
|
search_fields = ('name',)
|
|
|
|
ordering = ('name',)
|
|
|
|
list_per_page = 30
|
|
|
|
|
|
|
|
|
|
|
|
class LocationAdmin(admin.ModelAdmin):
|
|
|
|
list_display = ('city', 'place')
|
|
|
|
list_filter = ('city',)
|
|
|
|
ordering = ('city__name',)
|
|
|
|
search_fields = ('place', 'city__name',)
|
|
|
|
list_per_page = 30
|
|
|
|
|
2025-01-05 17:26:09 +02:00
|
|
|
|
2024-12-29 23:25:03 +02:00
|
|
|
class AlbumAdmin(admin.ModelAdmin):
|
|
|
|
prepopulated_fields = {'slug': ('name',)}
|
2025-02-24 20:23:07 +02:00
|
|
|
list_display = ('name', 'location', 'album_date', 'is_public', 'upload_link', 'thumbnail')
|
2024-12-29 23:25:03 +02:00
|
|
|
search_fields = ('name',)
|
2025-01-18 09:40:47 +02:00
|
|
|
ordering = ('-album_date',)
|
2025-01-20 21:21:09 +02:00
|
|
|
list_per_page = 20
|
2025-01-21 15:31:47 +02:00
|
|
|
list_editable = ('is_public',)
|
2025-02-24 20:23:07 +02:00
|
|
|
readonly_fields = ['cover_preview']
|
|
|
|
|
|
|
|
change_form_template = "admin/gallery/album/change_form.html"
|
|
|
|
|
|
|
|
def get_urls(self):
|
|
|
|
urls = super().get_urls()
|
|
|
|
custom_urls = [
|
|
|
|
path('<int:album_id>/upload/', self.admin_site.admin_view(self.upload_photos), name="gallery_album_upload"),
|
|
|
|
]
|
|
|
|
return custom_urls + urls
|
|
|
|
|
|
|
|
def upload_photos(self, request, album_id, *args, **kwargs):
|
|
|
|
"""Käsittelee valokuvien lataamisen albumiin."""
|
|
|
|
album = get_object_or_404(Album, id=album_id)
|
|
|
|
|
|
|
|
if request.method == 'POST' and request.FILES:
|
|
|
|
uploaded_files = request.FILES.getlist('photo')
|
|
|
|
for file in uploaded_files:
|
|
|
|
Photo.objects.create(album=album, photo=file)
|
|
|
|
return JsonResponse({"message": "Photos uploaded successfully!"})
|
|
|
|
|
|
|
|
return render(request, 'admin/upload_photos.html', {'album': album})
|
|
|
|
|
|
|
|
def upload_link(self, obj):
|
|
|
|
"""Lisää 'Upload Photos' -painikkeen albumin muokkaussivulle"""
|
|
|
|
if obj.id:
|
|
|
|
url = reverse("admin:gallery_album_upload", kwargs={"album_id": obj.id})
|
|
|
|
return format_html('<a href="{}" class="button">Upload Photos</a>', url)
|
|
|
|
return "-"
|
|
|
|
upload_link.short_description = "Upload Photos"
|
|
|
|
|
|
|
|
def thumbnail(self, obj):
|
|
|
|
if obj.cover and obj.cover.photo:
|
|
|
|
return format_html(
|
|
|
|
'<img src="{}" style="width: 50px; height: auto;"/>',
|
|
|
|
obj.cover.photo.url,
|
|
|
|
)
|
|
|
|
return "-"
|
|
|
|
thumbnail.short_description = "Thumbnail"
|
2025-01-20 21:21:09 +02:00
|
|
|
|
|
|
|
def cover_preview(self, obj):
|
|
|
|
if obj.cover and obj.cover.photo:
|
|
|
|
return format_html(
|
|
|
|
'<img src="{}" style="max-width: 300px; height: auto;"/>',
|
|
|
|
obj.cover.photo.url,
|
|
|
|
)
|
|
|
|
return "No cover image available"
|
|
|
|
cover_preview.short_description = "Cover Preview"
|
2024-12-29 23:25:03 +02:00
|
|
|
|
2025-01-15 20:31:41 +02:00
|
|
|
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)
|
2025-01-20 21:21:09 +02:00
|
|
|
|
2025-01-15 20:31:41 +02:00
|
|
|
return super().formfield_for_foreignkey(db_field, request, **kwargs)
|
2025-01-20 21:21:09 +02:00
|
|
|
|
2025-01-15 20:31:41 +02:00
|
|
|
|
2025-01-05 17:26:09 +02:00
|
|
|
class PhotoAdmin(admin.ModelAdmin):
|
2025-02-24 20:23:07 +02:00
|
|
|
list_display = ('slug', 'album', 'is_favorite', 'admin_thumbnail')
|
2025-01-11 23:07:53 +02:00
|
|
|
list_display_links = ('slug',)
|
2025-02-24 20:23:07 +02:00
|
|
|
search_fields = ('slug', 'photo')
|
|
|
|
readonly_fields = ['slug', 'taken_at', 'height', 'width', 'exif']
|
2025-01-11 23:07:53 +02:00
|
|
|
admin_thumbnail = AdminThumbnail(image_field=cached_admin_thumb)
|
2025-01-20 21:21:09 +02:00
|
|
|
list_filter = ('album',)
|
|
|
|
list_per_page = 30
|
|
|
|
list_editable = ('is_favorite',)
|
2025-01-05 17:26:09 +02:00
|
|
|
|
|
|
|
|
2024-12-29 23:25:03 +02:00
|
|
|
admin.site.register(City, CityAdmin)
|
2024-12-29 17:35:10 +02:00
|
|
|
admin.site.register(Location, LocationAdmin)
|
2024-12-29 23:25:03 +02:00
|
|
|
admin.site.register(Album, AlbumAdmin)
|
2025-01-05 17:26:09 +02:00
|
|
|
admin.site.register(Photo, PhotoAdmin)
|
2025-01-20 21:21:09 +02:00
|
|
|
admin.site.register(Redir, RedirAdmin)
|