From adb6fac8447d49048082fa02316727a382216b53 Mon Sep 17 00:00:00 2001 From: AP Jama Date: Wed, 21 Aug 2024 23:36:04 +0100 Subject: [PATCH] Moved url logic away from render method and to entry model --- django/contrib/admin/models.py | 10 ++++++++++ django/contrib/admin/templatetags/log.py | 19 +++---------------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/django/contrib/admin/models.py b/django/contrib/admin/models.py index bb81be8297..dbc5ca2c79 100644 --- a/django/contrib/admin/models.py +++ b/django/contrib/admin/models.py @@ -162,6 +162,16 @@ class LogEntry(models.Model): def is_deletion(self): return self.action_flag == DELETION + def get_url_for_user(self): + if not self.content_type: + return self.get_admin_url() + + view_permission_name = f"{self.content_type.app_label}.view_{self.content_type.model}" + if self.user.has_perm(view_permission_name): + return self.get_admin_url() + + return None + def get_change_message(self): """ If self.change_message is a JSON structure, interpret it as a change diff --git a/django/contrib/admin/templatetags/log.py b/django/contrib/admin/templatetags/log.py index 3e2111b8bd..e07e326ec3 100644 --- a/django/contrib/admin/templatetags/log.py +++ b/django/contrib/admin/templatetags/log.py @@ -19,22 +19,9 @@ class AdminLogNode(template.Node): if not user_id.isdigit(): user_id = context[self.user].pk entries = entries.filter(user__pk=user_id) - filtered_context = entries.select_related("content_type", "user")[ - : int(self.limit) - ] - for entry in filtered_context: - content_type = entry.content_type - if not content_type: - entry.url = entry.get_admin_url() - continue - user = entry.user - view_permission_name = f"{content_type.app_label}.view_{content_type.model}" - user_has_permission = user.has_perm(view_permission_name) - if user_has_permission: - entry.url = entry.get_admin_url() - else: - entry.url = None - context[self.varname] = filtered_context + for entry in entries: + entry.url = entry.get_url_for_user() + context[self.varname] = entries[: int(self.limit)] return ""