|
|
|
|
@ -1,49 +1,81 @@
|
|
|
|
|
# zy: 评论管理模块 - 配置Django后台管理中评论模型的显示和操作功能
|
|
|
|
|
from django.contrib import admin
|
|
|
|
|
from django.urls import reverse
|
|
|
|
|
from django.utils.html import format_html
|
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# zy: 禁用评论状态 - 管理员动作函数,用于批量禁用评论
|
|
|
|
|
def disable_commentstatus(modeladmin, request, queryset):
|
|
|
|
|
# zy: 将选中的评论集的is_enable字段更新为False(禁用)
|
|
|
|
|
queryset.update(is_enable=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# zy: 启用评论状态 - 管理员动作函数,用于批量启用评论
|
|
|
|
|
def enable_commentstatus(modeladmin, request, queryset):
|
|
|
|
|
# zy: 将选中的评论集的is_enable字段更新为True(启用)
|
|
|
|
|
queryset.update(is_enable=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# zy: 设置动作在后台显示的描述文字(支持国际化)
|
|
|
|
|
disable_commentstatus.short_description = _('Disable comments')
|
|
|
|
|
enable_commentstatus.short_description = _('Enable comments')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# zy: 评论管理类 - 自定义Django后台的评论管理界面
|
|
|
|
|
class CommentAdmin(admin.ModelAdmin):
|
|
|
|
|
# zy: 设置列表页每页显示20条评论
|
|
|
|
|
list_per_page = 20
|
|
|
|
|
|
|
|
|
|
# zy: 定义列表页显示的字段列
|
|
|
|
|
list_display = (
|
|
|
|
|
'id',
|
|
|
|
|
'body',
|
|
|
|
|
'link_to_userinfo',
|
|
|
|
|
'link_to_article',
|
|
|
|
|
'is_enable',
|
|
|
|
|
'creation_time')
|
|
|
|
|
'id', # zy: 评论ID
|
|
|
|
|
'body', # zy: 评论内容
|
|
|
|
|
'link_to_userinfo', # zy: 自定义方法 - 用户信息链接
|
|
|
|
|
'link_to_article', # zy: 自定义方法 - 文章链接
|
|
|
|
|
'is_enable', # zy: 是否启用状态
|
|
|
|
|
'creation_time' # zy: 创建时间
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# zy: 设置哪些字段可以作为链接点击进入编辑页
|
|
|
|
|
list_display_links = ('id', 'body', 'is_enable')
|
|
|
|
|
list_filter = ('is_enable',)
|
|
|
|
|
|
|
|
|
|
# zy: 设置右侧过滤侧边栏的过滤条件
|
|
|
|
|
list_filter = ('is_enable',) # zy: 按启用状态过滤
|
|
|
|
|
|
|
|
|
|
# zy: 排除在编辑表单中显示的字段(系统自动管理的字段)
|
|
|
|
|
exclude = ('creation_time', 'last_modify_time')
|
|
|
|
|
|
|
|
|
|
# zy: 定义批量操作动作列表
|
|
|
|
|
actions = [disable_commentstatus, enable_commentstatus]
|
|
|
|
|
|
|
|
|
|
# zy: 设置使用弹出窗口选择关联对象的字段(优化性能)
|
|
|
|
|
raw_id_fields = ('author', 'article')
|
|
|
|
|
search_fields = ('body',)
|
|
|
|
|
|
|
|
|
|
# zy: 设置可搜索的字段
|
|
|
|
|
search_fields = ('body',) # zy: 按评论内容搜索
|
|
|
|
|
|
|
|
|
|
# zy: 自定义方法 - 生成用户信息链接
|
|
|
|
|
def link_to_userinfo(self, obj):
|
|
|
|
|
# zy: 获取作者模型的app名称和模型名称
|
|
|
|
|
info = (obj.author._meta.app_label, obj.author._meta.model_name)
|
|
|
|
|
# zy: 生成作者编辑页面的URL
|
|
|
|
|
link = reverse('admin:%s_%s_change' % info, args=(obj.author.id,))
|
|
|
|
|
# zy: 返回HTML链接,显示用户昵称或邮箱
|
|
|
|
|
return format_html(
|
|
|
|
|
u'<a href="%s">%s</a>' %
|
|
|
|
|
(link, obj.author.nickname if obj.author.nickname else obj.author.email))
|
|
|
|
|
|
|
|
|
|
# zy: 自定义方法 - 生成文章链接
|
|
|
|
|
def link_to_article(self, obj):
|
|
|
|
|
# zy: 获取文章模型的app名称和模型名称
|
|
|
|
|
info = (obj.article._meta.app_label, obj.article._meta.model_name)
|
|
|
|
|
# zy: 生成文章编辑页面的URL
|
|
|
|
|
link = reverse('admin:%s_%s_change' % info, args=(obj.article.id,))
|
|
|
|
|
# zy: 返回HTML链接,显示文章标题
|
|
|
|
|
return format_html(
|
|
|
|
|
u'<a href="%s">%s</a>' % (link, obj.article.title))
|
|
|
|
|
|
|
|
|
|
# zy: 设置自定义方法在列表页显示的列标题(支持国际化)
|
|
|
|
|
link_to_userinfo.short_description = _('User')
|
|
|
|
|
link_to_article.short_description = _('Article')
|
|
|
|
|
link_to_article.short_description = _('Article')
|