chat.serializers#

Contains serialising routines.

Module Contents#

Classes#

ConversationOnListSerializer

Conversation representation.

ParticipantSerializer

Participant representation.

ConversationSerializer

Serialises a Conversation instance.

ConditionalFields

Maps field names to conditions.

MessageOptionSerializer

Message option representation.

MessageSerializer

Message representation.

ChatLimitsSerializer

Chat limits representation.

Functions#

Attributes#

class ConversationOnListSerializer(instance=None, data=empty, **kwargs)#

Bases: rest_framework.serializers.ModelSerializer

Conversation 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 = ('id', 'name', 'created_at', 'participant_count', 'my_participant_id')#
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.ModelSerializer

Participant 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 = ('id', 'name', 'type')#
read_only_fields = ('id', 'type')#
class ConversationSerializer(instance=None, data=empty, **kwargs)#

Bases: rest_framework.serializers.ModelSerializer

Serialises 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.

__call__(field_name, condition)#

Add a field name and its condition.

Parameters:
  • field_name (str) – name of the field

  • condition (collections.abc.Callable[[Message | dict[str, Any]], bool]) – function that returns whether the field should be used for a given instance

Return type:

None

__getitem__(field_name)#
Parameters:

field_name (str) –

Return type:

collections.abc.Callable[[Message | dict[str, Any]], bool]

_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#
_only_with_options(m)#
Parameters:

m (Message | dict[str, Any]) –

Return type:

bool

_only_with_file(m)#
Parameters:

m (Message | dict[str, Any]) –

Return type:

bool

class MessageOptionSerializer(instance=None, data=empty, **kwargs)#

Bases: rest_framework.serializers.ModelSerializer

Message 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.

class Meta#
model#
fields = ('option_text', 'position')#
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.ModelSerializer

Message 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 None if 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 None if 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 None if 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 None if it was not saved because it is duplicate (same local_id and sender as an existing message).

Return type:

Message | None

class ChatLimitsSerializer(instance=None, data=empty, **kwargs)#

Bases: rest_framework.serializers.Serializer

Chat 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#