chat qo'shildi
This commit is contained in:
68
core/apps/chat/serializers/chat/ChatRoom.py
Normal file
68
core/apps/chat/serializers/chat/ChatRoom.py
Normal file
@@ -0,0 +1,68 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
from core.apps.chat.models import ChatroomModel
|
||||
|
||||
|
||||
class MemberSerializer(serializers.Serializer):
|
||||
id = serializers.IntegerField()
|
||||
full_name = serializers.SerializerMethodField()
|
||||
role = serializers.CharField()
|
||||
|
||||
def get_full_name(self, obj):
|
||||
name = obj.get_full_name().strip()
|
||||
return name if name else str(obj.phone)
|
||||
|
||||
|
||||
class BaseChatroomSerializer(serializers.ModelSerializer):
|
||||
members = serializers.SerializerMethodField()
|
||||
|
||||
def get_members(self, obj):
|
||||
return [
|
||||
{
|
||||
"id": u.id,
|
||||
"full_name": (u.get_full_name().strip() or str(u.phone)),
|
||||
"role": u.role,
|
||||
}
|
||||
for u in obj.members.only("id", "first_name", "last_name", "phone", "role")
|
||||
]
|
||||
|
||||
class Meta:
|
||||
model = ChatroomModel
|
||||
fields = [
|
||||
"id",
|
||||
"type",
|
||||
"auto_evaluation",
|
||||
"members",
|
||||
"created_at",
|
||||
]
|
||||
|
||||
|
||||
class ListChatroomSerializer(BaseChatroomSerializer):
|
||||
class Meta(BaseChatroomSerializer.Meta): ...
|
||||
|
||||
|
||||
class RetrieveChatroomSerializer(BaseChatroomSerializer):
|
||||
class Meta(BaseChatroomSerializer.Meta): ...
|
||||
|
||||
|
||||
class CreateChatroomSerializer(serializers.ModelSerializer):
|
||||
type = serializers.ChoiceField(
|
||||
choices=["auto_evaluation", "direct"],
|
||||
required=True,
|
||||
)
|
||||
|
||||
class Meta:
|
||||
model = ChatroomModel
|
||||
fields = [
|
||||
"type",
|
||||
"auto_evaluation",
|
||||
"members",
|
||||
]
|
||||
|
||||
def validate(self, attrs):
|
||||
room_type = attrs.get("type")
|
||||
if room_type == "auto_evaluation" and not attrs.get("auto_evaluation"):
|
||||
raise serializers.ValidationError(
|
||||
{"auto_evaluation": "auto_evaluation turi uchun AutoEvaluation majburiy."}
|
||||
)
|
||||
return attrs
|
||||
Reference in New Issue
Block a user