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.
88 lines
3.5 KiB
88 lines
3.5 KiB
# Zxy导入Django的admin模块
|
|
from django.contrib import admin
|
|
# Zxy导入Django的LogEntry模型
|
|
from django.contrib.admin.models import DELETION
|
|
# Zxy导入Django的内容类型模型
|
|
from django.contrib.contenttypes.models import ContentType
|
|
# Zxy导入Django的URL工具
|
|
from django.urls import reverse, NoReverseMatch
|
|
# Zxy导入Django的编码工具
|
|
from django.utils.encoding import force_str
|
|
# Zxy导入Django的HTML工具
|
|
from django.utils.html import escape
|
|
# Zxy导入Django的安全字符串工具
|
|
from django.utils.safestring import mark_safe
|
|
# Zxy导入Django的翻译工具
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
# Zxy定义LogEntryAdmin类
|
|
class LogEntryAdmin(admin.ModelAdmin):
|
|
list_filter = ['content_type'] # Zxy按内容类型过滤
|
|
search_fields = ['object_repr', 'change_message'] # Zxy搜索字段
|
|
|
|
list_display_links = ['action_time', 'get_change_message'] # Zxy显示链接的字段
|
|
list_display = ['action_time', 'user_link', 'content_type', 'object_link', 'get_change_message'] # Zxy显示字段
|
|
|
|
# Zxy检查是否有添加权限
|
|
def has_add_permission(self, request):
|
|
return False
|
|
|
|
# Zxy检查是否有修改权限
|
|
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'
|
|
|
|
# Zxy检查是否有删除权限
|
|
def has_delete_permission(self, request, obj=None):
|
|
return False
|
|
|
|
# Zxy获取对象链接
|
|
def object_link(self, obj):
|
|
object_link = escape(obj.object_repr) # Zxy转义对象表示
|
|
content_type = obj.content_type
|
|
|
|
if obj.action_flag != DELETION and content_type is not None: # Zxy检查是否为删除操作
|
|
try:
|
|
url = reverse( # Zxy生成反向URL
|
|
'admin:{}_{}_change'.format(content_type.app_label, content_type.model),
|
|
args=[obj.object_id]
|
|
)
|
|
object_link = '<a href="{}">{}</a>'.format(url, object_link) # Zxy生成链接
|
|
except NoReverseMatch:
|
|
pass
|
|
return mark_safe(object_link) # Zxy标记为安全字符串
|
|
|
|
object_link.admin_order_field = 'object_repr' # Zxy排序字段
|
|
object_link.short_description = _('object') # Zxy字段描述
|
|
|
|
# Zxy获取用户链接
|
|
def user_link(self, obj):
|
|
content_type = ContentType.objects.get_for_model(type(obj.user)) # Zxy获取用户的内容类型
|
|
user_link = escape(force_str(obj.user)) # Zxy转义用户表示
|
|
|
|
try:
|
|
url = reverse( # Zxy生成反向URL
|
|
'admin:{}_{}_change'.format(content_type.app_label, content_type.model),
|
|
args=[obj.user.pk]
|
|
)
|
|
user_link = '<a href="{}">{}</a>'.format(url, user_link) # Zxy生成链接
|
|
except NoReverseMatch:
|
|
pass
|
|
return mark_safe(user_link) # Zxy标记为安全字符串
|
|
|
|
user_link.admin_order_field = 'user' # Zxy排序字段
|
|
user_link.short_description = _('user') # Zxy字段描述
|
|
|
|
# Zxy获取查询集
|
|
def get_queryset(self, request):
|
|
queryset = super(LogEntryAdmin, self).get_queryset(request)
|
|
return queryset.prefetch_related('content_type') # Zxy预加载内容类型
|
|
|
|
# Zxy获取操作
|
|
def get_actions(self, request):
|
|
actions = super(LogEntryAdmin, self).get_actions(request)
|
|
if 'delete_selected' in actions: # Zxy移除删除操作
|
|
del actions['delete_selected']
|
|
return actions |