mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-20 05:31:09 -05:00
Refactor parks and rides views for improved organization and readability
- Updated imports in parks/views.py to use ParkReview as Review for clarity. - Enhanced road trip views in parks/views_roadtrip.py by removing unnecessary parameters and improving context handling. - Streamlined error handling and response messages in CreateTripView and FindParksAlongRouteView. - Improved code formatting and consistency across various methods in parks/views_roadtrip.py. - Refactored rides/models.py to import Company from models for better clarity. - Updated rides/views.py to import RideSearchForm from services for better organization. - Added a comprehensive Django best practices analysis document to memory-bank/documentation.
This commit is contained in:
@@ -6,13 +6,14 @@ 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 import Company
|
||||
from media.models import Photo
|
||||
from core.history import TrackedModel
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from rides.models import Ride
|
||||
from . import ParkArea
|
||||
|
||||
|
||||
@pghistory.track()
|
||||
class Park(TrackedModel):
|
||||
@@ -71,7 +72,8 @@ class Park(TrackedModel):
|
||||
)
|
||||
photos = GenericRelation(Photo, related_query_name="park")
|
||||
areas: models.Manager['ParkArea'] # Type hint for reverse relation
|
||||
rides: models.Manager['Ride'] # Type hint for reverse relation from rides app
|
||||
# Type hint for reverse relation from rides app
|
||||
rides: models.Manager['Ride']
|
||||
|
||||
# Metadata
|
||||
created_at = models.DateTimeField(auto_now_add=True, null=True)
|
||||
@@ -86,7 +88,7 @@ class Park(TrackedModel):
|
||||
def save(self, *args: Any, **kwargs: Any) -> None:
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from core.history import HistoricalSlug
|
||||
|
||||
|
||||
# Get old instance if it exists
|
||||
if self.pk:
|
||||
try:
|
||||
@@ -99,14 +101,14 @@ class Park(TrackedModel):
|
||||
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(
|
||||
@@ -118,9 +120,11 @@ class Park(TrackedModel):
|
||||
def clean(self):
|
||||
super().clean()
|
||||
if self.operator and 'OPERATOR' not in self.operator.roles:
|
||||
raise ValidationError({'operator': 'Company must have the OPERATOR role.'})
|
||||
raise ValidationError(
|
||||
{'operator': 'Company must have the OPERATOR role.'})
|
||||
if self.property_owner and 'PROPERTY_OWNER' not in self.property_owner.roles:
|
||||
raise ValidationError({'property_owner': 'Company must have the PROPERTY_OWNER role.'})
|
||||
raise ValidationError(
|
||||
{'property_owner': 'Company must have the PROPERTY_OWNER role.'})
|
||||
|
||||
def get_absolute_url(self) -> str:
|
||||
return reverse("parks:park_detail", kwargs={"slug": self.slug})
|
||||
@@ -156,54 +160,55 @@ class Park(TrackedModel):
|
||||
"""Get park by current or historical slug"""
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from core.history import HistoricalSlug
|
||||
|
||||
|
||||
print(f"\nLooking up slug: {slug}")
|
||||
|
||||
|
||||
try:
|
||||
park = cls.objects.get(slug=slug)
|
||||
print(f"Found current park with slug: {slug}")
|
||||
return park, False
|
||||
except cls.DoesNotExist:
|
||||
print(f"No current park found with slug: {slug}")
|
||||
|
||||
|
||||
# Try historical slugs in HistoricalSlug model
|
||||
content_type = ContentType.objects.get_for_model(cls)
|
||||
print(f"Searching HistoricalSlug with content_type: {content_type}")
|
||||
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}")
|
||||
print(
|
||||
f"Found historical slug record for object_id: {historical.object_id}")
|
||||
try:
|
||||
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
|
||||
print("Park not found for historical slug record")
|
||||
else:
|
||||
print("No historical slug record found")
|
||||
|
||||
|
||||
# Try pghistory events
|
||||
print(f"Searching pghistory events")
|
||||
print("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}")
|
||||
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
|
||||
print("Park not found for pghistory event")
|
||||
else:
|
||||
print("No pghistory event found")
|
||||
|
||||
raise cls.DoesNotExist("No park found with this slug")
|
||||
|
||||
raise cls.DoesNotExist("No park found with this slug")
|
||||
|
||||
Reference in New Issue
Block a user