Add upload form and update Photo model
This commit is contained in:
parent
c43f3612e3
commit
555c6c0d89
5 changed files with 211 additions and 38 deletions
|
@ -1,4 +1,7 @@
|
|||
from django.contrib import admin
|
||||
from django.http import JsonResponse
|
||||
from django.shortcuts import get_object_or_404, render
|
||||
from django.urls import path, reverse
|
||||
from django.utils.html import format_html
|
||||
from imagekit import ImageSpec
|
||||
from imagekit.admin import AdminThumbnail
|
||||
|
@ -15,21 +18,23 @@ class AdminThumbnailSpec(ImageSpec):
|
|||
|
||||
|
||||
def cached_admin_thumb(instance):
|
||||
cached = ImageCacheFile(AdminThumbnailSpec(instance.photo))
|
||||
cached.generate()
|
||||
return cached
|
||||
if instance.photo:
|
||||
cached = ImageCacheFile(AdminThumbnailSpec(instance.photo))
|
||||
cached.generate()
|
||||
return cached
|
||||
return None
|
||||
|
||||
|
||||
class RedirAdmin(admin.ModelAdmin):
|
||||
list_display = ('path', 'album', 'test_url')
|
||||
search_fields = ('path',)
|
||||
ordering = ('path',)
|
||||
list_per_page = 10
|
||||
list_per_page = 30
|
||||
list_editable = ('album',)
|
||||
|
||||
def formfield_for_foreignkey(self, db_field, request, **kwargs):
|
||||
if db_field.name == "album":
|
||||
kwargs["queryset"] = Album.objects.all().order_by('name') # Järjestä albumit aakkosjärjestykseen
|
||||
kwargs["queryset"] = Album.objects.all().order_by('name')
|
||||
return super().formfield_for_foreignkey(db_field, request, **kwargs)
|
||||
|
||||
def test_url(self, obj):
|
||||
|
@ -57,12 +62,50 @@ class LocationAdmin(admin.ModelAdmin):
|
|||
|
||||
class AlbumAdmin(admin.ModelAdmin):
|
||||
prepopulated_fields = {'slug': ('name',)}
|
||||
list_display = ('name', 'location', 'album_date', 'is_public', 'thumbnail', )
|
||||
list_display = ('name', 'location', 'album_date', 'is_public', 'upload_link', 'thumbnail')
|
||||
search_fields = ('name',)
|
||||
ordering = ('-album_date',)
|
||||
list_per_page = 20
|
||||
list_editable = ('is_public',)
|
||||
readonly_fields = ['cover_preview'] # Lisätään esikatselukuva readonly_fieldsiin
|
||||
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"
|
||||
|
||||
def cover_preview(self, obj):
|
||||
if obj.cover and obj.cover.photo:
|
||||
|
@ -81,21 +124,12 @@ class AlbumAdmin(admin.ModelAdmin):
|
|||
|
||||
return super().formfield_for_foreignkey(db_field, request, **kwargs)
|
||||
|
||||
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"
|
||||
|
||||
|
||||
class PhotoAdmin(admin.ModelAdmin):
|
||||
list_display = ('slug', 'album', 'is_favorite', 'admin_thumbnail',)
|
||||
list_display = ('slug', 'album', 'is_favorite', 'admin_thumbnail')
|
||||
list_display_links = ('slug',)
|
||||
search_fields = ('slug', 'photo',)
|
||||
readonly_fields = ['slug', 'taken_at', 'height', 'width', 'exif', ]
|
||||
search_fields = ('slug', 'photo')
|
||||
readonly_fields = ['slug', 'taken_at', 'height', 'width', 'exif']
|
||||
admin_thumbnail = AdminThumbnail(image_field=cached_admin_thumb)
|
||||
list_filter = ('album',)
|
||||
list_per_page = 30
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue