from rest_framework import serializers
from .models import Questioncat, Question



class SubjectSerializer(serializers.ModelSerializer):
    """
    学科分类序列化
    """
    create_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False, read_only=True)
    update_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False, read_only=True)
    class Meta:
        model = Questioncat
        fields = ('id', 'name', 'create_time', 'update_time')
    
    def create(self, validated_data):
        validated_data['is_subject'] = True
        validated_data['pid'] = None
        return Questioncat.objects.create(**validated_data)



class QuestioncatSerializerDefault(serializers.ModelSerializer):
    """
    题库分类序列化
    """
    create_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False, read_only=True)
    update_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False, read_only=True)
    subject_name = serializers.StringRelatedField(source='pid')
    class Meta:
        model = Questioncat
        fields = '__all__'

class QuestioncatSerializer(serializers.ModelSerializer):
    """
    题库分类序列化
    """
    create_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False, read_only=True)
    update_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False, read_only=True)
    subject_name = serializers.StringRelatedField(source='pid')
    tmtotal = serializers.ReadOnlyField()
    class Meta:
        model = Questioncat
        fields = '__all__'


class QuestionSerializer(serializers.ModelSerializer):
    """
    题目序列化
    """
    # level_display = serializers.CharField(source='get_level_display')
    # type_display = serializers.CharField(source='get_type_display')
    questioncat_name = serializers.StringRelatedField(source='questioncat')
    create_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False, read_only=True)
    update_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False, read_only=True)

    class Meta:
        model = Question
        fields = '__all__'
        
