added more flight logic
This commit is contained in:
@@ -7,6 +7,7 @@ from simulator.utils import haversine_nm
|
||||
from datetime import timedelta
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.utils import timezone
|
||||
from decimal import Decimal
|
||||
|
||||
|
||||
class Flight(models.Model):
|
||||
@@ -49,7 +50,7 @@ class Flight(models.Model):
|
||||
|
||||
@property
|
||||
def duration(self) -> timedelta:
|
||||
speed = self.equipment.model.cruise_speed_kt # knots = nm/hr
|
||||
speed = self.equipment.model.cruise_speed_kt
|
||||
hours = self.distance_nm / speed
|
||||
return timedelta(hours=hours)
|
||||
|
||||
@@ -58,15 +59,23 @@ class Flight(models.Model):
|
||||
return self.departure_time + self.duration
|
||||
|
||||
def clean(self):
|
||||
# Validate flight distance is within aircraft range
|
||||
# Validate origin and destination are different
|
||||
if self.origin == self.destination:
|
||||
raise ValidationError("Origin and destination airports cannot be the same.")
|
||||
|
||||
# Validate carrier owns the equipment
|
||||
if self.equipment.owner != self.carrier:
|
||||
raise ValidationError(
|
||||
f"{self.equipment} is owned by {self.equipment.owner}, "
|
||||
f"not {self.carrier}. Cannot assign equipment to this flight."
|
||||
)
|
||||
|
||||
if self.distance_nm > self.equipment.model.range_nm:
|
||||
raise ValidationError(
|
||||
f"Flight distance ({self.distance_nm:.0f} nm) exceeds {self.equipment.model} "
|
||||
f"maximum range ({self.equipment.model.range_nm} nm)."
|
||||
)
|
||||
|
||||
# Validate equipment is at the origin airport
|
||||
# Get the last flight before this one chronologically
|
||||
previous_flight = (
|
||||
Flight.objects.filter(
|
||||
equipment=self.equipment, departure_time__lt=self.departure_time
|
||||
@@ -77,7 +86,6 @@ class Flight(models.Model):
|
||||
)
|
||||
|
||||
if previous_flight:
|
||||
# Equipment must be at the destination of the previous flight
|
||||
if previous_flight.destination != self.origin:
|
||||
raise ValidationError(
|
||||
f"{self.equipment} will be at {previous_flight.destination.icao} "
|
||||
@@ -85,14 +93,12 @@ class Flight(models.Model):
|
||||
f"Cannot depart from {self.origin.icao}."
|
||||
)
|
||||
|
||||
# Ensure enough time between arrival and next departure (at least arrival happens before departure)
|
||||
if previous_flight.arrival_time > self.departure_time:
|
||||
raise ValidationError(
|
||||
f"{self.equipment} arrives at {self.origin.icao} at {previous_flight.arrival_time}. "
|
||||
f"Cannot depart before arrival."
|
||||
)
|
||||
else:
|
||||
# No previous flights, equipment must be at base location
|
||||
if (
|
||||
self.equipment.base_location
|
||||
and self.equipment.base_location != self.origin
|
||||
@@ -102,7 +108,6 @@ class Flight(models.Model):
|
||||
f"First flight must depart from base location, not {self.origin.icao}."
|
||||
)
|
||||
|
||||
# Calculate arrival time for this flight
|
||||
arrival = self.arrival_time
|
||||
|
||||
@property
|
||||
@@ -114,3 +119,16 @@ class Flight(models.Model):
|
||||
return "In-Flight"
|
||||
else:
|
||||
return "Arrived"
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
"""Override save to update equipment cycles and air time when flight completes."""
|
||||
is_new = self.pk is None
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
if not is_new and self.status == "Arrived":
|
||||
if not hasattr(self, "_stats_updated"):
|
||||
flight_hours = Decimal(str(self.duration.total_seconds() / 3600))
|
||||
self.equipment.cycles += 1
|
||||
self.equipment.air_time_hours += flight_hours
|
||||
self.equipment.save(update_fields=["cycles", "air_time_hours"])
|
||||
self._stats_updated = True
|
||||
|
Reference in New Issue
Block a user