2022-01-13 11:07:20 +00:00
|
|
|
from django.db import models
|
2022-03-10 21:35:16 +00:00
|
|
|
from api.models import User, Order
|
2022-05-28 13:01:50 +00:00
|
|
|
from django.utils import timezone
|
|
|
|
import uuid
|
2022-03-10 21:35:16 +00:00
|
|
|
|
2022-10-20 09:56:10 +00:00
|
|
|
|
2022-03-10 21:35:16 +00:00
|
|
|
class ChatRoom(models.Model):
|
2022-10-20 09:56:10 +00:00
|
|
|
"""
|
2022-03-10 21:35:16 +00:00
|
|
|
Simple ChatRoom model. Needed to facilitate communication: Is my counterpart in the room?
|
2022-10-20 09:56:10 +00:00
|
|
|
"""
|
2022-03-10 21:35:16 +00:00
|
|
|
|
2022-10-20 09:56:10 +00:00
|
|
|
id = models.PositiveBigIntegerField(
|
|
|
|
primary_key=True, null=False, default=None, blank=True
|
|
|
|
)
|
2022-03-10 21:35:16 +00:00
|
|
|
order = models.ForeignKey(
|
|
|
|
Order,
|
2022-05-28 13:01:50 +00:00
|
|
|
related_name="chatroom",
|
2022-03-10 21:35:16 +00:00
|
|
|
on_delete=models.SET_NULL,
|
|
|
|
null=True,
|
2022-10-20 09:56:10 +00:00
|
|
|
default=None,
|
|
|
|
)
|
2022-03-10 21:35:16 +00:00
|
|
|
maker = models.ForeignKey(
|
|
|
|
User,
|
|
|
|
related_name="chat_maker",
|
|
|
|
on_delete=models.SET_NULL,
|
|
|
|
null=True,
|
2022-10-20 09:56:10 +00:00
|
|
|
default=None,
|
|
|
|
)
|
2022-03-10 21:35:16 +00:00
|
|
|
taker = models.ForeignKey(
|
|
|
|
User,
|
|
|
|
related_name="chat_taker",
|
|
|
|
on_delete=models.SET_NULL,
|
|
|
|
null=True,
|
|
|
|
default=None,
|
|
|
|
blank=True,
|
|
|
|
)
|
|
|
|
|
2022-10-20 09:56:10 +00:00
|
|
|
maker_connected = models.BooleanField(default=False, null=False)
|
|
|
|
taker_connected = models.BooleanField(default=False, null=False)
|
2022-03-10 21:35:16 +00:00
|
|
|
|
|
|
|
maker_connect_date = models.DateTimeField(auto_now_add=True)
|
|
|
|
taker_connect_date = models.DateTimeField(auto_now_add=True)
|
|
|
|
|
|
|
|
room_group_name = models.CharField(
|
|
|
|
max_length=50,
|
|
|
|
null=True,
|
|
|
|
default=None,
|
|
|
|
blank=True,
|
2022-05-28 13:01:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def __str__(self):
|
2022-06-03 09:39:48 +00:00
|
|
|
return f"Chat:{str(self.id)}"
|
2022-05-28 13:01:50 +00:00
|
|
|
|
2022-10-20 09:56:10 +00:00
|
|
|
|
2022-05-28 13:01:50 +00:00
|
|
|
class Message(models.Model):
|
2022-05-28 14:59:32 +00:00
|
|
|
class Meta:
|
2022-10-20 09:56:10 +00:00
|
|
|
get_latest_by = "index"
|
2022-05-28 14:59:32 +00:00
|
|
|
|
|
|
|
# id = models.PositiveBigIntegerField(primary_key=True, default=uuid.uuid4, editable=False)
|
2022-05-28 13:01:50 +00:00
|
|
|
order = models.ForeignKey(
|
2022-10-20 09:56:10 +00:00
|
|
|
Order, related_name="message", on_delete=models.CASCADE, null=True, default=None
|
|
|
|
)
|
2022-05-28 13:01:50 +00:00
|
|
|
chatroom = models.ForeignKey(
|
|
|
|
ChatRoom,
|
|
|
|
related_name="chatroom",
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
null=True,
|
2022-10-20 09:56:10 +00:00
|
|
|
default=None,
|
|
|
|
)
|
|
|
|
index = models.PositiveIntegerField(null=False, default=None, blank=True)
|
2022-05-28 13:01:50 +00:00
|
|
|
sender = models.ForeignKey(
|
|
|
|
User,
|
|
|
|
related_name="message_sender",
|
|
|
|
on_delete=models.SET_NULL,
|
|
|
|
null=True,
|
2022-10-20 09:56:10 +00:00
|
|
|
default=None,
|
|
|
|
)
|
2022-05-28 14:59:32 +00:00
|
|
|
receiver = models.ForeignKey(
|
|
|
|
User,
|
|
|
|
related_name="message_receiver",
|
|
|
|
on_delete=models.SET_NULL,
|
|
|
|
null=True,
|
2022-10-20 09:56:10 +00:00
|
|
|
default=None,
|
|
|
|
)
|
2022-05-28 13:01:50 +00:00
|
|
|
|
2022-10-20 09:56:10 +00:00
|
|
|
PGP_message = models.TextField(max_length=5000, null=True, default=None, blank=True)
|
2022-05-28 13:01:50 +00:00
|
|
|
|
|
|
|
created_at = models.DateTimeField(default=timezone.now)
|
|
|
|
|
|
|
|
def __str__(self):
|
2022-06-03 09:39:48 +00:00
|
|
|
return f"Chat:{str(self.chatroom.id)} - Idx:{self.index}"
|