Skip to content

Instantly share code, notes, and snippets.

@mentix02
Last active August 28, 2020 11:17
Show Gist options
  • Save mentix02/ac9098f527ef51051a548837d3f655e1 to your computer and use it in GitHub Desktop.
Save mentix02/ac9098f527ef51051a548837d3f655e1 to your computer and use it in GitHub Desktop.
from django.shortcuts import get_object_or_404
from django.contrib.auth import get_user_model
from django.core.handlers.wsgi import WSGIRequest
from rest_framework import status
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.parsers import FormParser
from rest_framework.permissions import IsAuthenticated
User = get_user_model()
class FollowUserAPIView(APIView):
"""
APIView to make a request (or directly follow is user to be followed
has a public account) by an authenticated user.
"""
parser_classes = (FormParser,)
permission_classes = (IsAuthenticated,)
@staticmethod
def post(request: WSGIRequest) -> Response:
to_follow_id = request.POST.get('to_follow_id')
if not to_follow_id:
return Response(
{'error': 'Follow request\'s user ID not provided.'},
status=status.HTTP_400_BAD_REQUEST,
)
user = get_object_or_404(User, id=to_follow_id)
request.user.follow(user)
return Response({'detail': 'requested'})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment