muistox/gallery/admin.py

57 lines
1.5 KiB
Python
Raw Normal View History

2024-12-28 12:21:29 +02:00
from django.contrib import admin
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
from imagekit.processors import ResizeToFill
2024-12-28 12:21:29 +02:00
2025-01-05 17:26:09 +02:00
from gallery.models import Album, City, Location, Photo
2024-12-29 17:35:10 +02:00
2025-01-11 23:07:53 +02:00
class AdminThumbnailSpec(ImageSpec):
processors = [ResizeToFill(50, 50)]
format = 'JPEG'
options = {'quality': 60}
def cached_admin_thumb(instance):
cached = ImageCacheFile(AdminThumbnailSpec(instance.photo))
cached.generate()
return cached
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-01-11 23:07:53 +02:00
list_display = ('__str__', 'location', 'album_date', 'is_public', )
2024-12-29 23:25:03 +02:00
search_fields = ('name',)
ordering = ('name',)
list_per_page = 30
2025-01-11 23:07:53 +02:00
list_editable = ('is_public', 'location')
2024-12-29 23:25:03 +02:00
2024-12-29 17:35:10 +02:00
2025-01-05 17:26:09 +02:00
class PhotoAdmin(admin.ModelAdmin):
2025-01-11 23:07:53 +02:00
list_display = ('slug', 'album', 'admin_thumbnail',)
list_display_links = ('slug',)
2025-01-05 17:26:09 +02:00
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-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)