Last active
August 30, 2020 11:14
-
-
Save mentix02/b97e1f78aaf6dec744e478899f189f49 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import IntegrityError | |
from django.shortcuts import get_object_or_404 | |
from rest_framework import status | |
from rest_framework.views import APIView | |
from rest_framework.response import Response | |
from rest_framework.permissions import IsAuthenticated | |
from post.models import Post | |
class PostLikeAPIView(APIView): | |
permission_classes = (IsAuthenticated,) | |
@staticmethod | |
def post(request) -> Response: | |
user = request.user | |
post_id = request.POST.get('post_id', False) | |
if not post_id: | |
return Response( | |
{'error': 'post_id required'}, status=status.HTTP_400_BAD_REQUEST | |
) | |
post = get_object_or_404(Post, id=post_id) | |
try: | |
if ( | |
not post.user.private | |
or post.user == user | |
or post.id | |
in Post.objects.filter( | |
user_id__in=user.following.values_list('id', flat=True) | |
).values_list('id', flat=True) | |
): | |
post.votes.up(user.id) | |
else: | |
return Response({'error': 'Not found.'}, status=status.HTTP_404_NOT_FOUND) | |
except IntegrityError: | |
return Response({'like': 'existed'}, status=status.HTTP_200_OK) | |
else: | |
return Response({'like': 'created'}, status=status.HTTP_201_CREATED) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment