Models
Collection of model.
Message (Model)
django-model
Reference to message model.
Source code in chat/models.py
class Message(models.Model):
"""Reference to message model."""
id = models.UUIDField( # noqa: A003
default=uuid.uuid4,
editable=False,
verbose_name=_("id"),
primary_key=True,
)
room = models.ForeignKey(
Room,
verbose_name=_("room"),
on_delete=models.CASCADE,
related_name="messages",
db_index=True,
)
sender = models.ForeignKey(
"users.CustomUser",
verbose_name=_("sender"),
on_delete=models.CASCADE,
related_name="messages",
db_index=True,
)
content = models.TextField(verbose_name=_("content"))
created_at = models.DateTimeField(verbose_name=_("created at"), auto_now_add=True)
updated_at = models.DateTimeField(verbose_name=_("updated at"), auto_now=True)
class Meta:
"""Meta data."""
verbose_name = _("message")
verbose_name_plural = _("messages")
def __str__(self: "Message") -> str:
"""It returns readable name for the model."""
return f"{self.sender.username} in {self.room.id}"
content: TextField
django-field
content
created_at: DateTimeField
blank
django-field
created at
id: UUIDField
django-field
id
room: ForeignKey
django-field
room
sender: ForeignKey
django-field
sender
updated_at: DateTimeField
blank
django-field
updated at
Participant (Model)
django-model
Reference to participant model.
Source code in chat/models.py
class Participant(models.Model):
"""Reference to participant model."""
id = models.UUIDField( # noqa: A003
default=uuid.uuid4,
editable=False,
verbose_name=_("id"),
primary_key=True,
)
user = models.ForeignKey(
"users.CustomUser",
verbose_name=_("user"),
on_delete=models.CASCADE,
related_name="participants",
db_index=True,
)
room = models.ForeignKey(
Room,
verbose_name=_("room"),
on_delete=models.CASCADE,
related_name="participants",
db_index=True,
)
class Meta:
"""Meta data."""
verbose_name = _("participant")
verbose_name_plural = _("participants")
def __str__(self: "Participant") -> str:
"""It returns readable name for the model."""
return f"{self.user} in {self.room}"
id: UUIDField
django-field
id
room: ForeignKey
django-field
room
user: ForeignKey
django-field
user
Room (Model)
django-model
Reference to room model.
Source code in chat/models.py
class Room(models.Model):
"""Reference to room model."""
id = models.UUIDField( # noqa: A003
default=uuid.uuid4,
editable=False,
verbose_name=_("id"),
primary_key=True,
)
class Meta:
"""Meta data."""
verbose_name = _("room")
verbose_name_plural = _("rooms")
def __str__(self: "Room") -> str:
"""It returns readable name for the model."""
return f"{self.id}"
message_created(sender, instance, created, **kwargs)
Send message to room group.
Source code in chat/models.py
@receiver(post_save, sender=Message)
def message_created(sender, instance, created, **kwargs: dict) -> None: # noqa
"""Send message to room group."""
if created:
from .schema import MessageSchema
message = MessageSchema.from_orm(instance)
channel_layer = get_channel_layer()
async_to_sync(channel_layer.group_send)(
f"chat_{instance.room.id}",
{
"type": "chat_message",
"message": message.json(),
},
)
Last update: 2023-06-09
Authors: