Integrate parks app with site-wide search system; add filter configuration, error handling, and search interfaces

This commit is contained in:
pacnpal
2025-02-12 16:59:20 -05:00
parent af57592496
commit 1fe299fb4b
13 changed files with 1267 additions and 72 deletions

View File

@@ -72,13 +72,52 @@ class Park(TrackedModel):
return self.name
def save(self, *args: Any, **kwargs: Any) -> None:
if not self.slug:
from django.contrib.contenttypes.models import ContentType
from history_tracking.models import HistoricalSlug
# Get old instance if it exists
if self.pk:
try:
old_instance = type(self).objects.get(pk=self.pk)
old_name = old_instance.name
old_slug = old_instance.slug
except type(self).DoesNotExist:
old_name = None
old_slug = None
else:
old_name = None
old_slug = None
# Generate new slug if name has changed or slug is missing
if not self.slug or (old_name and old_name != self.name):
self.slug = slugify(self.name)
# Save the model
super().save(*args, **kwargs)
# If slug has changed, save historical record
if old_slug and old_slug != self.slug:
HistoricalSlug.objects.create(
content_type=ContentType.objects.get_for_model(self),
object_id=self.pk,
slug=old_slug
)
def get_absolute_url(self) -> str:
return reverse("parks:park_detail", kwargs={"slug": self.slug})
def get_status_color(self) -> str:
"""Get Tailwind color classes for park status"""
status_colors = {
'OPERATING': 'bg-green-100 text-green-800',
'CLOSED_TEMP': 'bg-yellow-100 text-yellow-800',
'CLOSED_PERM': 'bg-red-100 text-red-800',
'UNDER_CONSTRUCTION': 'bg-blue-100 text-blue-800',
'DEMOLISHED': 'bg-gray-100 text-gray-800',
'RELOCATED': 'bg-purple-100 text-purple-800',
}
return status_colors.get(self.status, 'bg-gray-100 text-gray-500')
@property
def formatted_location(self) -> str:
if self.location.exists():
@@ -99,20 +138,58 @@ class Park(TrackedModel):
@classmethod
def get_by_slug(cls, slug: str) -> Tuple['Park', bool]:
"""Get park by current or historical slug"""
from django.contrib.contenttypes.models import ContentType
from history_tracking.models import HistoricalSlug
print(f"\nLooking up slug: {slug}")
try:
return cls.objects.get(slug=slug), False
park = cls.objects.get(slug=slug)
print(f"Found current park with slug: {slug}")
return park, False
except cls.DoesNotExist:
# Check historical slugs using pghistory
history_model = cls.get_history_model()
history = history_model.objects.filter(
slug=slug
).order_by('-pgh_created_at').first()
print(f"No current park found with slug: {slug}")
if history:
# Try historical slugs in HistoricalSlug model
content_type = ContentType.objects.get_for_model(cls)
print(f"Searching HistoricalSlug with content_type: {content_type}")
historical = HistoricalSlug.objects.filter(
content_type=content_type,
slug=slug
).order_by('-created_at').first()
if historical:
print(f"Found historical slug record for object_id: {historical.object_id}")
try:
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
park = cls.objects.get(pk=historical.object_id)
print(f"Found park from historical slug: {park.name}")
return park, True
except cls.DoesNotExist:
print(f"Park not found for historical slug record")
pass
else:
print("No historical slug record found")
# Try pghistory events
print(f"Searching pghistory events")
event_model = getattr(cls, 'event_model', None)
if event_model:
historical_event = event_model.objects.filter(
slug=slug
).order_by('-pgh_created_at').first()
if historical_event:
print(f"Found pghistory event for pgh_obj_id: {historical_event.pgh_obj_id}")
try:
park = cls.objects.get(pk=historical_event.pgh_obj_id)
print(f"Found park from pghistory: {park.name}")
return park, True
except cls.DoesNotExist:
print(f"Park not found for pghistory event")
pass
else:
print("No pghistory event found")
raise cls.DoesNotExist("No park found with this slug")
@pghistory.track()