mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2026-01-02 01:47:04 -05:00
feat: Implement initial schema and add various API, service, and management command enhancements across the application.
This commit is contained in:
@@ -13,6 +13,7 @@ logger = logging.getLogger(__name__)
|
||||
# Computed Field Maintenance
|
||||
# =============================================================================
|
||||
|
||||
|
||||
def update_ride_search_text(ride):
|
||||
"""
|
||||
Update ride's search_text computed field.
|
||||
@@ -25,7 +26,7 @@ def update_ride_search_text(ride):
|
||||
|
||||
try:
|
||||
ride._populate_computed_fields()
|
||||
ride.save(update_fields=['search_text'])
|
||||
ride.save(update_fields=["search_text"])
|
||||
logger.debug(f"Updated search_text for ride {ride.pk}")
|
||||
except Exception as e:
|
||||
logger.exception(f"Failed to update search_text for ride {ride.pk}: {e}")
|
||||
@@ -47,22 +48,16 @@ def handle_ride_status(sender, instance, **kwargs):
|
||||
if today >= instance.closing_date and instance.status == "CLOSING":
|
||||
target_status = instance.post_closing_status or "SBNO"
|
||||
|
||||
logger.info(
|
||||
f"Ride {instance.pk} closing date reached, "
|
||||
f"transitioning to {target_status}"
|
||||
)
|
||||
logger.info(f"Ride {instance.pk} closing date reached, " f"transitioning to {target_status}")
|
||||
|
||||
# Try to use FSM transition method if available
|
||||
transition_method_name = f'transition_to_{target_status.lower()}'
|
||||
transition_method_name = f"transition_to_{target_status.lower()}"
|
||||
if hasattr(instance, transition_method_name):
|
||||
# Check if transition is allowed before attempting
|
||||
if hasattr(instance, 'can_proceed'):
|
||||
can_proceed = getattr(instance, f'can_transition_to_{target_status.lower()}', None)
|
||||
if hasattr(instance, "can_proceed"):
|
||||
can_proceed = getattr(instance, f"can_transition_to_{target_status.lower()}", None)
|
||||
if can_proceed and callable(can_proceed) and not can_proceed():
|
||||
logger.warning(
|
||||
f"FSM transition to {target_status} not allowed "
|
||||
f"for ride {instance.pk}"
|
||||
)
|
||||
logger.warning(f"FSM transition to {target_status} not allowed " f"for ride {instance.pk}")
|
||||
# Fall back to direct status change
|
||||
instance.status = target_status
|
||||
instance.status_since = instance.closing_date
|
||||
@@ -72,13 +67,9 @@ def handle_ride_status(sender, instance, **kwargs):
|
||||
method = getattr(instance, transition_method_name)
|
||||
method()
|
||||
instance.status_since = instance.closing_date
|
||||
logger.info(
|
||||
f"Applied FSM transition to {target_status} for ride {instance.pk}"
|
||||
)
|
||||
logger.info(f"Applied FSM transition to {target_status} for ride {instance.pk}")
|
||||
except Exception as e:
|
||||
logger.exception(
|
||||
f"Failed to apply FSM transition for ride {instance.pk}: {e}"
|
||||
)
|
||||
logger.exception(f"Failed to apply FSM transition for ride {instance.pk}: {e}")
|
||||
# Fall back to direct status change
|
||||
instance.status = target_status
|
||||
instance.status_since = instance.closing_date
|
||||
@@ -101,23 +92,20 @@ def validate_closing_status(sender, instance, **kwargs):
|
||||
if instance.status == "CLOSING":
|
||||
# Ensure post_closing_status is set
|
||||
if not instance.post_closing_status:
|
||||
logger.warning(
|
||||
f"Ride {instance.pk} entering CLOSING without post_closing_status set"
|
||||
)
|
||||
logger.warning(f"Ride {instance.pk} entering CLOSING without post_closing_status set")
|
||||
# Default to SBNO if not set
|
||||
instance.post_closing_status = "SBNO"
|
||||
|
||||
# Ensure closing_date is set
|
||||
if not instance.closing_date:
|
||||
logger.warning(
|
||||
f"Ride {instance.pk} entering CLOSING without closing_date set"
|
||||
)
|
||||
logger.warning(f"Ride {instance.pk} entering CLOSING without closing_date set")
|
||||
# Default to today's date
|
||||
instance.closing_date = timezone.now().date()
|
||||
|
||||
|
||||
# FSM transition signal handlers
|
||||
|
||||
|
||||
def handle_ride_transition_to_closing(instance, source, target, user, **kwargs):
|
||||
"""
|
||||
Validate transition to CLOSING status.
|
||||
@@ -134,20 +122,15 @@ def handle_ride_transition_to_closing(instance, source, target, user, **kwargs):
|
||||
Returns:
|
||||
True if transition should proceed, False to abort.
|
||||
"""
|
||||
if target != 'CLOSING':
|
||||
if target != "CLOSING":
|
||||
return True
|
||||
|
||||
if not instance.post_closing_status:
|
||||
logger.error(
|
||||
f"Cannot transition ride {instance.pk} to CLOSING: "
|
||||
"post_closing_status not set"
|
||||
)
|
||||
logger.error(f"Cannot transition ride {instance.pk} to CLOSING: " "post_closing_status not set")
|
||||
return False
|
||||
|
||||
if not instance.closing_date:
|
||||
logger.warning(
|
||||
f"Ride {instance.pk} transitioning to CLOSING without closing_date"
|
||||
)
|
||||
logger.warning(f"Ride {instance.pk} transitioning to CLOSING without closing_date")
|
||||
|
||||
return True
|
||||
|
||||
@@ -166,45 +149,35 @@ def apply_post_closing_status(instance, user=None):
|
||||
Returns:
|
||||
True if status was applied, False otherwise.
|
||||
"""
|
||||
if instance.status != 'CLOSING':
|
||||
logger.debug(
|
||||
f"Ride {instance.pk} not in CLOSING state, skipping"
|
||||
)
|
||||
if instance.status != "CLOSING":
|
||||
logger.debug(f"Ride {instance.pk} not in CLOSING state, skipping")
|
||||
return False
|
||||
|
||||
target_status = instance.post_closing_status
|
||||
if not target_status:
|
||||
logger.warning(
|
||||
f"Ride {instance.pk} in CLOSING but no post_closing_status set"
|
||||
)
|
||||
logger.warning(f"Ride {instance.pk} in CLOSING but no post_closing_status set")
|
||||
return False
|
||||
|
||||
# Try to use FSM transition
|
||||
transition_method_name = f'transition_to_{target_status.lower()}'
|
||||
transition_method_name = f"transition_to_{target_status.lower()}"
|
||||
if hasattr(instance, transition_method_name):
|
||||
try:
|
||||
method = getattr(instance, transition_method_name)
|
||||
method(user=user)
|
||||
instance.post_closing_status = None
|
||||
instance.save(update_fields=['post_closing_status'])
|
||||
logger.info(
|
||||
f"Applied post_closing_status {target_status} to ride {instance.pk}"
|
||||
)
|
||||
instance.save(update_fields=["post_closing_status"])
|
||||
logger.info(f"Applied post_closing_status {target_status} to ride {instance.pk}")
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.exception(
|
||||
f"Failed to apply post_closing_status for ride {instance.pk}: {e}"
|
||||
)
|
||||
logger.exception(f"Failed to apply post_closing_status for ride {instance.pk}: {e}")
|
||||
return False
|
||||
else:
|
||||
# Direct status change
|
||||
instance.status = target_status
|
||||
instance.post_closing_status = None
|
||||
instance.status_since = timezone.now().date()
|
||||
instance.save(update_fields=['status', 'post_closing_status', 'status_since'])
|
||||
logger.info(
|
||||
f"Applied post_closing_status {target_status} to ride {instance.pk} (direct)"
|
||||
)
|
||||
instance.save(update_fields=["status", "post_closing_status", "status_since"])
|
||||
logger.info(f"Applied post_closing_status {target_status} to ride {instance.pk} (direct)")
|
||||
return True
|
||||
|
||||
|
||||
@@ -212,7 +185,8 @@ def apply_post_closing_status(instance, user=None):
|
||||
# Computed Field Maintenance Signal Handlers
|
||||
# =============================================================================
|
||||
|
||||
@receiver(post_save, sender='parks.Park')
|
||||
|
||||
@receiver(post_save, sender="parks.Park")
|
||||
def update_ride_search_text_on_park_change(sender, instance, **kwargs):
|
||||
"""
|
||||
Update ride search_text when park name or location changes.
|
||||
@@ -227,7 +201,7 @@ def update_ride_search_text_on_park_change(sender, instance, **kwargs):
|
||||
logger.exception(f"Failed to update ride search_text on park change: {e}")
|
||||
|
||||
|
||||
@receiver(post_save, sender='parks.Company')
|
||||
@receiver(post_save, sender="parks.Company")
|
||||
def update_ride_search_text_on_company_change(sender, instance, **kwargs):
|
||||
"""
|
||||
Update ride search_text when manufacturer/designer name changes.
|
||||
@@ -248,7 +222,7 @@ def update_ride_search_text_on_company_change(sender, instance, **kwargs):
|
||||
logger.exception(f"Failed to update ride search_text on company change: {e}")
|
||||
|
||||
|
||||
@receiver(post_save, sender='rides.RideModel')
|
||||
@receiver(post_save, sender="rides.RideModel")
|
||||
def update_ride_search_text_on_ride_model_change(sender, instance, **kwargs):
|
||||
"""
|
||||
Update ride search_text when ride model name changes.
|
||||
|
||||
Reference in New Issue
Block a user