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 86ae24bbac
commit 75f5b07129
18 changed files with 314 additions and 15 deletions

View File

@@ -5,7 +5,7 @@ from django.http import HttpRequest, HttpResponse, Http404
from django.template.loader import render_to_string
from django.core.exceptions import PermissionDenied
from .models import ChangeSet, CommentThread, Comment
from .models import ChangeSet, HistoricalCommentThread, Comment
from .notifications import NotificationDispatcher
from .state_machine import ApprovalStateMachine
@@ -16,7 +16,7 @@ def get_comments(request: HttpRequest) -> HttpResponse:
if not anchor:
raise Http404("Anchor parameter is required")
thread = CommentThread.objects.filter(anchor__id=anchor).first()
thread = HistoricalCommentThread.objects.filter(anchor__id=anchor).first()
comments = thread.comments.all() if thread else []
return render(request, 'history_tracking/partials/comments_list.html', {
@@ -44,7 +44,7 @@ def add_comment(request: HttpRequest) -> HttpResponse:
if not content:
return HttpResponse("Comment content is required", status=400)
thread, created = CommentThread.objects.get_or_create(
thread, created = HistoricalCommentThread.objects.get_or_create(
anchor={'id': anchor},
defaults={'created_by': request.user}
)

View File

@@ -18,7 +18,8 @@ class HistoricalModel(models.Model):
id = models.BigAutoField(primary_key=True)
history: HistoricalRecords = HistoricalRecords(
inherit=True,
bases=(HistoricalChangeMixin,)
bases=(HistoricalChangeMixin,),
excluded_fields=['comments', 'photos', 'reviews'] # Exclude all generic relations
)
class Meta:
@@ -116,8 +117,8 @@ class VersionTag(models.Model):
def __str__(self) -> str:
return f"{self.name} ({self.branch.name})"
class CommentThread(models.Model):
"""Represents a thread of comments on a historical record"""
class HistoricalCommentThread(models.Model):
"""Represents a thread of comments specific to historical records and version control"""
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
@@ -149,7 +150,7 @@ class CommentThread(models.Model):
class Comment(models.Model):
"""Individual comment within a thread"""
thread = models.ForeignKey(CommentThread, on_delete=models.CASCADE, related_name='comments')
thread = models.ForeignKey(HistoricalCommentThread, on_delete=models.CASCADE, related_name='comments')
author = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
content = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)

View File

@@ -9,7 +9,7 @@ from django.views.decorators.http import require_http_methods
from django.core.exceptions import PermissionDenied
from typing import Dict, Any
from .models import VersionBranch, ChangeSet, VersionTag, CommentThread
from .models import VersionBranch, ChangeSet, VersionTag, HistoricalCommentThread
from .managers import ChangeTracker
from .comparison import ComparisonEngine
from .state_machine import ApprovalStateMachine
@@ -42,7 +42,7 @@ def version_comparison(request: HttpRequest) -> HttpResponse:
# Add comments to changes
for change in diff_result['changes']:
anchor_id = change['metadata']['comment_anchor_id']
change['comments'] = CommentThread.objects.filter(
change['comments'] = HistoricalCommentThread.objects.filter(
anchor__contains={'id': anchor_id}
).prefetch_related('comments')