Refactor model imports and update admin classes to use pghistory for historical tracking; replace HistoricalModel with TrackedModel in relevant models

This commit is contained in:
pacnpal
2025-02-09 11:20:40 -05:00
parent 7ecf43f1a4
commit b7f6c60682
24 changed files with 1729 additions and 137 deletions

View File

@@ -5,17 +5,18 @@ 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
import pghistory
from companies.models import Company
from media.models import Photo
from history_tracking.models import HistoricalModel
from history_tracking.models import TrackedModel
from location.models import Location
if TYPE_CHECKING:
from rides.models import Ride
class Park(HistoricalModel):
@pghistory.track()
class Park(TrackedModel):
id: int # Type hint for Django's automatic id field
STATUS_CHOICES = [
("OPERATING", "Operating"),
@@ -101,17 +102,21 @@ class Park(HistoricalModel):
try:
return cls.objects.get(slug=slug), False
except cls.DoesNotExist:
# Check historical slugs
history = cls.history.filter(slug=slug).order_by("-history_date").first() # type: ignore[attr-defined]
# Check historical slugs using pghistory
history_model = cls.get_history_model()
history = history_model.objects.filter(
slug=slug
).order_by('-pgh_created_at').first()
if history:
try:
return cls.objects.get(pk=history.instance.pk), True
return cls.objects.get(pk=history.pgh_obj_id), True
except cls.DoesNotExist as e:
raise cls.DoesNotExist("No park found with this slug") from e
raise cls.DoesNotExist("No park found with this slug")
class ParkArea(HistoricalModel):
@pghistory.track()
class ParkArea(TrackedModel):
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)
@@ -148,11 +153,15 @@ class ParkArea(HistoricalModel):
try:
return cls.objects.get(slug=slug), False
except cls.DoesNotExist:
# Check historical slugs
history = cls.history.filter(slug=slug).order_by("-history_date").first() # type: ignore[attr-defined]
# Check historical slugs using pghistory
history_model = cls.get_history_model()
history = history_model.objects.filter(
slug=slug
).order_by('-pgh_created_at').first()
if history:
try:
return cls.objects.get(pk=history.instance.pk), True
return cls.objects.get(pk=history.pgh_obj_id), True
except cls.DoesNotExist as e:
raise cls.DoesNotExist("No park area found with this slug") from e
raise cls.DoesNotExist("No park area found with this slug")