Added checks to Flight creation
This commit is contained in:
@@ -58,17 +58,53 @@ class Flight(models.Model):
|
||||
return self.departure_time + self.duration
|
||||
|
||||
def clean(self):
|
||||
overlapping = Flight.objects.filter(
|
||||
equipment=self.equipment,
|
||||
departure_time__lt=self.arrival_time,
|
||||
arrival_time__gt=self.departure_time,
|
||||
).exclude(pk=self.pk)
|
||||
|
||||
if overlapping.exists():
|
||||
# Validate flight distance is within aircraft range
|
||||
if self.distance_nm > self.equipment.model.range_nm:
|
||||
raise ValidationError(
|
||||
f"{self.equipment} is already assigned to another flight in this timeframe."
|
||||
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()
|
||||
|
Reference in New Issue
Block a user