|
|
|
|
@ -1,47 +1,84 @@
|
|
|
|
|
# gst: 导入Django admin模块,用于自定义后台管理功能
|
|
|
|
|
from django.contrib import admin
|
|
|
|
|
# gst: 导入reverse用于生成后台管理页面的URL
|
|
|
|
|
from django.urls import reverse
|
|
|
|
|
# gst: 导入format_html用于在管理界面生成HTML链接
|
|
|
|
|
from django.utils.html import format_html
|
|
|
|
|
# gst: 导入gettext_lazy用于国际化翻译(别名_)
|
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def disable_commentstatus(modeladmin, request, queryset):
|
|
|
|
|
"""
|
|
|
|
|
gst: 定义admin动作函数,批量将选中的评论设置为“不显示”
|
|
|
|
|
:param modeladmin: 模型管理类实例
|
|
|
|
|
:param request: 请求对象
|
|
|
|
|
:param queryset: 选中的评论查询集
|
|
|
|
|
"""
|
|
|
|
|
queryset.update(is_enable=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def enable_commentstatus(modeladmin, request, queryset):
|
|
|
|
|
"""
|
|
|
|
|
gst: 定义admin动作函数,批量将选中的评论设置为“显示”
|
|
|
|
|
:param modeladmin: 模型管理类实例
|
|
|
|
|
:param request: 请求对象
|
|
|
|
|
:param queryset: 选中的评论查询集
|
|
|
|
|
"""
|
|
|
|
|
queryset.update(is_enable=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# gst: 为动作函数设置后台显示名称(支持国际化)
|
|
|
|
|
disable_commentstatus.short_description = _('Disable comments')
|
|
|
|
|
enable_commentstatus.short_description = _('Enable comments')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CommentAdmin(admin.ModelAdmin):
|
|
|
|
|
list_per_page = 20
|
|
|
|
|
"""
|
|
|
|
|
gst: 自定义Comment模型的后台管理类,配置列表显示、过滤、动作等功能
|
|
|
|
|
"""
|
|
|
|
|
list_per_page = 20 # gst: 后台列表每页显示20条数据
|
|
|
|
|
list_display = (
|
|
|
|
|
'id',
|
|
|
|
|
'body',
|
|
|
|
|
'link_to_userinfo',
|
|
|
|
|
'link_to_article',
|
|
|
|
|
'link_to_userinfo', # gst: 自定义列,显示评论作者的可点击链接
|
|
|
|
|
'link_to_article', # gst: 自定义列,显示评论关联文章的可点击链接
|
|
|
|
|
'is_enable',
|
|
|
|
|
'creation_time')
|
|
|
|
|
list_display_links = ('id', 'body', 'is_enable')
|
|
|
|
|
list_filter = ('is_enable',)
|
|
|
|
|
exclude = ('creation_time', 'last_modify_time')
|
|
|
|
|
actions = [disable_commentstatus, enable_commentstatus]
|
|
|
|
|
'creation_time'
|
|
|
|
|
)
|
|
|
|
|
list_display_links = ('id', 'body', 'is_enable') # gst: 这些字段可点击进入详情页
|
|
|
|
|
list_filter = ('is_enable',) # gst: 后台列表过滤条件(按是否显示过滤)
|
|
|
|
|
exclude = ('creation_time', 'last_modify_time') # gst: 编辑页排除这些字段(不允许编辑)
|
|
|
|
|
actions = [disable_commentstatus, enable_commentstatus] # gst: 注册批量动作
|
|
|
|
|
|
|
|
|
|
def link_to_userinfo(self, obj):
|
|
|
|
|
"""
|
|
|
|
|
gst: 生成评论作者的后台管理链接(点击可进入用户详情页)
|
|
|
|
|
:param obj: Comment模型实例
|
|
|
|
|
:return: 带HTML链接的作者信息
|
|
|
|
|
"""
|
|
|
|
|
# 获取用户模型的app_label和model_name,用于生成URL
|
|
|
|
|
info = (obj.author._meta.app_label, obj.author._meta.model_name)
|
|
|
|
|
# 生成用户详情页的URL(如admin/auth/user/1/change/)
|
|
|
|
|
link = reverse('admin:%s_%s_change' % info, args=(obj.author.id,))
|
|
|
|
|
return format_html(
|
|
|
|
|
u'<a href="%s">%s</a>' %
|
|
|
|
|
(link, obj.author.nickname if obj.author.nickname else obj.author.email))
|
|
|
|
|
# 优先显示用户昵称,无昵称则显示邮箱
|
|
|
|
|
display_text = obj.author.nickname if obj.author.nickname else obj.author.email
|
|
|
|
|
# 生成带链接的HTML内容
|
|
|
|
|
return format_html(u'<a href="%s">%s</a>' % (link, display_text))
|
|
|
|
|
|
|
|
|
|
def link_to_article(self, obj):
|
|
|
|
|
"""
|
|
|
|
|
gst: 生成评论关联文章的后台管理链接(点击可进入文章详情页)
|
|
|
|
|
:param obj: Comment模型实例
|
|
|
|
|
:return: 带HTML链接的文章标题
|
|
|
|
|
"""
|
|
|
|
|
# 获取文章模型的app_label和model_name,用于生成URL
|
|
|
|
|
info = (obj.article._meta.app_label, obj.article._meta.model_name)
|
|
|
|
|
# 生成文章详情页的URL(如admin/blog/article/1/change/)
|
|
|
|
|
link = reverse('admin:%s_%s_change' % info, args=(obj.article.id,))
|
|
|
|
|
return format_html(
|
|
|
|
|
u'<a href="%s">%s</a>' % (link, obj.article.title))
|
|
|
|
|
# 生成带链接的HTML内容(显示文章标题)
|
|
|
|
|
return format_html(u'<a href="%s">%s</a>' % (link, obj.article.title))
|
|
|
|
|
|
|
|
|
|
# gst: 为自定义列设置后台显示名称(支持国际化)
|
|
|
|
|
link_to_userinfo.short_description = _('User')
|
|
|
|
|
link_to_article.short_description = _('Article')
|
|
|
|
|
link_to_article.short_description = _('Article')
|