62 lines
1.8 KiB
Python
62 lines
1.8 KiB
Python
from rest_framework import serializers
|
|
|
|
from .models import Notification
|
|
|
|
|
|
class NotificationSerializer(serializers.ModelSerializer):
|
|
type = serializers.CharField(source="notification_type", read_only=True)
|
|
unread = serializers.SerializerMethodField()
|
|
project_name = serializers.CharField(source="project.name", read_only=True)
|
|
body = serializers.SerializerMethodField()
|
|
metadata = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = Notification
|
|
fields = [
|
|
"id",
|
|
"type",
|
|
"notification_type",
|
|
"priority",
|
|
"title",
|
|
"brief",
|
|
"body",
|
|
"source",
|
|
"project",
|
|
"project_name",
|
|
"stage",
|
|
"owner_label",
|
|
"cost_label",
|
|
"related_url",
|
|
"is_read",
|
|
"unread",
|
|
"read_at",
|
|
"archived_at",
|
|
"metadata",
|
|
"created_at",
|
|
"updated_at",
|
|
]
|
|
read_only_fields = [
|
|
"id",
|
|
"type",
|
|
"project_name",
|
|
"read_at",
|
|
"archived_at",
|
|
"created_at",
|
|
"updated_at",
|
|
]
|
|
|
|
def get_unread(self, obj):
|
|
return not obj.is_read
|
|
|
|
def get_body(self, obj):
|
|
"""兼容早期 AI 失败通知:不再向普通用户回传嵌入正文的上游原始异常。"""
|
|
body = obj.body or ""
|
|
marker = "—— 第三方服务商 API 返回的原始报错 ——"
|
|
return body.split(marker, 1)[0].strip()
|
|
|
|
def get_metadata(self, obj):
|
|
"""旧通知的 api_error 也必须从普通用户 API 中移除。"""
|
|
metadata = dict(obj.metadata or {})
|
|
metadata.pop("api_error", None)
|
|
return metadata
|