lty/qy_lty/aiapp/admin.py
pmc 653f057b51 feat(01-02): aiapp/admin.py 注册 CredentialSlotAdmin(脱敏 + 单例新增 + 禁删)
- import 追加 CredentialSlot 与 common.utils.mask_token
- 新增 CredentialSlotAdmin(覆盖 CRED-02):
  - list_display 含计算字段 access_token_masked(仅末 4 位明文)
  - fieldsets 分「凭据信息」明文可写 + 「元数据」updated_at 只读折叠
  - has_add_permission 已存在记录时返回 False(隐藏增加按钮)
  - has_delete_permission 永远返回 False(含批量动作)
- 不修改既有 BotAdmin / ChatMessage 注册块
2026-05-07 17:42:36 +08:00

54 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from django.contrib import admin
from .models import Bot, ChatMessage, CredentialSlot
from common.utils import mask_token
@admin.register(Bot)
class BotAdmin(admin.ModelAdmin):
list_display = ('id', 'name', 'description')
search_fields = ('id', 'name', 'description')
@admin.register(ChatMessage)
class BotAdmin(admin.ModelAdmin):
list_display = ('id', 'user', 'bot', 'message', 'timestamp', 'sender', 'message_type')
search_fields = ('id', 'user', 'bot', 'message', 'timestamp', 'sender', 'message_type')
@admin.register(CredentialSlot)
class CredentialSlotAdmin(admin.ModelAdmin):
"""通用凭据槽位 Admin单例— Milestone v1.0 / Phase 1
UX 行为:
- 列表 / 查看态 access_token 显示末 4 位掩码
- 编辑表单 access_token 明文(运营录入需要)
- 已存在记录时隐藏「增加」按钮
- 永远禁止删除(防运营误操作丢失单例)
"""
list_display = ('id', 'app_id', 'access_token_masked', 'updated_at')
readonly_fields = ('updated_at',)
fieldsets = (
('凭据信息', {
'fields': ('app_id', 'access_token'),
'description': '第三方服务商分配的 APP ID + Access Token保存后立即对手机端 / 设备端生效',
}),
('元数据', {
'fields': ('updated_at',),
'classes': ('collapse',),
}),
)
def access_token_masked(self, obj):
return mask_token(obj.access_token)
access_token_masked.short_description = 'Access Token (脱敏)'
def has_add_permission(self, request):
# 已存在记录时隐藏「增加」,配合 has_delete_permission 强制单例
return not CredentialSlot.objects.exists()
def has_delete_permission(self, request, obj=None):
# 永远禁止删除(含批量动作)
return False