Add comments app with models, views, and tests; integrate comments into existing models

This commit is contained in:
pacnpal
2025-02-07 21:58:02 -05:00
parent 7d7ebe1c0c
commit f000c492e8
18 changed files with 314 additions and 15 deletions

73
comments/models.py Normal file
View File

@@ -0,0 +1,73 @@
from django.db import models
from django.conf import settings
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
class CommentThread(models.Model):
"""
A generic comment thread that can be attached to any model instance.
Used for tracking discussions on various objects across the platform.
"""
content_type = models.ForeignKey(
ContentType,
on_delete=models.CASCADE,
related_name='comment_threads'
)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
title = models.CharField(max_length=255, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
created_by = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.SET_NULL,
null=True,
related_name='created_comment_threads'
)
is_locked = models.BooleanField(default=False)
is_hidden = models.BooleanField(default=False)
class Meta:
indexes = [
models.Index(fields=['content_type', 'object_id']),
]
ordering = ['-created_at']
def __str__(self):
return f"Comment Thread on {self.content_object} - {self.title}"
class Comment(models.Model):
"""
Individual comment within a comment thread.
"""
thread = models.ForeignKey(
CommentThread,
on_delete=models.CASCADE,
related_name='comments'
)
author = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.SET_NULL,
null=True,
related_name='comments'
)
content = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
is_edited = models.BooleanField(default=False)
is_hidden = models.BooleanField(default=False)
parent = models.ForeignKey(
'self',
on_delete=models.CASCADE,
null=True,
blank=True,
related_name='replies'
)
class Meta:
ordering = ['created_at']
def __str__(self):
return f"Comment by {self.author} on {self.created_at}"