from rest_framework import serializers
from .models import Article, Material, Source, Threshold
from utils.constants import EXCLUDE_FIELDS_BASE


class ArticelSerializer(serializers.ModelSerializer):
    """
    文章序列化
    """
    class Meta:
        model = Article
        fields = '__all__'

class ArticelListSerializer(serializers.ModelSerializer):
    """
    文章列表序列化
    """
    class Meta:
        model = Article
        exclude = ('content',)

class MaterialSerializer(serializers.ModelSerializer):
    vid = serializers.SerializerMethodField(read_only=True)
    class Meta:
        model = Material
        fields = '__all__'

    def get_vid(self, obj):
        if obj.type == '视频':
            return obj.path.split('/')[-1].replace('.html', '')
        else:
            return ''

class SourceSerializer(serializers.ModelSerializer):
    class Meta:
        model = Source
        fields = '__all__'
        read_only_fields = EXCLUDE_FIELDS_BASE


class ThresholdSerializer(serializers.ModelSerializer):
    source_ = SourceSerializer(source='source', read_only=True)
    class Meta:
        model = Threshold
        fields = '__all__'
        read_only_fields = EXCLUDE_FIELDS_BASE


class PathSerializer(serializers.Serializer):
    path = serializers.CharField(label='文件地址')