ft_transcendence/chat/views.py

101 lines
3.3 KiB
Python
Raw Normal View History

2023-12-12 04:05:13 -05:00
from rest_framework.views import APIView
from rest_framework.response import Response
2023-12-15 14:32:43 -05:00
from rest_framework import authentication, permissions, status
2023-12-12 04:05:13 -05:00
from rest_framework.authentication import SessionAuthentication
from .models import ChannelModel, MemberModel, MessageModel
2023-12-15 14:32:43 -05:00
from django.core import serializers
2023-11-27 17:31:31 -05:00
2023-12-12 04:05:13 -05:00
class ChatView(APIView):
permission_classes = (permissions.IsAuthenticated,)
authentication_classes = (SessionAuthentication,)
2023-12-15 14:32:43 -05:00
def get(self, request, pk):
if (ChannelModel.objects.filter(pk=pk)):
return Response({'channel_id': pk})
else:
return Response("Channel doesn't exist")
def delete(self, request, pk):
ChannelModel.objects.filter(pk=pk).delete()
MessageModel.objects.filter(pk=pk).delete()
MemberModel.objects.filter(pk=pk).delete()
return Response({'channel_id': pk})
"""
2023-12-12 04:05:13 -05:00
def post(self, request, pk):
2023-12-15 14:32:43 -05:00
channel = ChannelModel.objects.filter(pk=pk)
message = request.data.get("message", [])
print(message)
if (message == []):
return Response('No message', status=status.HTTP_400_BAD_REQUEST)
new_message = MessageModel()
new_message.channel_id = message["channel_id"]
new_message.author_id = message["author_id"]
new_message.content = message["content"]
new_message.time = message["time"]
new_message.save()
messages = MessageModel.objects.filter(channel_id=pk)
messages = serializers.serialize("json", messages)
return Response({'messages':messages}, status=status.HTTP_200_OK)
"""
class ChatsView(APIView):
def post(self, request):
2023-12-12 04:05:13 -05:00
data: dict = request.data
users_id = request.data.get("users_id", [])
if len(users_id) < 2:
2023-12-15 14:32:43 -05:00
return Response('Not enought members to create the channel', status=status.HTTP_400_BAD_REQUEST)
if users_id[0] == users_id[1]:
return Response('Same member', status=status.HTTP_400_BAD_REQUEST)
2023-12-12 04:05:13 -05:00
2023-12-15 14:32:43 -05:00
for user_id1 in users_id:
for member1 in MemberModel.objects.filter(member_id=user_id1):
for user_id2 in users_id:
if user_id1 == user_id2:
continue
for member2 in MemberModel.objects.filter(member_id=user_id2):
if (member1.channel_id == member2.channel_id):
messages = MessageModel.objects.filter(channel_id=member1.channel_id)
messages = serializers.serialize("json", messages)
return Response({'channel_id': member1.channel_id, 'messages':messages}, status=status.HTTP_200_OK)
2023-12-12 04:05:13 -05:00
new_channel = ChannelModel()
new_channel.save()
for user_id in users_id:
2023-12-15 14:32:43 -05:00
2023-12-12 04:05:13 -05:00
new_member = MemberModel()
new_member.channel_id = new_channel.pk
new_member.member_id = user_id
new_member.save()
2023-12-15 14:32:43 -05:00
return Response({'channel_id': new_channel.pk}, status=status.HTTP_201_CREATED)
def delete(self, request):
data: dict = request.data
users_id = request.data.get("users_id", [])
#print(list(MemberModel.objects.all()))
for user_id1 in users_id:
for member1 in MemberModel.objects.filter(member_id=user_id1):
for user_id2 in users_id:
if user_id1 == user_id2:
break
for member2 in MemberModel.objects.filter(member_id=user_id2):
if (member1.channel_id == member2.channel_id):
MessageModel.objects.filter(channel_id=member1.channel_id).delete()
member1.delete()
member2.delete()
ChannelModel.objects.get(pk=member1.channel_id).delete()
return Response("Channel removed", status=status.HTTP_200_OK)
return Response("Channel doesn't exist", status=status.HTTP_404_NOT_FOUND)