1
0
mirror of https://github.com/django/django.git synced 2024-12-23 01:25:58 +00:00

Moved url logic away from render method and to entry model

This commit is contained in:
AP Jama 2024-08-21 23:36:04 +01:00
parent 0d971ab7ca
commit adb6fac844
2 changed files with 13 additions and 16 deletions

View File

@ -162,6 +162,16 @@ class LogEntry(models.Model):
def is_deletion(self): def is_deletion(self):
return self.action_flag == DELETION 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): def get_change_message(self):
""" """
If self.change_message is a JSON structure, interpret it as a change If self.change_message is a JSON structure, interpret it as a change

View File

@ -19,22 +19,9 @@ class AdminLogNode(template.Node):
if not user_id.isdigit(): if not user_id.isdigit():
user_id = context[self.user].pk user_id = context[self.user].pk
entries = entries.filter(user__pk=user_id) entries = entries.filter(user__pk=user_id)
filtered_context = entries.select_related("content_type", "user")[ for entry in entries:
: int(self.limit) entry.url = entry.get_url_for_user()
] context[self.varname] = entries[: 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
return "" return ""