mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2026-01-02 03:27:02 -05:00
feat: Implement initial schema and add various API, service, and management command enhancements across the application.
This commit is contained in:
@@ -15,6 +15,7 @@ logger = logging.getLogger(__name__)
|
||||
# Computed Field Maintenance Signals
|
||||
# =============================================================================
|
||||
|
||||
|
||||
def update_park_search_text(park):
|
||||
"""
|
||||
Update park's search_text computed field.
|
||||
@@ -27,17 +28,17 @@ def update_park_search_text(park):
|
||||
|
||||
try:
|
||||
park._populate_computed_fields()
|
||||
park.save(update_fields=['search_text'])
|
||||
park.save(update_fields=["search_text"])
|
||||
logger.debug(f"Updated search_text for park {park.pk}")
|
||||
except Exception as e:
|
||||
logger.exception(f"Failed to update search_text for park {park.pk}: {e}")
|
||||
|
||||
|
||||
# Status values that count as "active" rides for counting purposes
|
||||
ACTIVE_STATUSES = {'OPERATING', 'SEASONAL', 'UNDER_CONSTRUCTION'}
|
||||
ACTIVE_STATUSES = {"OPERATING", "SEASONAL", "UNDER_CONSTRUCTION"}
|
||||
|
||||
# Status values that should decrement ride counts
|
||||
INACTIVE_STATUSES = {'CLOSED_PERM', 'DEMOLISHED', 'RELOCATED', 'REMOVED'}
|
||||
INACTIVE_STATUSES = {"CLOSED_PERM", "DEMOLISHED", "RELOCATED", "REMOVED"}
|
||||
|
||||
|
||||
def update_park_ride_counts(park, old_status=None, new_status=None):
|
||||
@@ -54,11 +55,11 @@ def update_park_ride_counts(park, old_status=None, new_status=None):
|
||||
return
|
||||
|
||||
# Get park ID
|
||||
park_id = park.pk if hasattr(park, 'pk') else park
|
||||
park_id = park.pk if hasattr(park, "pk") else park
|
||||
|
||||
try:
|
||||
# Fetch the park if we only have an ID
|
||||
if not hasattr(park, 'rides'):
|
||||
if not hasattr(park, "rides"):
|
||||
park = Park.objects.get(id=park_id)
|
||||
|
||||
# Build the query for active rides
|
||||
@@ -72,14 +73,9 @@ def update_park_ride_counts(park, old_status=None, new_status=None):
|
||||
coaster_count = park.rides.filter(operating_rides, category__in=["RC", "WC"]).count()
|
||||
|
||||
# Update park counts
|
||||
Park.objects.filter(id=park_id).update(
|
||||
ride_count=ride_count, coaster_count=coaster_count
|
||||
)
|
||||
Park.objects.filter(id=park_id).update(ride_count=ride_count, coaster_count=coaster_count)
|
||||
|
||||
logger.debug(
|
||||
f"Updated park {park_id} counts: "
|
||||
f"ride_count={ride_count}, coaster_count={coaster_count}"
|
||||
)
|
||||
logger.debug(f"Updated park {park_id} counts: " f"ride_count={ride_count}, coaster_count={coaster_count}")
|
||||
|
||||
except Park.DoesNotExist:
|
||||
logger.warning(f"Park {park_id} does not exist, cannot update counts")
|
||||
@@ -124,14 +120,12 @@ def ride_saved(sender, instance, created, **kwargs):
|
||||
return
|
||||
|
||||
# Check if status changed using model's tracker if available
|
||||
if hasattr(instance, 'tracker') and hasattr(instance.tracker, 'has_changed'):
|
||||
if instance.tracker.has_changed('status'):
|
||||
old_status = instance.tracker.previous('status')
|
||||
if hasattr(instance, "tracker") and hasattr(instance.tracker, "has_changed"):
|
||||
if instance.tracker.has_changed("status"):
|
||||
old_status = instance.tracker.previous("status")
|
||||
new_status = instance.status
|
||||
if should_update_counts(old_status, new_status):
|
||||
logger.info(
|
||||
f"Ride {instance.pk} status changed: {old_status} → {new_status}"
|
||||
)
|
||||
logger.info(f"Ride {instance.pk} status changed: {old_status} → {new_status}")
|
||||
update_park_ride_counts(instance.park, old_status, new_status)
|
||||
else:
|
||||
# Fallback: always update counts on save
|
||||
@@ -151,6 +145,7 @@ def ride_deleted(sender, instance, **kwargs):
|
||||
|
||||
# FSM transition signal handlers
|
||||
|
||||
|
||||
def handle_ride_status_transition(instance, source, target, user, **kwargs):
|
||||
"""
|
||||
Handle ride status FSM transitions.
|
||||
@@ -165,10 +160,7 @@ def handle_ride_status_transition(instance, source, target, user, **kwargs):
|
||||
user: The user who initiated the transition.
|
||||
"""
|
||||
if should_update_counts(source, target):
|
||||
logger.info(
|
||||
f"FSM transition: Ride {instance.pk} {source} → {target} "
|
||||
f"by {user if user else 'system'}"
|
||||
)
|
||||
logger.info(f"FSM transition: Ride {instance.pk} {source} → {target} " f"by {user if user else 'system'}")
|
||||
update_park_ride_counts(instance.park, source, target)
|
||||
|
||||
|
||||
@@ -176,7 +168,8 @@ def handle_ride_status_transition(instance, source, target, user, **kwargs):
|
||||
# Computed Field Maintenance Signal Handlers
|
||||
# =============================================================================
|
||||
|
||||
@receiver(post_save, sender='parks.ParkLocation')
|
||||
|
||||
@receiver(post_save, sender="parks.ParkLocation")
|
||||
def update_park_search_text_on_location_change(sender, instance, **kwargs):
|
||||
"""
|
||||
Update park search_text when location changes.
|
||||
@@ -186,13 +179,13 @@ def update_park_search_text_on_location_change(sender, instance, **kwargs):
|
||||
location information.
|
||||
"""
|
||||
try:
|
||||
if hasattr(instance, 'park') and instance.park:
|
||||
if hasattr(instance, "park") and instance.park:
|
||||
update_park_search_text(instance.park)
|
||||
except Exception as e:
|
||||
logger.exception(f"Failed to update park search_text on location change: {e}")
|
||||
|
||||
|
||||
@receiver(post_save, sender='parks.Company')
|
||||
@receiver(post_save, sender="parks.Company")
|
||||
def update_park_search_text_on_company_change(sender, instance, **kwargs):
|
||||
"""
|
||||
Update park search_text when operator/owner name changes.
|
||||
|
||||
Reference in New Issue
Block a user