Skip to content

Instantly share code, notes, and snippets.

@mentix02
Created August 27, 2020 18:56
Show Gist options
  • Save mentix02/8222c556322d27ed5a82df406411091b to your computer and use it in GitHub Desktop.
Save mentix02/8222c556322d27ed5a82df406411091b to your computer and use it in GitHub Desktop.
class FollowRequestActionAPIView(APIView):
"""
APIView to accept or reject a FollowRequest by the person
who is being requested to act upon said request.
"""
parser_classes = (FormParser,)
permission_classes = (IsAuthenticated,)
@staticmethod
def post(request: WSGIRequest) -> Response:
try:
action = request.POST['action']
follow_request_id = request.POST['follow_request_id']
except KeyError as field:
return Response(
{'error': f'{str(field)} not provided.'},
status=status.HTTP_400_BAD_REQUEST,
)
follow_request: FollowRequest = get_object_or_404(
FollowRequest, id=follow_request_id
)
resp = {'detail': 'rejected'}
if action == '1':
resp['detail'] = 'accepted'
follow_request.accept()
else:
follow_request.reject()
return Response(resp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment