mirror of
https://gitlab.science.ru.nl/technicie/MarietjeDjango.git
synced 2025-12-09 19:12:20 +01:00
Before this patch, we used the default setting, which emits a HTML <select> tag containing a list of *all* the users. We currently have enough users that we do not want to load that complete list every time. So now, use the autocomplete field instead.
48 lines
1.3 KiB
Python
48 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]
|
|
autocomplete_fields = ('user',)
|
|
|
|
@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')
|
|
autocomplete_fields = ('user',)
|
|
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"
|