67 lines
2.2 KiB
Python
67 lines
2.2 KiB
Python
from rest_framework.views import APIView
|
|
from rest_framework.response import Response
|
|
from rest_framework import authentication, permissions, status
|
|
from rest_framework.authentication import SessionAuthentication
|
|
from rest_framework.renderers import JSONRenderer
|
|
from django.core import serializers
|
|
from .models import BlockModel
|
|
|
|
class BlockView(APIView):
|
|
permission_classes = (permissions.IsAuthenticated,)
|
|
authentication_classes = (SessionAuthentication,)
|
|
|
|
def get(self, request, pk):
|
|
block = BlockModel.objects.filter(pk=pk)
|
|
if (block.exists()):
|
|
return Response(serializers.serialize("json", block), status=status.HTTP_200_OK)
|
|
else:
|
|
return Response("Not Found", status=status.HTTP_404_NOT_FOUND)
|
|
|
|
|
|
class BlocksView(APIView):
|
|
permission_classes = (permissions.IsAuthenticated,)
|
|
authentication_classes = (SessionAuthentication,)
|
|
|
|
def get(self, request):
|
|
blocks = BlockModel.objects.filter(blocker=request.user.pk)
|
|
if (blocks):
|
|
return Response(serializers.serialize("json", BlockModel.objects.filter(blocker=request.user.pk)), status=status.HTTP_200_OK)
|
|
else:
|
|
return Response({}, status=status.HTTP_404_NOT_FOUND)
|
|
|
|
def post(self, request):
|
|
data: dict = request.data
|
|
users_id = request.data.get("users_id", None)
|
|
|
|
if (users_id == None):
|
|
return Response({"Error"}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
if (BlockModel.objects.filter(blocker=users_id[0], blocked=users_id[1])):
|
|
return Response({"Already Exist"}, status=status.HTTP_409_CONFLICT)
|
|
|
|
new_block = BlockModel()
|
|
new_block.blocker = users_id[0]
|
|
new_block.blocked = users_id[1]
|
|
new_block.save()
|
|
|
|
return Response({"block_id": new_block.pk}, status=status.HTTP_201_CREATED)
|
|
|
|
def delete(self, request):
|
|
data: dict = request.data
|
|
users_id = request.data.get("users_id", None)
|
|
|
|
if (users_id == None):
|
|
return Response({"Error"}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
block = BlockModel.objects.filter(blocker=users_id[0], blocked=users_id[1])
|
|
|
|
print(list(block))
|
|
if (block.count() > 1):
|
|
return Response("Not normal >:[", status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
|
|
|
if (not block):
|
|
return Response("Don't exist", status=status.HTTP_404_NOT_FOUND)
|
|
|
|
block.delete()
|
|
return Response("Deleted", status=status.HTTP_200_OK)
|