ft_transcendence/chat/views.py
2023-12-12 10:05:13 +01:00

30 lines
926 B
Python

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import authentication, permissions
from rest_framework.authentication import SessionAuthentication
from .models import ChannelModel, MemberModel, MessageModel
class ChatView(APIView):
permission_classes = (permissions.IsAuthenticated,)
authentication_classes = (SessionAuthentication,)
def post(self, request, pk):
if (ChannelModel.objects.filter(pk = pk)):
return Response("Channel already exist")
data: dict = request.data
users_id = request.data.get("users_id", [])
if len(users_id) < 2:
return Response("Not enought members to create the channel")
new_channel = ChannelModel()
new_channel.save()
for user_id in users_id:
new_member = MemberModel()
new_member.channel_id = new_channel.pk
new_member.member_id = user_id
new_member.save()
return Response("Channel created")