2025-05-19 18:23:59 +08:00
|
|
|
from rest_framework import serializers
|
2025-05-30 11:49:59 +08:00
|
|
|
from .models import Template
|
2025-05-19 18:23:59 +08:00
|
|
|
|
|
|
|
class TemplateListSerializer(serializers.ModelSerializer):
|
|
|
|
"""模板列表序列化器(简化版)"""
|
|
|
|
mission_display = serializers.CharField(source='get_mission_display', read_only=True)
|
|
|
|
platform_display = serializers.CharField(source='get_platform_display', read_only=True)
|
|
|
|
collaboration_type_display = serializers.CharField(source='get_collaboration_type_display', read_only=True)
|
|
|
|
service_display = serializers.CharField(source='get_service_display', read_only=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Template
|
|
|
|
fields = [
|
2025-05-30 11:49:59 +08:00
|
|
|
'id', 'title', 'preview',
|
2025-05-19 18:23:59 +08:00
|
|
|
'mission', 'mission_display',
|
|
|
|
'platform', 'platform_display',
|
|
|
|
'service', 'service_display',
|
|
|
|
'collaboration_type', 'collaboration_type_display',
|
|
|
|
'is_public', 'created_at', 'updated_at'
|
|
|
|
]
|
|
|
|
read_only_fields = ['created_at', 'updated_at', 'preview']
|
|
|
|
|
|
|
|
class TemplateDetailSerializer(serializers.ModelSerializer):
|
|
|
|
"""模板详情序列化器"""
|
|
|
|
mission_display = serializers.CharField(source='get_mission_display', read_only=True)
|
|
|
|
platform_display = serializers.CharField(source='get_platform_display', read_only=True)
|
|
|
|
collaboration_type_display = serializers.CharField(source='get_collaboration_type_display', read_only=True)
|
|
|
|
service_display = serializers.CharField(source='get_service_display', read_only=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Template
|
|
|
|
fields = [
|
2025-05-30 11:49:59 +08:00
|
|
|
'id', 'title', 'content', 'preview',
|
2025-05-19 18:23:59 +08:00
|
|
|
'mission', 'mission_display',
|
|
|
|
'platform', 'platform_display',
|
|
|
|
'service', 'service_display',
|
|
|
|
'collaboration_type', 'collaboration_type_display',
|
|
|
|
'is_public', 'created_at', 'updated_at'
|
|
|
|
]
|
|
|
|
read_only_fields = ['created_at', 'updated_at', 'preview']
|
|
|
|
|
|
|
|
class TemplateCreateUpdateSerializer(serializers.ModelSerializer):
|
|
|
|
"""模板创建和更新序列化器"""
|
|
|
|
class Meta:
|
|
|
|
model = Template
|
|
|
|
fields = [
|
2025-05-30 11:49:59 +08:00
|
|
|
'id', 'title', 'content',
|
|
|
|
'mission', 'platform',
|
2025-05-19 18:23:59 +08:00
|
|
|
'service', 'collaboration_type',
|
|
|
|
'is_public'
|
|
|
|
]
|
|
|
|
read_only_fields = ['preview']
|
|
|
|
|
|
|
|
def validate(self, data):
|
|
|
|
"""验证数据,处理测试期间可能缺失的字段"""
|
|
|
|
# 确保其他必填字段有默认值
|
|
|
|
if 'mission' not in data:
|
|
|
|
data['mission'] = 'initial_contact'
|
|
|
|
|
|
|
|
if 'platform' not in data:
|
|
|
|
data['platform'] = 'tiktok'
|
|
|
|
|
|
|
|
if 'service' not in data:
|
|
|
|
data['service'] = 'text'
|
|
|
|
|
|
|
|
if 'collaboration_type' not in data:
|
|
|
|
data['collaboration_type'] = 'paid_promotion'
|
|
|
|
|
|
|
|
if 'is_public' not in data:
|
|
|
|
data['is_public'] = True
|
|
|
|
|
|
|
|
return data
|