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.
ReviewAndAnalyzeOpenSourceS.../comments/admin.py

81 lines
3.3 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.

# 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', # 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')
# 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')
# 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')