feat: Implement initial schema and add various API, service, and management command enhancements across the application.

This commit is contained in:
pacnpal
2026-01-01 15:13:01 -05:00
parent c95f99ca10
commit b243b17af7
413 changed files with 11164 additions and 17433 deletions

View File

@@ -24,17 +24,11 @@ class Company(TrackedModel):
website = models.URLField(blank=True, help_text="Company website URL")
# General company info
founded_date = models.DateField(
null=True, blank=True, help_text="Date the company was founded"
)
founded_date = models.DateField(null=True, blank=True, help_text="Date the company was founded")
# Manufacturer-specific fields
rides_count = models.IntegerField(
default=0, help_text="Number of rides manufactured (auto-calculated)"
)
coasters_count = models.IntegerField(
default=0, help_text="Number of coasters manufactured (auto-calculated)"
)
rides_count = models.IntegerField(default=0, help_text="Number of rides manufactured (auto-calculated)")
coasters_count = models.IntegerField(default=0, help_text="Number of coasters manufactured (auto-calculated)")
# Frontend URL
url = models.URLField(blank=True, help_text="Frontend URL for this company")
@@ -50,9 +44,7 @@ class Company(TrackedModel):
# CRITICAL: Only MANUFACTURER and DESIGNER are for rides domain
# OPERATOR and PROPERTY_OWNER are for parks domain and handled separately
if self.roles:
frontend_domain = getattr(
settings, "FRONTEND_DOMAIN", "https://thrillwiki.com"
)
frontend_domain = getattr(settings, "FRONTEND_DOMAIN", "https://thrillwiki.com")
primary_role = self.roles[0] # Use first role as primary
if primary_role == "MANUFACTURER":
@@ -76,12 +68,9 @@ class Company(TrackedModel):
# Check pghistory first
try:
from django.apps import apps
history_model = apps.get_model('rides', f'{cls.__name__}Event')
history_entry = (
history_model.objects.filter(slug=slug)
.order_by("-pgh_created_at")
.first()
)
history_model = apps.get_model("rides", f"{cls.__name__}Event")
history_entry = history_model.objects.filter(slug=slug).order_by("-pgh_created_at").first()
if history_entry:
return cls.objects.get(id=history_entry.pgh_obj_id), True
except LookupError:
@@ -90,12 +79,10 @@ class Company(TrackedModel):
# Check manual slug history as fallback
try:
historical = HistoricalSlug.objects.get(
content_type__model="company", slug=slug
)
historical = HistoricalSlug.objects.get(content_type__model="company", slug=slug)
return cls.objects.get(pk=historical.object_id), True
except (HistoricalSlug.DoesNotExist, cls.DoesNotExist):
raise cls.DoesNotExist("No company found with this slug")
raise cls.DoesNotExist("No company found with this slug") from None
class Meta(TrackedModel.Meta):
app_label = "rides"

View File

@@ -27,27 +27,17 @@ class RideCredit(TrackedModel):
)
# Credit Details
count = models.PositiveIntegerField(
default=1, help_text="Number of times ridden"
)
count = models.PositiveIntegerField(default=1, help_text="Number of times ridden")
rating = models.IntegerField(
null=True,
blank=True,
validators=[MinValueValidator(1), MaxValueValidator(5)],
help_text="Personal rating (1-5)",
)
first_ridden_at = models.DateField(
null=True, blank=True, help_text="Date of first ride"
)
last_ridden_at = models.DateField(
null=True, blank=True, help_text="Date of most recent ride"
)
notes = models.TextField(
blank=True, help_text="Personal notes about the experience"
)
display_order = models.PositiveIntegerField(
default=0, help_text="User-defined display order for drag-drop sorting"
)
first_ridden_at = models.DateField(null=True, blank=True, help_text="Date of first ride")
last_ridden_at = models.DateField(null=True, blank=True, help_text="Date of most recent ride")
notes = models.TextField(blank=True, help_text="Personal notes about the experience")
display_order = models.PositiveIntegerField(default=0, help_text="User-defined display order for drag-drop sorting")
class Meta(TrackedModel.Meta):
verbose_name = "Ride Credit"

View File

@@ -12,9 +12,7 @@ class RideLocation(models.Model):
"""
# Relationships
ride = models.OneToOneField(
"rides.Ride", on_delete=models.CASCADE, related_name="ride_location"
)
ride = models.OneToOneField("rides.Ride", on_delete=models.CASCADE, related_name="ride_location")
# Optional Spatial Data - keep it simple with single point
point = gis_models.PointField(
@@ -29,9 +27,7 @@ class RideLocation(models.Model):
max_length=100,
blank=True,
db_index=True,
help_text=(
"Themed area or land within the park (e.g., 'Frontierland', 'Tomorrowland')"
),
help_text=("Themed area or land within the park (e.g., 'Frontierland', 'Tomorrowland')"),
)
# General notes field to match database schema

View File

@@ -35,14 +35,12 @@ def ride_photo_upload_path(instance: models.Model, filename: str) -> str:
class RidePhoto(TrackedModel):
"""Photo model specific to rides."""
ride = models.ForeignKey(
"rides.Ride", on_delete=models.CASCADE, related_name="photos"
)
ride = models.ForeignKey("rides.Ride", on_delete=models.CASCADE, related_name="photos")
image = models.ForeignKey(
'django_cloudflareimages_toolkit.CloudflareImage',
"django_cloudflareimages_toolkit.CloudflareImage",
on_delete=models.CASCADE,
help_text="Ride photo stored on Cloudflare Images"
help_text="Ride photo stored on Cloudflare Images",
)
caption = models.CharField(max_length=255, blank=True)
@@ -56,7 +54,7 @@ class RidePhoto(TrackedModel):
domain="rides",
max_length=50,
default="exterior",
help_text="Type of photo for categorization and display purposes"
help_text="Type of photo for categorization and display purposes",
)
# Metadata
@@ -100,9 +98,7 @@ class RidePhoto(TrackedModel):
# Set default caption if not provided
if not self.caption and self.uploaded_by:
self.caption = MediaService.generate_default_caption(
self.uploaded_by.username
)
self.caption = MediaService.generate_default_caption(self.uploaded_by.username)
# If this is marked as primary, unmark other primary photos for this ride
if self.is_primary:

View File

@@ -22,17 +22,12 @@ class RideRanking(models.Model):
"""
ride = models.OneToOneField(
"rides.Ride", on_delete=models.CASCADE, related_name="ranking",
help_text="Ride this ranking entry describes"
"rides.Ride", on_delete=models.CASCADE, related_name="ranking", help_text="Ride this ranking entry describes"
)
# Core ranking metrics
rank = models.PositiveIntegerField(
db_index=True, help_text="Overall rank position (1 = best)"
)
wins = models.PositiveIntegerField(
default=0, help_text="Number of rides this ride beats in pairwise comparisons"
)
rank = models.PositiveIntegerField(db_index=True, help_text="Overall rank position (1 = best)")
wins = models.PositiveIntegerField(default=0, help_text="Number of rides this ride beats in pairwise comparisons")
losses = models.PositiveIntegerField(
default=0,
help_text="Number of rides that beat this ride in pairwise comparisons",
@@ -66,9 +61,7 @@ class RideRanking(models.Model):
)
# Metadata
last_calculated = models.DateTimeField(
default=timezone.now, help_text="When this ranking was last calculated"
)
last_calculated = models.DateTimeField(default=timezone.now, help_text="When this ranking was last calculated")
calculation_version = models.CharField(
max_length=10, default="1.0", help_text="Algorithm version used for calculation"
)
@@ -85,8 +78,7 @@ class RideRanking(models.Model):
constraints = [
models.CheckConstraint(
name="rideranking_winning_percentage_range",
check=models.Q(winning_percentage__gte=0)
& models.Q(winning_percentage__lte=1),
check=models.Q(winning_percentage__gte=0) & models.Q(winning_percentage__lte=1),
violation_error_message="Winning percentage must be between 0 and 1",
),
models.CheckConstraint(
@@ -115,23 +107,13 @@ class RidePairComparison(models.Model):
(users who have rated both rides). It's used to speed up ranking calculations.
"""
ride_a = models.ForeignKey(
"rides.Ride", on_delete=models.CASCADE, related_name="comparisons_as_a"
)
ride_b = models.ForeignKey(
"rides.Ride", on_delete=models.CASCADE, related_name="comparisons_as_b"
)
ride_a = models.ForeignKey("rides.Ride", on_delete=models.CASCADE, related_name="comparisons_as_a")
ride_b = models.ForeignKey("rides.Ride", on_delete=models.CASCADE, related_name="comparisons_as_b")
# Comparison results
ride_a_wins = models.PositiveIntegerField(
default=0, help_text="Number of mutual riders who rated ride_a higher"
)
ride_b_wins = models.PositiveIntegerField(
default=0, help_text="Number of mutual riders who rated ride_b higher"
)
ties = models.PositiveIntegerField(
default=0, help_text="Number of mutual riders who rated both rides equally"
)
ride_a_wins = models.PositiveIntegerField(default=0, help_text="Number of mutual riders who rated ride_a higher")
ride_b_wins = models.PositiveIntegerField(default=0, help_text="Number of mutual riders who rated ride_b higher")
ties = models.PositiveIntegerField(default=0, help_text="Number of mutual riders who rated both rides equally")
# Metrics
mutual_riders_count = models.PositiveIntegerField(
@@ -153,9 +135,7 @@ class RidePairComparison(models.Model):
)
# Metadata
last_calculated = models.DateTimeField(
auto_now=True, help_text="When this comparison was last calculated"
)
last_calculated = models.DateTimeField(auto_now=True, help_text="When this comparison was last calculated")
class Meta:
verbose_name = "Ride Pair Comparison"
@@ -197,14 +177,10 @@ class RankingSnapshot(models.Model):
This allows us to show ranking trends and movements.
"""
ride = models.ForeignKey(
"rides.Ride", on_delete=models.CASCADE, related_name="ranking_history"
)
ride = models.ForeignKey("rides.Ride", on_delete=models.CASCADE, related_name="ranking_history")
rank = models.PositiveIntegerField()
winning_percentage = models.DecimalField(max_digits=5, decimal_places=4)
snapshot_date = models.DateField(
db_index=True, help_text="Date when this ranking snapshot was taken"
)
snapshot_date = models.DateField(db_index=True, help_text="Date when this ranking snapshot was taken")
class Meta:
verbose_name = "Ranking Snapshot"

View File

@@ -12,15 +12,9 @@ class RideReview(TrackedModel):
A review of a ride.
"""
ride = models.ForeignKey(
"rides.Ride", on_delete=models.CASCADE, related_name="reviews"
)
user = models.ForeignKey(
"accounts.User", on_delete=models.CASCADE, related_name="ride_reviews"
)
rating = models.PositiveSmallIntegerField(
validators=[MinValueValidator(1), MaxValueValidator(10)]
)
ride = models.ForeignKey("rides.Ride", on_delete=models.CASCADE, related_name="reviews")
user = models.ForeignKey("accounts.User", on_delete=models.CASCADE, related_name="ride_reviews")
rating = models.PositiveSmallIntegerField(validators=[MinValueValidator(1), MaxValueValidator(10)])
title = models.CharField(max_length=200)
content = models.TextField()
visit_date = models.DateField()
@@ -63,10 +57,7 @@ class RideReview(TrackedModel):
name="ride_review_moderation_consistency",
check=models.Q(moderated_by__isnull=True, moderated_at__isnull=True)
| models.Q(moderated_by__isnull=False, moderated_at__isnull=False),
violation_error_message=(
"Moderated reviews must have both moderator and moderation "
"timestamp"
),
violation_error_message=("Moderated reviews must have both moderator and moderation " "timestamp"),
),
]

View File

@@ -1,5 +1,4 @@
import contextlib
from typing import TYPE_CHECKING
import pghistory
from django.contrib.auth.models import AbstractBaseUser
@@ -14,10 +13,6 @@ from config.django import base as settings
from .company import Company
if TYPE_CHECKING:
from .rides import RollerCoasterStats
@pghistory.track()
class RideModel(TrackedModel):
@@ -30,9 +25,7 @@ class RideModel(TrackedModel):
"""
name = models.CharField(max_length=255, help_text="Name of the ride model")
slug = models.SlugField(
max_length=255, help_text="URL-friendly identifier (unique within manufacturer)"
)
slug = models.SlugField(max_length=255, help_text="URL-friendly identifier (unique within manufacturer)")
manufacturer = models.ForeignKey(
Company,
on_delete=models.SET_NULL,
@@ -42,9 +35,7 @@ class RideModel(TrackedModel):
limit_choices_to={"roles__contains": ["MANUFACTURER"]},
help_text="Primary manufacturer of this ride model",
)
description = models.TextField(
blank=True, help_text="Detailed description of the ride model"
)
description = models.TextField(blank=True, help_text="Detailed description of the ride model")
category = RichChoiceField(
choice_group="categories",
domain="rides",
@@ -125,9 +116,7 @@ class RideModel(TrackedModel):
blank=True,
help_text="Year of last installation of this model (if discontinued)",
)
is_discontinued = models.BooleanField(
default=False, help_text="Whether this model is no longer being manufactured"
)
is_discontinued = models.BooleanField(default=False, help_text="Whether this model is no longer being manufactured")
total_installations = models.PositiveIntegerField(
default=0, help_text="Total number of installations worldwide (auto-calculated)"
)
@@ -156,9 +145,7 @@ class RideModel(TrackedModel):
)
# SEO and metadata
meta_title = models.CharField(
max_length=60, blank=True, help_text="SEO meta title (auto-generated if blank)"
)
meta_title = models.CharField(max_length=60, blank=True, help_text="SEO meta title (auto-generated if blank)")
meta_description = models.CharField(
max_length=160,
blank=True,
@@ -175,25 +162,21 @@ class RideModel(TrackedModel):
constraints = [
# Unique constraints (replacing unique_together for better error messages)
models.UniqueConstraint(
fields=['manufacturer', 'name'],
name='ridemodel_manufacturer_name_unique',
violation_error_message='A ride model with this name already exists for this manufacturer'
fields=["manufacturer", "name"],
name="ridemodel_manufacturer_name_unique",
violation_error_message="A ride model with this name already exists for this manufacturer",
),
models.UniqueConstraint(
fields=['manufacturer', 'slug'],
name='ridemodel_manufacturer_slug_unique',
violation_error_message='A ride model with this slug already exists for this manufacturer'
fields=["manufacturer", "slug"],
name="ridemodel_manufacturer_slug_unique",
violation_error_message="A ride model with this slug already exists for this manufacturer",
),
# Height range validation
models.CheckConstraint(
name="ride_model_height_range_logical",
condition=models.Q(typical_height_range_min_ft__isnull=True)
| models.Q(typical_height_range_max_ft__isnull=True)
| models.Q(
typical_height_range_min_ft__lte=models.F(
"typical_height_range_max_ft"
)
),
| models.Q(typical_height_range_min_ft__lte=models.F("typical_height_range_max_ft")),
violation_error_message="Minimum height cannot exceed maximum height",
),
# Speed range validation
@@ -201,11 +184,7 @@ class RideModel(TrackedModel):
name="ride_model_speed_range_logical",
condition=models.Q(typical_speed_range_min_mph__isnull=True)
| models.Q(typical_speed_range_max_mph__isnull=True)
| models.Q(
typical_speed_range_min_mph__lte=models.F(
"typical_speed_range_max_mph"
)
),
| models.Q(typical_speed_range_min_mph__lte=models.F("typical_speed_range_max_mph")),
violation_error_message="Minimum speed cannot exceed maximum speed",
),
# Capacity range validation
@@ -213,11 +192,7 @@ class RideModel(TrackedModel):
name="ride_model_capacity_range_logical",
condition=models.Q(typical_capacity_range_min__isnull=True)
| models.Q(typical_capacity_range_max__isnull=True)
| models.Q(
typical_capacity_range_min__lte=models.F(
"typical_capacity_range_max"
)
),
| models.Q(typical_capacity_range_min__lte=models.F("typical_capacity_range_max")),
violation_error_message="Minimum capacity cannot exceed maximum capacity",
),
# Installation years validation
@@ -225,27 +200,19 @@ class RideModel(TrackedModel):
name="ride_model_installation_years_logical",
condition=models.Q(first_installation_year__isnull=True)
| models.Q(last_installation_year__isnull=True)
| models.Q(
first_installation_year__lte=models.F("last_installation_year")
),
| models.Q(first_installation_year__lte=models.F("last_installation_year")),
violation_error_message="First installation year cannot be after last installation year",
),
]
def __str__(self) -> str:
return (
self.name
if not self.manufacturer
else f"{self.manufacturer.name} {self.name}"
)
return self.name if not self.manufacturer else f"{self.manufacturer.name} {self.name}"
def clean(self) -> None:
"""Validate RideModel business rules."""
super().clean()
if self.is_discontinued and not self.last_installation_year:
raise ValidationError({
'last_installation_year': 'Discontinued models must have a last installation year'
})
raise ValidationError({"last_installation_year": "Discontinued models must have a last installation year"})
def save(self, *args, **kwargs) -> None:
if not self.slug:
@@ -257,11 +224,7 @@ class RideModel(TrackedModel):
# Ensure uniqueness within the same manufacturer
counter = 1
while (
RideModel.objects.filter(manufacturer=self.manufacturer, slug=self.slug)
.exclude(pk=self.pk)
.exists()
):
while RideModel.objects.filter(manufacturer=self.manufacturer, slug=self.slug).exclude(pk=self.pk).exists():
self.slug = f"{base_slug}-{counter}"
counter += 1
@@ -269,16 +232,12 @@ class RideModel(TrackedModel):
if not self.meta_title:
self.meta_title = str(self)[:60]
if not self.meta_description:
desc = (
f"{self} - {self.description[:100]}" if self.description else str(self)
)
desc = f"{self} - {self.description[:100]}" if self.description else str(self)
self.meta_description = desc[:160]
# Generate frontend URL
if self.manufacturer:
frontend_domain = getattr(
settings, "FRONTEND_DOMAIN", "https://thrillwiki.com"
)
frontend_domain = getattr(settings, "FRONTEND_DOMAIN", "https://thrillwiki.com")
self.url = f"{frontend_domain}/rides/manufacturers/{self.manufacturer.slug}/{self.slug}/"
super().save(*args, **kwargs)
@@ -342,9 +301,7 @@ class RideModelVariant(TrackedModel):
help_text="Base ride model this variant belongs to",
)
name = models.CharField(max_length=255, help_text="Name of this variant")
description = models.TextField(
blank=True, help_text="Description of variant differences"
)
description = models.TextField(blank=True, help_text="Description of variant differences")
# Variant-specific specifications
min_height_ft = models.DecimalField(
@@ -402,16 +359,12 @@ class RideModelPhoto(TrackedModel):
help_text="Ride model this photo belongs to",
)
image = models.ForeignKey(
'django_cloudflareimages_toolkit.CloudflareImage',
"django_cloudflareimages_toolkit.CloudflareImage",
on_delete=models.CASCADE,
help_text="Photo of the ride model stored on Cloudflare Images"
)
caption = models.CharField(
max_length=500, blank=True, help_text="Photo caption or description"
)
alt_text = models.CharField(
max_length=255, blank=True, help_text="Alternative text for accessibility"
help_text="Photo of the ride model stored on Cloudflare Images",
)
caption = models.CharField(max_length=500, blank=True, help_text="Photo caption or description")
alt_text = models.CharField(max_length=255, blank=True, help_text="Alternative text for accessibility")
# Photo metadata
photo_type = RichChoiceField(
@@ -422,18 +375,12 @@ class RideModelPhoto(TrackedModel):
help_text="Type of photo for categorization and display purposes",
)
is_primary = models.BooleanField(
default=False, help_text="Whether this is the primary photo for the ride model"
)
is_primary = models.BooleanField(default=False, help_text="Whether this is the primary photo for the ride model")
# Attribution
photographer = models.CharField(
max_length=255, blank=True, help_text="Name of the photographer"
)
photographer = models.CharField(max_length=255, blank=True, help_text="Name of the photographer")
source = models.CharField(max_length=255, blank=True, help_text="Source of the photo")
copyright_info = models.CharField(
max_length=255, blank=True, help_text="Copyright information"
)
copyright_info = models.CharField(max_length=255, blank=True, help_text="Copyright information")
class Meta(TrackedModel.Meta):
verbose_name = "Ride Model Photo"
@@ -446,9 +393,9 @@ class RideModelPhoto(TrackedModel):
def save(self, *args, **kwargs) -> None:
# Ensure only one primary photo per ride model
if self.is_primary:
RideModelPhoto.objects.filter(
ride_model=self.ride_model, is_primary=True
).exclude(pk=self.pk).update(is_primary=False)
RideModelPhoto.objects.filter(ride_model=self.ride_model, is_primary=True).exclude(pk=self.pk).update(
is_primary=False
)
super().save(*args, **kwargs)
@@ -474,15 +421,9 @@ class RideModelTechnicalSpec(TrackedModel):
)
spec_name = models.CharField(max_length=100, help_text="Name of the specification")
spec_value = models.CharField(
max_length=255, help_text="Value of the specification"
)
spec_unit = models.CharField(
max_length=20, blank=True, help_text="Unit of measurement"
)
notes = models.TextField(
blank=True, help_text="Additional notes about this specification"
)
spec_value = models.CharField(max_length=255, help_text="Value of the specification")
spec_unit = models.CharField(max_length=20, blank=True, help_text="Unit of measurement")
notes = models.TextField(blank=True, help_text="Additional notes about this specification")
class Meta(TrackedModel.Meta):
verbose_name = "Ride Model Technical Specification"
@@ -503,17 +444,15 @@ class Ride(StateMachineMixin, TrackedModel):
jobs. Use selectors or annotations for real-time calculations if needed.
"""
if TYPE_CHECKING:
coaster_stats: 'RollerCoasterStats'
# Type hint for the reverse relation from RollerCoasterStats
coaster_stats: "RollerCoasterStats"
state_field_name = "status"
name = models.CharField(max_length=255)
slug = models.SlugField(max_length=255)
description = models.TextField(blank=True)
park = models.ForeignKey(
"parks.Park", on_delete=models.CASCADE, related_name="rides"
)
park = models.ForeignKey("parks.Park", on_delete=models.CASCADE, related_name="rides")
park_area = models.ForeignKey(
"parks.ParkArea",
on_delete=models.SET_NULL,
@@ -527,7 +466,7 @@ class Ride(StateMachineMixin, TrackedModel):
max_length=2,
default="",
blank=True,
help_text="Ride category classification"
help_text="Ride category classification",
)
manufacturer = models.ForeignKey(
Company,
@@ -558,7 +497,7 @@ class Ride(StateMachineMixin, TrackedModel):
domain="rides",
max_length=20,
default="OPERATING",
help_text="Current operational status of the ride"
help_text="Current operational status of the ride",
)
post_closing_status = RichChoiceField(
choice_group="post_closing_statuses",
@@ -575,9 +514,7 @@ class Ride(StateMachineMixin, TrackedModel):
max_height_in = models.PositiveIntegerField(null=True, blank=True)
capacity_per_hour = models.PositiveIntegerField(null=True, blank=True)
ride_duration_seconds = models.PositiveIntegerField(null=True, blank=True)
average_rating = models.DecimalField(
max_digits=3, decimal_places=2, null=True, blank=True
)
average_rating = models.DecimalField(max_digits=3, decimal_places=2, null=True, blank=True)
# Computed fields for hybrid filtering
opening_year = models.IntegerField(null=True, blank=True, db_index=True)
@@ -603,9 +540,7 @@ class Ride(StateMachineMixin, TrackedModel):
# Frontend URL
url = models.URLField(blank=True, help_text="Frontend URL for this ride")
park_url = models.URLField(
blank=True, help_text="Frontend URL for this ride's park"
)
park_url = models.URLField(blank=True, help_text="Frontend URL for this ride's park")
class Meta(TrackedModel.Meta):
verbose_name = "Ride"
@@ -635,17 +570,13 @@ class Ride(StateMachineMixin, TrackedModel):
name="ride_min_height_reasonable",
condition=models.Q(min_height_in__isnull=True)
| (models.Q(min_height_in__gte=30) & models.Q(min_height_in__lte=90)),
violation_error_message=(
"Minimum height must be between 30 and 90 inches"
),
violation_error_message=("Minimum height must be between 30 and 90 inches"),
),
models.CheckConstraint(
name="ride_max_height_reasonable",
condition=models.Q(max_height_in__isnull=True)
| (models.Q(max_height_in__gte=30) & models.Q(max_height_in__lte=90)),
violation_error_message=(
"Maximum height must be between 30 and 90 inches"
),
violation_error_message=("Maximum height must be between 30 and 90 inches"),
),
# Business rule: Rating must be between 1 and 10
models.CheckConstraint(
@@ -657,14 +588,12 @@ class Ride(StateMachineMixin, TrackedModel):
# Business rule: Capacity and duration must be positive
models.CheckConstraint(
name="ride_capacity_positive",
condition=models.Q(capacity_per_hour__isnull=True)
| models.Q(capacity_per_hour__gt=0),
condition=models.Q(capacity_per_hour__isnull=True) | models.Q(capacity_per_hour__gt=0),
violation_error_message="Hourly capacity must be positive",
),
models.CheckConstraint(
name="ride_duration_positive",
condition=models.Q(ride_duration_seconds__isnull=True)
| models.Q(ride_duration_seconds__gt=0),
condition=models.Q(ride_duration_seconds__isnull=True) | models.Q(ride_duration_seconds__gt=0),
violation_error_message="Ride duration must be positive",
),
]
@@ -699,9 +628,7 @@ class Ride(StateMachineMixin, TrackedModel):
from django.core.exceptions import ValidationError
if not post_closing_status:
raise ValidationError(
"post_closing_status must be set when entering CLOSING status"
)
raise ValidationError("post_closing_status must be set when entering CLOSING status")
self.transition_to_closing(user=user)
self.closing_date = closing_date
self.post_closing_status = post_closing_status
@@ -770,7 +697,7 @@ class Ride(StateMachineMixin, TrackedModel):
self._ensure_unique_slug_in_park()
# Handle park area validation when park changes
if park_changed and self.park_area:
if park_changed and self.park_area: # noqa: SIM102
# Check if park_area belongs to the new park
if self.park_area.park.id != self.park.id:
# Clear park_area if it doesn't belong to the new park
@@ -786,9 +713,7 @@ class Ride(StateMachineMixin, TrackedModel):
# Generate frontend URLs
if self.park:
frontend_domain = getattr(
settings, "FRONTEND_DOMAIN", "https://thrillwiki.com"
)
frontend_domain = getattr(settings, "FRONTEND_DOMAIN", "https://thrillwiki.com")
self.url = f"{frontend_domain}/parks/{self.park.slug}/rides/{self.slug}/"
self.park_url = f"{frontend_domain}/parks/{self.park.slug}/"
@@ -817,7 +742,7 @@ class Ride(StateMachineMixin, TrackedModel):
# Park info
if self.park:
search_parts.append(self.park.name)
if hasattr(self.park, 'location') and self.park.location:
if hasattr(self.park, "location") and self.park.location:
if self.park.location.city:
search_parts.append(self.park.location.city)
if self.park.location.state:
@@ -855,7 +780,7 @@ class Ride(StateMachineMixin, TrackedModel):
# Roller coaster stats if available
try:
if hasattr(self, 'coaster_stats') and self.coaster_stats:
if hasattr(self, "coaster_stats") and self.coaster_stats:
stats = self.coaster_stats
if stats.track_type:
search_parts.append(stats.track_type)
@@ -877,7 +802,7 @@ class Ride(StateMachineMixin, TrackedModel):
# Ignore if coaster_stats doesn't exist or has issues
pass
self.search_text = ' '.join(filter(None, search_parts)).lower()
self.search_text = " ".join(filter(None, search_parts)).lower()
def _ensure_unique_slug_in_park(self) -> None:
"""Ensure the ride's slug is unique within its park."""
@@ -885,11 +810,7 @@ class Ride(StateMachineMixin, TrackedModel):
self.slug = base_slug
counter = 1
while (
Ride.objects.filter(park=self.park, slug=self.slug)
.exclude(pk=self.pk)
.exists()
):
while Ride.objects.filter(park=self.park, slug=self.slug).exclude(pk=self.pk).exists():
self.slug = f"{base_slug}-{counter}"
counter += 1
@@ -921,26 +842,15 @@ class Ride(StateMachineMixin, TrackedModel):
# Return summary of changes
changes = {
'old_park': {
'id': old_park.id,
'name': old_park.name,
'slug': old_park.slug
},
'new_park': {
'id': new_park.id,
'name': new_park.name,
'slug': new_park.slug
},
'url_changed': old_url != self.url,
'old_url': old_url,
'new_url': self.url,
'park_area_cleared': clear_park_area and old_park_area is not None,
'old_park_area': {
'id': old_park_area.id,
'name': old_park_area.name
} if old_park_area else None,
'slug_changed': self.slug != slugify(self.name),
'final_slug': self.slug
"old_park": {"id": old_park.id, "name": old_park.name, "slug": old_park.slug},
"new_park": {"id": new_park.id, "name": new_park.name, "slug": new_park.slug},
"url_changed": old_url != self.url,
"old_url": old_url,
"new_url": self.url,
"park_area_cleared": clear_park_area and old_park_area is not None,
"old_park_area": {"id": old_park_area.id, "name": old_park_area.name} if old_park_area else None,
"slug_changed": self.slug != slugify(self.name),
"final_slug": self.slug,
}
return changes
@@ -963,9 +873,9 @@ class Ride(StateMachineMixin, TrackedModel):
except cls.DoesNotExist:
# Try historical slugs in HistoricalSlug model
content_type = ContentType.objects.get_for_model(cls)
historical_query = HistoricalSlug.objects.filter(
content_type=content_type, slug=slug
).order_by("-created_at")
historical_query = HistoricalSlug.objects.filter(content_type=content_type, slug=slug).order_by(
"-created_at"
)
for historical in historical_query:
try:
@@ -986,14 +896,13 @@ class Ride(StateMachineMixin, TrackedModel):
except cls.DoesNotExist:
continue
raise cls.DoesNotExist("No ride found with this slug")
raise cls.DoesNotExist("No ride found with this slug") from None
@pghistory.track()
class RollerCoasterStats(models.Model):
"""Model for tracking roller coaster specific statistics"""
ride = models.OneToOneField(
Ride,
on_delete=models.CASCADE,
@@ -1021,22 +930,16 @@ class RollerCoasterStats(models.Model):
blank=True,
help_text="Maximum speed in mph",
)
inversions = models.PositiveIntegerField(
default=0, help_text="Number of inversions"
)
ride_time_seconds = models.PositiveIntegerField(
null=True, blank=True, help_text="Duration of the ride in seconds"
)
track_type = models.CharField(
max_length=255, blank=True, help_text="Type of track (e.g., tubular steel, wooden)"
)
inversions = models.PositiveIntegerField(default=0, help_text="Number of inversions")
ride_time_seconds = models.PositiveIntegerField(null=True, blank=True, help_text="Duration of the ride in seconds")
track_type = models.CharField(max_length=255, blank=True, help_text="Type of track (e.g., tubular steel, wooden)")
track_material = RichChoiceField(
choice_group="track_materials",
domain="rides",
max_length=20,
default="STEEL",
blank=True,
help_text="Track construction material type"
help_text="Track construction material type",
)
roller_coaster_type = RichChoiceField(
choice_group="coaster_types",
@@ -1044,7 +947,7 @@ class RollerCoasterStats(models.Model):
max_length=20,
default="SITDOWN",
blank=True,
help_text="Roller coaster type classification"
help_text="Roller coaster type classification",
)
max_drop_height_ft = models.DecimalField(
max_digits=6,
@@ -1058,20 +961,12 @@ class RollerCoasterStats(models.Model):
domain="rides",
max_length=20,
default="CHAIN",
help_text="Propulsion or lift system type"
)
train_style = models.CharField(
max_length=255, blank=True, help_text="Style of train (e.g., floorless, inverted)"
)
trains_count = models.PositiveIntegerField(
null=True, blank=True, help_text="Number of trains"
)
cars_per_train = models.PositiveIntegerField(
null=True, blank=True, help_text="Number of cars per train"
)
seats_per_car = models.PositiveIntegerField(
null=True, blank=True, help_text="Number of seats per car"
help_text="Propulsion or lift system type",
)
train_style = models.CharField(max_length=255, blank=True, help_text="Style of train (e.g., floorless, inverted)")
trains_count = models.PositiveIntegerField(null=True, blank=True, help_text="Number of trains")
cars_per_train = models.PositiveIntegerField(null=True, blank=True, help_text="Number of cars per train")
seats_per_car = models.PositiveIntegerField(null=True, blank=True, help_text="Number of seats per car")
class Meta:
verbose_name = "Roller Coaster Statistics"