Advertisement
horozov86

2. Chat App

Dec 4th, 2023
787
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | None | 0 0
  1. class UserProfile(models.Model):
  2.     username = models.CharField(
  3.         max_length=70,
  4.         unique=True,
  5.     )
  6.  
  7.     email = models.EmailField(
  8.         unique=True,
  9.     )
  10.  
  11.     bio = models.TextField(
  12.         null=True,
  13.         blank=True,
  14.     )
  15.  
  16.  
  17. class Message(models.Model):
  18.     sender = models.ForeignKey(
  19.         to=UserProfile,
  20.         related_name="sent_messages",
  21.         on_delete=models.CASCADE,
  22.     )
  23.  
  24.     receiver = models.ForeignKey(
  25.         to=UserProfile,
  26.         related_name="received_messages",
  27.         on_delete=models.CASCADE,
  28.     )
  29.  
  30.     content = models.TextField()
  31.  
  32.     timestamp = models.DateTimeField(
  33.         auto_now_add=True,
  34.     )
  35.  
  36.     is_read = models.BooleanField(
  37.         default=False,
  38.     )
  39.  
  40.     def mark_as_read(self) -> None:
  41.         self.is_read = True
  42.  
  43.     def mark_as_unread(self) -> None:
  44.         self.is_read = False
  45.  
  46.     def reply_to_message(self, reply_content: str, receiver: UserProfile) -> object:
  47.         return Message(
  48.             sender=self.receiver,
  49.             receiver=receiver,
  50.             content=reply_content
  51.         )
  52.  
  53.     def forward_message(self, sender: UserProfile, receiver: UserProfile) -> object:
  54.         return Message(
  55.             sender=sender,
  56.             receiver=receiver,
  57.             content=self.content,
  58.         )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement