2023-12-19 05:27:18 -05:00
|
|
|
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
|
2023-12-20 17:48:52 -05:00
|
|
|
from rest_framework.renderers import JSONRenderer
|
2023-12-19 05:27:18 -05:00
|
|
|
from django.core import serializers
|
2023-12-20 17:48:52 -05:00
|
|
|
from .models import BlockModel
|
2023-12-19 05:27:18 -05:00
|
|
|
|
|
|
|
class BlockView(APIView):
|
|
|
|
permission_classes = (permissions.IsAuthenticated,)
|
|
|
|
authentication_classes = (SessionAuthentication,)
|
|
|
|
|
|
|
|
def get(self, request, pk):
|
2023-12-20 17:48:52 -05:00
|
|
|
block = BlockModel.objects.filter(pk=pk)
|
2023-12-28 05:32:49 -05:00
|
|
|
if (block.exists()):
|
2023-12-20 17:48:52 -05:00
|
|
|
return Response(serializers.serialize("json", block), status=status.HTTP_200_OK)
|
|
|
|
else:
|
|
|
|
return Response("Not Found", status=status.HTTP_404_NOT_FOUND)
|
2023-12-19 05:27:18 -05:00
|
|
|
|
2023-12-20 17:48:52 -05:00
|
|
|
|
|
|
|
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)
|