forum
This commit is contained in:
@@ -2,7 +2,7 @@ from django.contrib import admin
|
||||
from django.utils.html import format_html
|
||||
from unfold.admin import ModelAdmin, TabularInline
|
||||
from unfold.decorators import display
|
||||
from .models import Activity, ActivitySignup, Topic, Reply, TopicMedia
|
||||
from .models import Activity, ActivitySignup, Topic, Reply, TopicMedia, Announcement
|
||||
|
||||
class ActivitySignupInline(TabularInline):
|
||||
model = ActivitySignup
|
||||
@@ -24,7 +24,7 @@ class ReplyInline(TabularInline):
|
||||
class TopicMediaInline(TabularInline):
|
||||
model = TopicMedia
|
||||
extra = 0
|
||||
fields = ('file', 'media_type', 'created_at')
|
||||
fields = ('file', 'file_url', 'media_type', 'created_at')
|
||||
readonly_fields = ('created_at',)
|
||||
can_delete = True
|
||||
|
||||
@@ -91,15 +91,15 @@ class ActivitySignupAdmin(ModelAdmin):
|
||||
|
||||
@admin.register(Topic)
|
||||
class TopicAdmin(ModelAdmin):
|
||||
list_display = ('title', 'author', 'get_related_item', 'reply_count', 'view_count', 'is_pinned', 'created_at')
|
||||
list_filter = ('is_pinned', 'created_at', 'related_product', 'related_service', 'related_course')
|
||||
list_display = ('title', 'category', 'author', 'get_related_item', 'reply_count', 'view_count', 'is_pinned', 'created_at')
|
||||
list_filter = ('category', 'is_pinned', 'created_at', 'related_product', 'related_service', 'related_course')
|
||||
search_fields = ('title', 'content', 'author__nickname')
|
||||
autocomplete_fields = ['author', 'related_product', 'related_service', 'related_course']
|
||||
inlines = [TopicMediaInline, ReplyInline]
|
||||
|
||||
fieldsets = (
|
||||
('帖子内容', {
|
||||
'fields': ('title', 'content', 'is_pinned')
|
||||
'fields': ('title', 'category', 'content', 'is_pinned')
|
||||
}),
|
||||
('关联信息', {
|
||||
'fields': ('author', 'related_product', 'related_service', 'related_course'),
|
||||
@@ -157,6 +157,63 @@ class TopicMediaAdmin(ModelAdmin):
|
||||
|
||||
@display(description="预览")
|
||||
def file_preview(self, obj):
|
||||
if obj.media_type == 'image':
|
||||
return format_html('<img src="{}" height="50" style="border-radius: 4px;" />', obj.file.url)
|
||||
return obj.file.name
|
||||
url = ""
|
||||
if obj.file:
|
||||
url = obj.file.url
|
||||
elif obj.file_url:
|
||||
url = obj.file_url
|
||||
|
||||
if obj.media_type == 'image' and url:
|
||||
return format_html('<img src="{}" height="50" style="border-radius: 4px;" />', url)
|
||||
return obj.file.name or "外部文件"
|
||||
|
||||
@admin.register(Announcement)
|
||||
class AnnouncementAdmin(ModelAdmin):
|
||||
list_display = ('title', 'image_preview', 'active_label', 'pinned_label', 'priority', 'start_time', 'end_time', 'created_at')
|
||||
list_filter = ('is_active', 'is_pinned', 'created_at')
|
||||
search_fields = ('title', 'content')
|
||||
|
||||
fieldsets = (
|
||||
('公告信息', {
|
||||
'fields': ('title', 'content', 'link_url')
|
||||
}),
|
||||
('图片设置', {
|
||||
'fields': ('image', 'image_url'),
|
||||
'description': '上传图片或填写图片链接,优先显示上传的图片'
|
||||
}),
|
||||
('显示设置', {
|
||||
'fields': ('is_active', 'is_pinned', 'priority'),
|
||||
'classes': ('tab',)
|
||||
}),
|
||||
('排期设置', {
|
||||
'fields': ('start_time', 'end_time'),
|
||||
'classes': ('tab',)
|
||||
}),
|
||||
)
|
||||
|
||||
@display(description="图片预览")
|
||||
def image_preview(self, obj):
|
||||
url = obj.display_image_url
|
||||
if url:
|
||||
return format_html('<img src="{}" height="50" style="border-radius: 4px;" />', url)
|
||||
return "无图片"
|
||||
|
||||
@display(
|
||||
description="状态",
|
||||
label={
|
||||
True: "success",
|
||||
False: "danger",
|
||||
}
|
||||
)
|
||||
def active_label(self, obj):
|
||||
return obj.is_active
|
||||
|
||||
@display(
|
||||
description="置顶",
|
||||
label={
|
||||
True: "warning",
|
||||
False: "default",
|
||||
}
|
||||
)
|
||||
def pinned_label(self, obj):
|
||||
return obj.is_pinned
|
||||
|
||||
Reference in New Issue
Block a user