You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Django/djangoblog-yql/logentryadmin.py

107 lines
3.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# 余秋玲: 此文件定义了Django管理后台的日志条目管理类
# 余秋玲: 导入必要的模块和类
from django.contrib import admin
from django.contrib.admin.models import DELETION
from django.contrib.contenttypes.models import ContentType
from django.urls import reverse, NoReverseMatch
from django.utils.encoding import force_str
from django.utils.html import escape
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _
class LogEntryAdmin(admin.ModelAdmin):
# 余秋玲: 设置列表页过滤器字段
list_filter = [
'content_type'
]
# 余秋玲: 设置搜索字段
search_fields = [
'object_repr',
'change_message'
]
# 余秋玲: 设置列表页可点击链接字段
list_display_links = [
'action_time',
'get_change_message',
]
# 余秋玲: 设置列表页显示字段
list_display = [
'action_time',
'user_link',
'content_type',
'object_link',
'get_change_message',
]
def has_add_permission(self, request):
# 余秋玲: 禁用添加权限
return False
def has_change_permission(self, request, obj=None):
# 余秋玲: 只有超级用户或有特定权限的用户可以查看日志
return (
request.user.is_superuser or
request.user.has_perm('admin.change_logentry')
) and request.method != 'POST'
def has_delete_permission(self, request, obj=None):
# 余秋玲: 禁用删除权限
return False
def object_link(self, obj):
# 余秋玲: 创建对象链接,如果是删除操作则不显示链接
object_link = escape(obj.object_repr)
content_type = obj.content_type
if obj.action_flag != DELETION and content_type is not None:
# 余秋玲: 尝试返回实际链接而不是对象字符串
try:
url = reverse(
'admin:{}_{}_change'.format(content_type.app_label,
content_type.model),
args=[obj.object_id]
)
object_link = '<a href="{}">{}</a>'.format(url, object_link)
except NoReverseMatch:
pass
return mark_safe(object_link)
# 余秋玲: 设置对象链接的排序字段和描述
object_link.admin_order_field = 'object_repr'
object_link.short_description = _('object')
def user_link(self, obj):
# 余秋玲: 创建用户链接
content_type = ContentType.objects.get_for_model(type(obj.user))
user_link = escape(force_str(obj.user))
try:
# 余秋玲: 尝试返回实际链接而不是对象字符串
url = reverse(
'admin:{}_{}_change'.format(content_type.app_label,
content_type.model),
args=[obj.user.pk]
)
user_link = '<a href="{}">{}</a>'.format(url, user_link)
except NoReverseMatch:
pass
return mark_safe(user_link)
# 余秋玲: 设置用户链接的排序字段和描述
user_link.admin_order_field = 'user'
user_link.short_description = _('user')
def get_queryset(self, request):
# 余秋玲: 优化查询预取关联的content_type
queryset = super(LogEntryAdmin, self).get_queryset(request)
return queryset.prefetch_related('content_type')
def get_actions(self, request):
# 余秋玲: 移除删除选中动作
actions = super(LogEntryAdmin, self).get_actions(request)
if 'delete_selected' in actions:
del actions['delete_selected']
return actions