Esempio n. 1
0
class AnswerSerializer(ModelSerializer):
    writer = UserOtherDetailsSerializer(read_only=True)
    question = QuestionSerializer()
    no_of_comments = SerializerMethodField()

    class Meta:
        model = Answer
        fields = [
            'id',
            'question',
            'writer',
            'date_written',
            'time_written',
            'body',
            'no_of_upvotes',
            'no_of_downvotes',
            'no_of_views',
            'no_of_comments',
        ]

    def get_no_of_comments(self, obj):
        try:
            return obj.getComments()
        except:
            return 0
Esempio n. 2
0
    def get(self, request):
        search_term = request.GET.get('search_term')
        sort_type = int(request.GET.get('sort_type'))
        page = int(request.GET['page'])

        splited_term = search_term.split()

        if sort_type == 1:
            q = Question.objects.filter(
                Q(title__icontains=search_term)
                | Q(question_details__icontains=search_term)).distinct(
                ).order_by('-no_of_answers')
            pagination_class = Paginator(q, 5)
            q = pagination_class.page(page)
            serializer = QuestionSerializer(q, many=True)
            return Response(serializer.data)

        if sort_type == 2:
            p = UserOtherDetails.objects.filter(
                Q(user__username__icontains=search_term)
                | Q(user__first_name__icontains=search_term)
                | Q(user__last_name__icontains=search_term)).distinct()
            pagination_class = Paginator(p, 2)
            p = pagination_class.page(page)
            serializer = UserOtherDetailsSerializer(p, many=True)
            return Response(serializer.data)

        if sort_type == 3:
            t = Topic.objects.filter(
                Q(title__icontains=search_term)
                | Q(desc__icontains=search_term)).distinct()
            pagination_class = Paginator(t, 3)
            t = pagination_class.page(page)
            serializer = TopicSerializer(t, many=True)
            return Response(serializer.data)
        return Response(None)
Esempio n. 3
0
class CommentDetailsSerializer(ModelSerializer):
    writer = UserOtherDetailsSerializer(read_only=True)
    parent_answer = SerializerMethodField()
    replies = HyperlinkedIdentityField(view_name='children_comment')
    replies_count = SerializerMethodField()

    class Meta:
        model = Comment
        fields = [
            'id',
            'writer',
            'body',
            'no_of_upvotes',
            'timestamp',
            'replies',
            'replies_count',
            'parent_answer',
        ]
        read_only_fields = [
            'no_of_upvotes',
        ]

    def get_replies(self, obj):
        if obj.is_parent:
            return CommentChildSerializer(obj.children(), many=True).data
        return None

    def get_replies_count(self, obj):
        if obj.is_parent:
            return obj.children().count()
        return 0

    def get_parent_answer(self, obj):
        if obj.parent_answer is not None:
            return CommentParentAnswerSerializer(obj.parent_answer).data
        return None
Esempio n. 4
0
class AnswerSerializer(ModelSerializer):
    writer = UserOtherDetailsSerializer(read_only=True)
    question = QuestionSerializer()
    no_of_comments = SerializerMethodField()
    upvoted = SerializerMethodField()
    downvoted = SerializerMethodField()
    archived = SerializerMethodField()
    thanked = SerializerMethodField()
    edit_suggested = SerializerMethodField()

    class Meta:
        model = Answer
        fields = [
            'id',
            'question',
            'writer',
            'date_written',
            'time_written',
            'body',
            'no_of_upvotes',
            'no_of_downvotes',
            'no_of_views',
            'no_of_comments',
            'upvoted',
            'downvoted',
            'archived',
            'thanked',
            'edit_suggested',
            'anonymous',
        ]

    def get_no_of_comments(self, obj):
        try:
            return obj.getComments()
        except:
            return 0

    def get_upvoted(self, obj):
        try:
            return obj.isUpvoted(self.context.get('request').user)
        except:
            return False

    def get_downvoted(self, obj):
        try:
            return obj.isDownvoted(self.context.get('request').user)
        except:
            return False

    def get_archived(self, obj):
        try:
            return obj.isArchived(self.context.get('request').user)
        except:
            return False

    def get_thanked(self, obj):
        try:
            return obj.hasThanked(self.context.get('request').user)
        except:
            return False

    def get_edit_suggested(self, obj):
        try:
            return obj.hasSuggested(self.context.get('request').user)
        except:
            return False
Esempio n. 5
0
class TopicFollowingSerializer(serializers.ModelSerializer):
    user = UserOtherDetailsSerializer(read_only=True)

    class Meta:
        model = TopicFollowing
        fields = ['user']
Esempio n. 6
0
class CommentParentAnswerSerializer(ModelSerializer):
    writer = UserOtherDetailsSerializer(read_only=True)

    class Meta:
        model = Answer
        fields = ['writer', 'body']