ft_transcendence/chat/serializers.py

30 lines
1.0 KiB
Python
Raw Normal View History

2023-12-12 04:05:13 -05:00
from rest_framework import serializers
2023-12-27 10:14:39 -05:00
from profiles.models import ProfileModel
from .models import ChatChannelModel, ChatMessageModel
class ChatChannelSerializer(serializers.ModelSerializer):
members_id = serializers.ListField(child = serializers.IntegerField())
class Meta:
model = ChatChannelModel
fields = ["members_id", "pk"]
def validate_members_id(self, value):
members_id: [int] = value
if len(members_id) < 2:
raise serializers.ValidationError('Not enought members to create the channel')
if len(set(members_id)) != len(members_id):
raise serializers.ValidationError('Same member')
for member_id in members_id:
if not ProfileModel.objects.filter(pk = member_id).exists():
raise serializers.ValidationError(f"The profile {member_id} doesn't exists.")
return members_id
class ChatMessageSerializer(serializers.Serializer):
2023-12-12 04:05:13 -05:00
class Meta:
2023-12-27 10:14:39 -05:00
model = ChatMessageModel
fields = ["channel_id", "author_id", "content", "time"]