mirror of
https://gitlab.science.ru.nl/technicie/MarietjeDjango.git
synced 2025-12-09 20:32:21 +01:00
69 lines
2.0 KiB
Python
69 lines
2.0 KiB
Python
from django.contrib import admin
|
|
from django.db.models import Count
|
|
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
|
|
|
|
class SongHasReportNoteFilter(admin.SimpleListFilter):
|
|
title = 'report notes'
|
|
parameter_name = 'reportnotes'
|
|
|
|
def lookups(self, request, model_admin):
|
|
return (
|
|
('yes', 'yes'),
|
|
('no', 'no'),
|
|
)
|
|
|
|
def queryset(self, request, queryset):
|
|
queryset = queryset.annotate(num_reports=Count('reportnote'))
|
|
if self.value() == 'yes':
|
|
return queryset.exclude(num_reports=0)
|
|
if self.value() == 'no':
|
|
return queryset.filter(num_reports=0)
|
|
return queryset
|
|
|
|
|
|
@admin.register(Song)
|
|
class SongAdmin(admin.ModelAdmin):
|
|
list_display = ('artist', 'title', 'user_name', 'reports')
|
|
list_display_links = ('artist', 'title')
|
|
list_filter = (SongHasReportNoteFilter,)
|
|
search_fields = ('artist', 'title', 'user__name')
|
|
inlines = [ReportNoteInline]
|
|
|
|
@staticmethod
|
|
def reports(song):
|
|
# num_reports is annotated by SongHasReportNoteFilter
|
|
return song.num_reports
|
|
|
|
@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"
|