mirror of
https://gitlab.science.ru.nl/technicie/MarietjeDjango.git
synced 2025-12-09 19:12:20 +01:00
In 1b5b510, the complete list of songs was re-added. Loading this
list is super slow and should not happen.
46 lines
1.3 KiB
Python
46 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):
|
|
exclude = ('song',)
|
|
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"
|