ft_transcendence/chat/views.py

30 lines
926 B
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
from rest_framework import authentication, permissions
from rest_framework.authentication import SessionAuthentication
from .models import ChannelModel, MemberModel, MessageModel
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,)
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")