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.
195 lines
7.5 KiB
195 lines
7.5 KiB
from django import forms
|
|
from django.contrib import admin
|
|
from django.contrib.auth import get_user_model
|
|
from django.urls import reverse
|
|
from django.utils.html import format_html
|
|
from django.utils.translation import gettext_lazy as _
|
|
from django.core.exceptions import ValidationError
|
|
|
|
# 导入 blog 应用下的所有模型
|
|
from .models import (
|
|
Article, Category, Tag, Links, SideBar, BlogSettings,
|
|
UserProfile, Favorite # 确保导入了 UserProfile 和 Favorite
|
|
)
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Article Admin
|
|
# ------------------------------------------------------------------------------
|
|
class ArticleForm(forms.ModelForm):
|
|
class Meta:
|
|
model = Article
|
|
fields = '__all__'
|
|
|
|
def make_article_publish(modeladmin, request, queryset):
|
|
queryset.update(status='p')
|
|
make_article_publish.short_description = _('Publish selected articles')
|
|
|
|
def draft_article(modeladmin, request, queryset):
|
|
queryset.update(status='d')
|
|
draft_article.short_description = _('Set selected articles to draft')
|
|
|
|
def close_article_commentstatus(modeladmin, request, queryset):
|
|
queryset.update(comment_status='c')
|
|
close_article_commentstatus.short_description = _('Close comments for selected articles')
|
|
|
|
def open_article_commentstatus(modeladmin, request, queryset):
|
|
queryset.update(comment_status='o')
|
|
open_article_commentstatus.short_description = _('Open comments for selected articles')
|
|
|
|
@admin.register(Article)
|
|
class ArticleAdmin(admin.ModelAdmin):
|
|
list_per_page = 20
|
|
search_fields = ('body', 'title')
|
|
form = ArticleForm
|
|
list_display = (
|
|
'id', 'title', 'author', 'link_to_category',
|
|
'pub_time', 'views', 'status', 'type', 'article_order'
|
|
)
|
|
list_display_links = ('id', 'title')
|
|
list_filter = ('status', 'type', 'category', 'tags')
|
|
filter_horizontal = ('tags',)
|
|
# 使用 fieldsets 来组织编辑页面的字段布局,更清晰
|
|
fieldsets = (
|
|
(_('Basic Information'), {
|
|
'fields': ('title', 'author', 'category', 'tags', 'status', 'type')
|
|
}),
|
|
(_('Content'), {
|
|
'fields': ('body',)
|
|
}),
|
|
(_('Settings'), {
|
|
'fields': ('article_order', 'show_toc', 'comment_status'),
|
|
'classes': ('collapse',) # 默认折叠
|
|
}),
|
|
)
|
|
exclude = ('creation_time', 'last_modify_time')
|
|
view_on_site = True
|
|
actions = [
|
|
make_article_publish, draft_article,
|
|
close_article_commentstatus, open_article_commentstatus
|
|
]
|
|
|
|
def link_to_category(self, obj):
|
|
if obj.category:
|
|
info = (obj.category._meta.app_label, obj.category._meta.model_name)
|
|
link = reverse('admin:%s_%s_change' % info, args=(obj.category.id,))
|
|
return format_html(u'<a href="{}">{}</a>', link, obj.category.name)
|
|
return _('None')
|
|
link_to_category.short_description = _('Category')
|
|
|
|
def get_form(self, request, obj=None, **kwargs):
|
|
form = super().get_form(request, obj,** kwargs)
|
|
# 限制作者只能是超级管理员
|
|
form.base_fields['author'].queryset = get_user_model().objects.filter(is_superuser=True)
|
|
return form
|
|
|
|
def get_view_on_site_url(self, obj=None):
|
|
if obj:
|
|
return obj.get_full_url()
|
|
return super().get_view_on_site_url(obj)
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Category Admin
|
|
# ------------------------------------------------------------------------------
|
|
@admin.register(Category)
|
|
class CategoryAdmin(admin.ModelAdmin):
|
|
list_display = ('id', 'name', 'parent_category', 'index')
|
|
list_display_links = ('id', 'name')
|
|
list_filter = ('parent_category',)
|
|
search_fields = ('name',)
|
|
exclude = ('slug', 'last_mod_time', 'creation_time')
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Tag Admin
|
|
# ------------------------------------------------------------------------------
|
|
@admin.register(Tag)
|
|
class TagAdmin(admin.ModelAdmin):
|
|
list_display = ('id', 'name')
|
|
list_display_links = ('id', 'name')
|
|
search_fields = ('name',)
|
|
exclude = ('slug', 'last_mod_time', 'creation_time')
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Links Admin
|
|
# ------------------------------------------------------------------------------
|
|
@admin.register(Links)
|
|
class LinksAdmin(admin.ModelAdmin):
|
|
list_display = ('id', 'name', 'link', 'sequence', 'is_enable', 'show_type')
|
|
list_display_links = ('id', 'name')
|
|
list_filter = ('is_enable', 'show_type')
|
|
search_fields = ('name', 'link')
|
|
exclude = ('last_mod_time', 'creation_time')
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# SideBar Admin
|
|
# ------------------------------------------------------------------------------
|
|
@admin.register(SideBar)
|
|
class SideBarAdmin(admin.ModelAdmin):
|
|
list_display = ('id', 'name', 'is_enable', 'sequence')
|
|
list_display_links = ('id', 'name')
|
|
list_filter = ('is_enable',)
|
|
search_fields = ('name', 'content')
|
|
exclude = ('last_mod_time', 'creation_time')
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# BlogSettings Admin (Singleton Pattern)
|
|
# ------------------------------------------------------------------------------
|
|
@admin.register(BlogSettings)
|
|
class BlogSettingsAdmin(admin.ModelAdmin):
|
|
list_display = ('id', 'site_name')
|
|
exclude = ('last_mod_time', 'creation_time')
|
|
|
|
def has_add_permission(self, request):
|
|
"""
|
|
限制只能有一个配置实例
|
|
如果已经存在一条记录,则禁用“添加”按钮
|
|
"""
|
|
if BlogSettings.objects.exists():
|
|
return False
|
|
return True
|
|
|
|
def save_model(self, request, obj, form, change):
|
|
"""
|
|
确保始终只有一个配置实例
|
|
"""
|
|
if not change and BlogSettings.objects.exists():
|
|
raise ValidationError(_('There can be only one Blog Settings instance.'))
|
|
super().save_model(request, obj, form, change)
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# UserProfile Admin
|
|
# ------------------------------------------------------------------------------
|
|
@admin.register(UserProfile)
|
|
class UserProfileAdmin(admin.ModelAdmin):
|
|
list_display = ('id', 'user', 'created_at')
|
|
list_display_links = ('id', 'user')
|
|
list_filter = ('created_at',)
|
|
search_fields = ('user__username', 'user__email', 'bio')
|
|
fieldsets = (
|
|
(_('User'), {
|
|
'fields': ('user',)
|
|
}),
|
|
(_('Profile Information'), {
|
|
'fields': ('bio', 'avatar')
|
|
}),
|
|
(_('Social Links'), {
|
|
'fields': ('website', 'github', 'twitter', 'weibo'),
|
|
'classes': ('collapse',)
|
|
}),
|
|
)
|
|
readonly_fields = ('created_at', 'updated_at') # 时间戳设为只读
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Favorite Admin (Optional)
|
|
# ------------------------------------------------------------------------------
|
|
@admin.register(Favorite)
|
|
class FavoriteAdmin(admin.ModelAdmin):
|
|
list_display = ('id', 'user', 'article', 'created_at')
|
|
list_display_links = ('id',)
|
|
list_filter = ('created_at',)
|
|
search_fields = ('user__username', 'article__title')
|
|
readonly_fields = ('created_at',)
|
|
# 通常不希望管理员手动创建或修改收藏,所以可以禁用相关权限
|
|
def has_add_permission(self, request):
|
|
return False
|
|
def has_change_permission(self, request, obj=None):
|
|
return False |