from rest_framework import serializers
from .models import Article, Material


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 ''

