From adaa107f2f01c0b83e505a3590dd8a8fffabaf18 Mon Sep 17 00:00:00 2001 From: George Katsikas <giorgakis.katsikas@gmail.com> Date: Tue, 2 Apr 2024 09:36:13 +0200 Subject: [PATCH] improve notes admin page --- scipost_django/pins/admin.py | 24 +++++++++++++++++++++--- scipost_django/pins/views.py | 2 +- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/scipost_django/pins/admin.py b/scipost_django/pins/admin.py index 7b6608d52..7809af2a5 100644 --- a/scipost_django/pins/admin.py +++ b/scipost_django/pins/admin.py @@ -2,13 +2,22 @@ __copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)" __license__ = "AGPL v3" from django.contrib import admin +from django.urls import reverse +from django.utils.html import format_html from pins.models import Note @admin.register(Note) class NoteAdmin(admin.ModelAdmin): - list_display = ("id", "regarding__object", "title", "author", "created", "modified") + list_display = ( + "id", + "regarding_object_links", + "title", + "author", + "created", + "modified", + ) list_filter = ("created", "modified", "visibility", "regarding_content_type") search_fields = ("title", "author__username") date_hierarchy = "created" @@ -25,5 +34,14 @@ class NoteAdmin(admin.ModelAdmin): ) autocomplete_fields = ("author",) - def regarding__object(self, obj): - return str(obj.regarding) + def regarding_object_links(self, obj): + content_type = obj.regarding_content_type + model = content_type.model_class() + regarding = model.objects.get(pk=obj.regarding_object_id) + admin_url = f"admin:{content_type.app_label}_{content_type.model}_change" + return format_html( + "<div style='display: flex; justify-content:space-between;>" + f'<a href="{regarding.get_absolute_url()}">{regarding}</a>' + f'<a href="{reverse(admin_url, args=[obj.regarding_object_id])}">[admin]</a>' + "</div>" + ) diff --git a/scipost_django/pins/views.py b/scipost_django/pins/views.py index ae0209ab9..a274544ed 100644 --- a/scipost_django/pins/views.py +++ b/scipost_django/pins/views.py @@ -70,7 +70,7 @@ def _hx_notes_list(request, regarding_content_type, regarding_object_id): can_create_notes = request.user.has_perm("pins.can_add_notes") # Filter according to the visibility of the notes - notes = notes.visible_to(request.user, object) + notes = notes.visible_to(request.user, object.__class__) context = { "object": object, -- GitLab