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

@@ -1,16 +1,21 @@
from django.db import models
from django.utils.text import slugify
from django.urls import reverse
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericRelation
from django.contrib.contenttypes.models import ContentType
from typing import Tuple, Optional, ClassVar, TYPE_CHECKING
from history_tracking.models import HistoricalModel, VersionBranch, ChangeSet
from history_tracking.signals import get_current_branch, ChangesetContextManager
from comments.mixins import CommentableMixin
from media.mixins import PhotoableModel
if TYPE_CHECKING:
from history_tracking.models import HistoricalSlug
class Company(HistoricalModel):
class Company(HistoricalModel, CommentableMixin, PhotoableModel):
comments = GenericRelation(
'comments.CommentThread') # Explicit relationship
name = models.CharField(max_length=255)
slug = models.SlugField(max_length=255, unique=True)
website = models.URLField(blank=True)
@@ -20,16 +25,13 @@ class Company(HistoricalModel):
total_rides = models.IntegerField(default=0)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
comments = GenericRelation('comments.CommentThread',
related_name='company_threads',
related_query_name='comments_thread'
)
objects: ClassVar[models.Manager['Company']]
class Meta:
verbose_name_plural = 'companies'
ordering = ['name']
excluded_fields = ['comments'] # Exclude from historical tracking
def __str__(self) -> str:
return self.name
@@ -37,10 +39,10 @@ class Company(HistoricalModel):
def save(self, *args, **kwargs) -> None:
if not self.slug:
self.slug = slugify(self.name)
# Get the branch from context or use default
current_branch = get_current_branch()
if current_branch:
# Save in the context of the current branch
super().save(*args, **kwargs)
@@ -50,7 +52,7 @@ class Company(HistoricalModel):
name='main',
defaults={'metadata': {'type': 'default_branch'}}
)
with ChangesetContextManager(branch=main_branch):
super().save(*args, **kwargs)
@@ -62,20 +64,20 @@ class Company(HistoricalModel):
object_id=self.pk,
status='applied'
).order_by('-created_at')[:5]
active_branches = VersionBranch.objects.filter(
changesets__content_type=content_type,
changesets__object_id=self.pk,
is_active=True
).distinct()
return {
'latest_changes': latest_changes,
'active_branches': active_branches,
'current_branch': get_current_branch(),
'total_changes': latest_changes.count()
}
def get_absolute_url(self) -> str:
return reverse("companies:company_detail", kwargs={"slug": self.slug})
@@ -96,7 +98,10 @@ class Company(HistoricalModel):
except (HistoricalSlug.DoesNotExist, cls.DoesNotExist):
raise cls.DoesNotExist()
class Manufacturer(HistoricalModel):
class Manufacturer(HistoricalModel, CommentableMixin, PhotoableModel):
comments = GenericRelation(
'comments.CommentThread') # Explicit relationship
name = models.CharField(max_length=255)
slug = models.SlugField(max_length=255, unique=True)
website = models.URLField(blank=True)
@@ -106,15 +111,13 @@ class Manufacturer(HistoricalModel):
total_roller_coasters = models.IntegerField(default=0)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
comments = GenericRelation('comments.CommentThread',
related_name='manufacturer_threads',
related_query_name='comments_thread'
)
objects: ClassVar[models.Manager['Manufacturer']]
class Meta:
ordering = ['name']
excluded_fields = ['comments'] # Exclude from historical tracking
history_exclude = ['comments'] # Exclude from historical models
def __str__(self) -> str:
return self.name
@@ -122,10 +125,10 @@ class Manufacturer(HistoricalModel):
def save(self, *args, **kwargs) -> None:
if not self.slug:
self.slug = slugify(self.name)
# Get the branch from context or use default
current_branch = get_current_branch()
if current_branch:
# Save in the context of the current branch
super().save(*args, **kwargs)
@@ -135,7 +138,7 @@ class Manufacturer(HistoricalModel):
name='main',
defaults={'metadata': {'type': 'default_branch'}}
)
with ChangesetContextManager(branch=main_branch):
super().save(*args, **kwargs)
@@ -147,13 +150,13 @@ class Manufacturer(HistoricalModel):
object_id=self.pk,
status='applied'
).order_by('-created_at')[:5]
active_branches = VersionBranch.objects.filter(
changesets__content_type=content_type,
changesets__object_id=self.pk,
is_active=True
).distinct()
return {
'latest_changes': latest_changes,
'active_branches': active_branches,
@@ -181,7 +184,10 @@ class Manufacturer(HistoricalModel):
except (HistoricalSlug.DoesNotExist, cls.DoesNotExist):
raise cls.DoesNotExist()
class Designer(HistoricalModel):
class Designer(HistoricalModel, CommentableMixin, PhotoableModel):
comments = GenericRelation(
'comments.CommentThread') # Explicit relationship
name = models.CharField(max_length=255)
slug = models.SlugField(max_length=255, unique=True)
website = models.URLField(blank=True)
@@ -190,10 +196,6 @@ class Designer(HistoricalModel):
total_roller_coasters = models.IntegerField(default=0)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
comments = GenericRelation('comments.CommentThread',
related_name='designer_threads',
related_query_name='comments_thread'
)
objects: ClassVar[models.Manager['Designer']]
@@ -206,10 +208,10 @@ class Designer(HistoricalModel):
def save(self, *args, **kwargs) -> None:
if not self.slug:
self.slug = slugify(self.name)
# Get the branch from context or use default
current_branch = get_current_branch()
if current_branch:
# Save in the context of the current branch
super().save(*args, **kwargs)
@@ -219,7 +221,7 @@ class Designer(HistoricalModel):
name='main',
defaults={'metadata': {'type': 'default_branch'}}
)
with ChangesetContextManager(branch=main_branch):
super().save(*args, **kwargs)
@@ -231,13 +233,13 @@ class Designer(HistoricalModel):
object_id=self.pk,
status='applied'
).order_by('-created_at')[:5]
active_branches = VersionBranch.objects.filter(
changesets__content_type=content_type,
changesets__object_id=self.pk,
is_active=True
).distinct()
return {
'latest_changes': latest_changes,
'active_branches': active_branches,