chat.serializers#
Contains serialising routines.
Module Contents#
Classes#
Conversation representation. |
|
Participant representation. |
|
Serialises a Conversation instance. |
|
Maps field names to conditions. |
|
Message option representation. |
|
Message representation. |
|
Chat limits representation. |
Functions#
|
|
Attributes#
- class ConversationOnListSerializer(instance=None, data=empty, **kwargs)#
Bases:
rest_framework.serializers.ModelSerializerConversation representation.
When a field is instantiated, we store the arguments that were used, so that we can present a helpful representation of the object.
- participant_count#
- my_participant_id#
- get_participant_count(conversation)#
Count the number of participants in the conversation.
- Parameters:
conversation (Conversation) – the conversation
- Returns:
how many participants there are in the conversation
- Return type:
int
- get_my_participant_id(conversation)#
Return the participant id of the user in the conversation.
- Parameters:
conversation (Conversation) – the conversation
- Returns:
the participant id in the conversation, or None if the session is not connected to the conversation
- Return type:
str | None
- class ParticipantSerializer(instance=None, data=empty, **kwargs)#
Bases:
rest_framework.serializers.ModelSerializerParticipant representation.
When a field is instantiated, we store the arguments that were used, so that we can present a helpful representation of the object.
- class ConversationSerializer(instance=None, data=empty, **kwargs)#
Bases:
rest_framework.serializers.ModelSerializerSerialises a Conversation instance.
When a field is instantiated, we store the arguments that were used, so that we can present a helpful representation of the object.
- class Meta#
- model#
- fields = ('id', 'name', 'created_at', 'participants', 'my_participant_id')#
- read_only_fields = ['participants']#
- participants#
- my_participant_id#
- get_my_participant_id(conversation)#
Return the participant id of the user in the conversation.
- Parameters:
conversation (Conversation) – the conversation
- Returns:
the participant id in the conversation, or None if the session is not connected to the conversation
- Return type:
str | None
- class ConditionalFields#
Maps field names to conditions.
Create an empty map of field names to conditions.
- _only_type(t)#
- Parameters:
t (str) –
- Return type:
collections.abc.Callable[[Message | dict[str, Any]], bool]
- _only_not_type(t)#
- Parameters:
t (str) –
- Return type:
collections.abc.Callable[[Message | dict[str, Any]], bool]
- _only_system#
- _only_non_system#
- class MessageOptionSerializer(instance=None, data=empty, **kwargs)#
Bases:
rest_framework.serializers.ModelSerializerMessage option representation.
When a field is instantiated, we store the arguments that were used, so that we can present a helpful representation of the object.
- to_internal_value(data)#
Dict of native values <- Dict of primitive datatypes.
- Parameters:
data (str | dict[str, Any]) –
- Return type:
dict[str, Any]
- class MessageSerializer(instance=None, data=empty, **kwargs)#
Bases:
rest_framework.serializers.ModelSerializerMessage representation.
When a field is instantiated, we store the arguments that were used, so that we can present a helpful representation of the object.
- class Meta#
- model#
- fields = ('type', 'time', 'id', 'sent_by_human', 'event', 'additional_metadata', 'quoted_message_id',...#
- conditional#
- id#
- sent_by_human#
- additional_metadata#
- event#
- quoted_message_id#
- sender_id#
- file_url#
- file_size#
- file_name#
- file#
- options#
- get_file_url(message)#
Return the URL to download the attached file.
- Parameters:
message (Message) – the instance being serialised
- Returns:
the attachment URL, or
Noneif this message does not have an attached file- Return type:
str | None
- get_sent_by_human(message)#
Return True if the message sender is human.
- Returns:
True if the message was sent by a person, False otherwise
- Parameters:
message (Message) –
- Return type:
bool
- get_file_name(message)#
Return the original name of the attached file.
- Parameters:
message (Message) – the instance being serialised
- Returns:
the attachment name, or
Noneif this message does not have an attached file- Return type:
str | None
- get_additional_metadata(message)#
Return additional metadata (only for system messages).
- Parameters:
message (Message) – the instance being serialised
- Returns:
the additional metadata of the system message, or
Noneif this is not a system message- Return type:
dict[str, Any] | None
- create(validated_data)#
Create an instance based on validated data.
- Parameters:
validated_data (dict[str, Any]) – the validated instance fields
- Returns:
the created instance
- Return type:
django.db.models.Model
- to_internal_value(data)#
Dict of native values <- Dict of primitive datatypes.
- Parameters:
data (dict[str, Any]) –
- Return type:
dict[str, Any]
- to_representation(instance)#
Object instance -> Dict of primitive datatypes.
- Parameters:
instance (Message) –
- Return type:
dict[str, Any]
- classmethod create_message(message_data)#
Create a message and save it to the database.
- Parameters:
message_data (dict[str, Any]) – message parameters and data
- Raises:
ValidationError – if validation fails
- Returns:
the new instance of Message if it was saved successfully, or
Noneif it was not saved because it is duplicate (samelocal_idand sender as an existing message).- Return type:
Message | None
- class ChatLimitsSerializer(instance=None, data=empty, **kwargs)#
Bases:
rest_framework.serializers.SerializerChat limits representation.
When a field is instantiated, we store the arguments that were used, so that we can present a helpful representation of the object.
- MAX_ATTACHMENT_SIZE#
- MAX_IMAGE_SIZE#
- MAX_VIDEO_SIZE#
- MAX_AUDIO_SIZE#
- MAX_VOICE_SIZE#