117 lines
4.2 KiB
Python
117 lines
4.2 KiB
Python
from django.db import models
|
|
from .carrier import Carrier
|
|
from .aircraft import Equipment
|
|
from .aerodrome import Aerodrome
|
|
from django.core.validators import MinValueValidator, MaxValueValidator
|
|
from simulator.utils import haversine_nm
|
|
from datetime import timedelta
|
|
from django.core.exceptions import ValidationError
|
|
from django.utils import timezone
|
|
|
|
|
|
class Flight(models.Model):
|
|
carrier = models.ForeignKey(
|
|
Carrier, on_delete=models.CASCADE, related_name="flights"
|
|
)
|
|
equipment = models.ForeignKey(
|
|
Equipment, on_delete=models.CASCADE, related_name="flights"
|
|
)
|
|
origin = models.ForeignKey(
|
|
Aerodrome, on_delete=models.CASCADE, related_name="departures"
|
|
)
|
|
destination = models.ForeignKey(
|
|
Aerodrome, on_delete=models.CASCADE, related_name="arrivals"
|
|
)
|
|
|
|
flight_number = models.PositiveIntegerField(
|
|
validators=[MinValueValidator(1), MaxValueValidator(9999)]
|
|
)
|
|
departure_time = models.DateTimeField()
|
|
|
|
class Meta:
|
|
constraints = [
|
|
models.UniqueConstraint(
|
|
fields=["carrier", "flight_number"], name="unique_flight_per_carrier"
|
|
)
|
|
]
|
|
|
|
def __str__(self):
|
|
return f"{self.carrier.icao}{self.flight_number} {self.origin.icao} > {self.destination.icao}"
|
|
|
|
@property
|
|
def distance_nm(self) -> float:
|
|
return haversine_nm(
|
|
self.origin.latitude,
|
|
self.origin.longitude,
|
|
self.destination.latitude,
|
|
self.destination.longitude,
|
|
)
|
|
|
|
@property
|
|
def duration(self) -> timedelta:
|
|
speed = self.equipment.model.cruise_speed_kt # knots = nm/hr
|
|
hours = self.distance_nm / speed
|
|
return timedelta(hours=hours)
|
|
|
|
@property
|
|
def arrival_time(self):
|
|
return self.departure_time + self.duration
|
|
|
|
def clean(self):
|
|
# Validate flight distance is within aircraft range
|
|
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
|
|
)
|
|
.order_by("-departure_time")
|
|
.exclude(pk=self.pk)
|
|
.first()
|
|
)
|
|
|
|
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} "
|
|
f"after flight {previous_flight.carrier.icao}{previous_flight.flight_number}. "
|
|
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
|
|
):
|
|
raise ValidationError(
|
|
f"{self.equipment} is based at {self.equipment.base_location.icao}. "
|
|
f"First flight must depart from base location, not {self.origin.icao}."
|
|
)
|
|
|
|
# Calculate arrival time for this flight
|
|
arrival = self.arrival_time
|
|
|
|
@property
|
|
def status(self):
|
|
now = timezone.now()
|
|
if now < self.departure_time:
|
|
return "Scheduled"
|
|
elif self.departure_time <= now < self.arrival_time:
|
|
return "In-Flight"
|
|
else:
|
|
return "Arrived"
|