adding features for social network

This commit is contained in:
2026-04-18 13:31:20 +02:00
parent 1bc57a99ce
commit 043e866ac9
7 changed files with 177 additions and 13 deletions

View File

@@ -2,6 +2,7 @@ from django.db import models
from django.conf import settings
from django.core.exceptions import ValidationError
from django.utils import timezone
from vontor_cz.custom_fields import WebPImageField
class Chat(models.Model):
owner = models.ForeignKey(
@@ -10,6 +11,11 @@ class Chat(models.Model):
null=True,
related_name='owned_chats'
)
name = models.CharField(max_length=255, blank=True)
icon = WebPImageField(upload_to='chat_icons/', null=True, blank=True)
banner = WebPImageField(upload_to='chat_banners/', null=True, blank=True)
members = models.ManyToManyField(
settings.AUTH_USER_MODEL,
@@ -23,6 +29,15 @@ class Chat(models.Model):
blank=True
)
hub = models.ForeignKey(
'pages.Hub',
on_delete=models.CASCADE,
null=True,
blank=True
)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
@@ -43,8 +58,9 @@ class Message(models.Model):
chat = models.ForeignKey(Chat, related_name='messages', on_delete=models.CASCADE)
sender = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
settings.AUTH_USER_MODEL,
on_delete=models.SET_NULL,
null=True,
related_name='sent_messages'
)
@@ -68,18 +84,10 @@ class Message(models.Model):
updated_at = models.DateTimeField(auto_now=True)
def clean(self):
# VALIDATION: Ensure sender is actually in the chat
# Note: We check self.id to avoid running this on creation if logic depends on M2M
# But generally, a sender must be a member.
if self.chat and self.sender:
if not self.chat.members.filter(id=self.sender.id).exists():
if self.chat_id and self.sender_id:
if not self.chat.members.filter(id=self.sender_id).exists():
raise ValidationError("Sender is not a member of this chat.")
def save(self, *args, **kwargs):
# Optional: Run validation before saving
# self.full_clean()
super().save(*args, **kwargs)
# --- HELPER METHODS FOR WEBSOCKETS / VIEWS ---
def edit_content(self, new_text):