Refactor comments app to use mixins for comment functionality; update admin interfaces and add historical model fixes

This commit is contained in:
pacnpal
2025-02-08 16:33:55 -05:00
parent f000c492e8
commit 181f49a0f2
21 changed files with 548 additions and 280 deletions

View File

@@ -2,20 +2,17 @@ from django.db import models
from django.conf import settings
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from .managers import CommentThreadManager, ThreadedModelManager
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.
A thread of comments that can be attached to any model instance,
including historical versions.
"""
content_type = models.ForeignKey(
ContentType,
on_delete=models.CASCADE,
related_name='comment_threads'
)
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
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)
@@ -28,6 +25,8 @@ class CommentThread(models.Model):
is_locked = models.BooleanField(default=False)
is_hidden = models.BooleanField(default=False)
objects = CommentThreadManager()
class Meta:
indexes = [
models.Index(fields=['content_type', 'object_id']),
@@ -37,11 +36,57 @@ class CommentThread(models.Model):
def __str__(self):
return f"Comment Thread on {self.content_object} - {self.title}"
class CommentThreads:
"""
Helper class to manage comment threads for a model instance.
This is used instead of direct inheritance to avoid historical model issues.
"""
def __init__(self, instance):
self.instance = instance
self._info = {}
def get_info(self):
"""Get or compute comment thread information."""
if not self._info:
ct = ContentType.objects.get_for_model(self.instance.__class__)
self._info = {
'count': CommentThread.objects.filter(
content_type=ct,
object_id=self.instance.pk
).count(),
'content_type': ct,
'object_id': self.instance.pk
}
return self._info
def get_threads(self):
"""Get comment threads for this instance."""
info = self.get_info()
return CommentThread.objects.filter(
content_type=info['content_type'],
object_id=info['object_id']
)
def add_thread(self, title='', created_by=None):
"""Create a new comment thread for this instance."""
info = self.get_info()
thread = CommentThread.objects.create(
content_type=info['content_type'],
object_id=info['object_id'],
title=title,
created_by=created_by
)
self._info = {} # Clear cache
return thread
def get_comment_threads(instance):
"""Get or create a CommentThreads helper for a model instance."""
if not hasattr(instance, '_comment_threads'):
instance._comment_threads = CommentThreads(instance)
return instance._comment_threads
class Comment(models.Model):
"""
Individual comment within a comment thread.
"""
"""Individual comment within a thread."""
thread = models.ForeignKey(
CommentThread,
on_delete=models.CASCADE,