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,22 +1,26 @@
from django.db import models
from django.urls import reverse
from django.utils.text import slugify
from django.contrib.contenttypes.fields import GenericRelation
from django.core.exceptions import ValidationError
from decimal import Decimal, ROUND_DOWN, InvalidOperation
from typing import Tuple, Optional, Any, TYPE_CHECKING
from django.contrib.contenttypes.fields import GenericRelation
from companies.models import Company
from history_tracking.signals import get_current_branch
from media.models import Photo
from history_tracking.models import HistoricalModel
from location.models import Location
from comments.mixins import CommentableMixin
from media.mixins import PhotoableModel
from location.mixins import LocationMixin
if TYPE_CHECKING:
from rides.models import Ride
class Park(HistoricalModel):
class Park(HistoricalModel, CommentableMixin, PhotoableModel, LocationMixin):
comments = GenericRelation('comments.CommentThread') # Centralized reference
id: int # Type hint for Django's automatic id field
STATUS_CHOICES = [
("OPERATING", "Operating"),
@@ -34,9 +38,6 @@ class Park(HistoricalModel):
max_length=20, choices=STATUS_CHOICES, default="OPERATING"
)
# Location fields using GenericRelation
location = GenericRelation(Location, related_query_name='park')
# Details
opening_date = models.DateField(null=True, blank=True)
closing_date = models.DateField(null=True, blank=True)
@@ -57,12 +58,8 @@ class Park(HistoricalModel):
owner = models.ForeignKey(
Company, on_delete=models.SET_NULL, null=True, blank=True, related_name="parks"
)
photos = GenericRelation(Photo, related_query_name="park")
comments = GenericRelation('comments.CommentThread',
related_name='park_threads',
related_query_name='comments_thread'
)
areas: models.Manager['ParkArea'] # Type hint for reverse relation
rides: models.Manager['Ride'] # Type hint for reverse relation from rides app
# Metadata
@@ -71,6 +68,7 @@ class Park(HistoricalModel):
class Meta:
ordering = ["name"]
excluded_fields = ['comments'] # Exclude from historical tracking
def __str__(self) -> str:
return self.name
@@ -126,23 +124,6 @@ class Park(HistoricalModel):
def get_absolute_url(self) -> str:
return reverse("parks:park_detail", kwargs={"slug": self.slug})
@property
def formatted_location(self) -> str:
if self.location.exists():
location = self.location.first()
if location:
return location.get_formatted_address()
return ""
@property
def coordinates(self) -> Optional[Tuple[float, float]]:
"""Returns coordinates as a tuple (latitude, longitude)"""
if self.location.exists():
location = self.location.first()
if location:
return location.coordinates
return None
@classmethod
def get_by_slug(cls, slug: str) -> Tuple['Park', bool]:
"""Get park by current or historical slug"""
@@ -159,7 +140,8 @@ class Park(HistoricalModel):
raise cls.DoesNotExist("No park found with this slug")
class ParkArea(HistoricalModel):
class ParkArea(HistoricalModel, CommentableMixin, PhotoableModel):
comments = GenericRelation('comments.CommentThread') # Centralized reference
id: int # Type hint for Django's automatic id field
park = models.ForeignKey(Park, on_delete=models.CASCADE, related_name="areas")
name = models.CharField(max_length=255)
@@ -169,10 +151,6 @@ class ParkArea(HistoricalModel):
closing_date = models.DateField(null=True, blank=True)
# Relationships
comments = GenericRelation('comments.CommentThread',
related_name='park_area_threads',
related_query_name='comments_thread'
)
# Metadata
created_at = models.DateTimeField(auto_now_add=True, null=True)
@@ -181,6 +159,7 @@ class ParkArea(HistoricalModel):
class Meta:
ordering = ["name"]
unique_together = ["park", "slug"]
excluded_fields = ['comments'] # Exclude from historical tracking
def __str__(self) -> str:
return f"{self.name} at {self.park.name}"