Exemplo n.º 1
0
class AnswerTreeSerializer(serializers.ModelSerializer):
    author = UserSafeSerializer()
    text = MarkdownXField()
    parent = SoftField(source='parent.slug')
    descendants = serializers.SerializerMethodField()
    question = serializers.CharField(source='question.slug')

    class Meta:
        model = Answer
        fields = [
            'created',
            'slug',
            'question',
            'author',
            'parent',
            'text',
            'descendants',
        ]

    def get_descendants(self, obj):
        queryset = obj.get_first_level_descendants()
        serializer = AnswerTreeSerializer(
            queryset,
            many=True,
            context=self.context,
        )

        return serializer.data
Exemplo n.º 2
0
class AnswerRetrieveSerializer(serializers.ModelSerializer):
    author = UserNameSerializer()
    text = MarkdownXField()
    parent = SoftField(source='parent.slug')
    descendants = serializers.SerializerMethodField()

    class Meta:
        model = Answer
        fields = [
            'created',
            'slug',
            'author',
            'parent',
            'text',
            'descendants',
        ]

    def get_descendants(self, obj):
        user = self.context['request'].user
        queryset = obj.get_first_level_descendants()
        if not user.has_perm('homework.see_all_answers'):
            queryset = queryset.for_user(user)

        serializer = AnswerRetrieveSerializer(
            queryset,
            many=True,
            context=self.context,
        )

        return serializer.data
Exemplo n.º 3
0
class QuestionSerializer(serializers.ModelSerializer):
    text = MarkdownXField()

    class Meta:
        model = Question
        fields = [
            'slug',
            'name',
            'text',
        ]
Exemplo n.º 4
0
class AnswerSerializer(serializers.ModelSerializer):
    author = UserNameSerializer()
    text = MarkdownXField()
    parent = SoftField(source='parent.slug')

    class Meta:
        model = Answer
        fields = [
            'slug',
            'author',
            'parent',
            'text',
        ]