Files
MarietjeDjango/marietje/songs/admin.py
Daan Sprenkels 1b5b5106ba report-note: admin: Add a link to relevant song
Previously, the admin could not directly move from a report note to
its corresponding song. This commit adds a link that will go
directly to the "change" page for the corresponding song.
2020-06-15 15:18:37 +02:00

45 lines
1.3 KiB
Python

from django.contrib import admin
from django.urls import reverse
from django.utils.html import format_html
from .models import ReportNote, Song
class ReportNoteInline(admin.StackedInline):
model = ReportNote
extra = 0
@admin.register(Song)
class SongAdmin(admin.ModelAdmin):
list_display = ('artist', 'title', 'user_name', 'reports')
search_fields = ('artist', 'title', 'user__name')
inlines = [ReportNoteInline]
@staticmethod
def reports(song):
return ReportNote.objects.filter(song=song).count()
@staticmethod
def user_name(song):
try:
return song.user.name
except AttributeError:
return '<unknown>'
@staticmethod
def get_readonly_fields(request, obj=None):
return [] if request.user.is_superuser else ['hash']
@admin.register(ReportNote)
class ReportNoteAdmin(admin.ModelAdmin):
list_display = ('song', 'note', 'user')
search_fields = ('song__artist', 'song__title', 'user__name')
readonly_fields = ('song_link',)
@staticmethod
def song_link(note):
url = reverse("admin:songs_song_change", args=(note.song.id,))
return format_html("<a href='{url}'>{song}</a>", url=url, song=note.song)
song_link.short_description = "Song link"