L
L
Luke LikeSkywalker2020-02-17 05:23:46
Django
Luke LikeSkywalker, 2020-02-17 05:23:46

How to rewrite generic CBV style view in DRF?

How can this functional view be rewritten in generic CBV style

# Like comment
@api_view(['PUT'])
@authentication_classes([SessionAuthentication, BasicAuthentication])
@permission_classes([IsAuthenticated])
def like_comment_api(request, story_id, comment_id):
    if request.method == 'PUT':
        author = request.user
        story = Story.objects.get(pk=story_id)
        comment = story.comments.get(pk=comment_id)
        like = CommentLike.objects.filter(author=author, comment=comment).exists()
        if like is False:
            comment_like = CommentLikeSerializer(data=request.data)
            if comment_like.is_valid():
                comment_like.save(author=author, comment=comment)
                return Response(comment_like.data, status=201)
            return Response(comment_like.data, status=203)
        comment_like = CommentLike.objects.get(author=author, comment=comment)
        comment_like.delete()
        return Response(status=202)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question