2023-12-12 04:05:13 -05:00
|
|
|
from rest_framework.views import APIView
|
|
|
|
from rest_framework.response import Response
|
2023-12-16 05:58:50 -05:00
|
|
|
from rest_framework import authentication, permissions
|
2023-12-12 04:05:13 -05:00
|
|
|
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):
|
2023-12-16 05:58:50 -05:00
|
|
|
if (ChannelModel.objects.filter(pk = pk)):
|
|
|
|
return Response("Channel already exist")
|
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-16 05:58:50 -05:00
|
|
|
return Response("Not enought members to create the channel")
|
2023-12-12 04:05:13 -05:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
2023-12-16 05:58:50 -05:00
|
|
|
return Response("Channel created")
|