Added checks to Flight creation

This commit is contained in:
2025-09-29 23:37:58 -05:00
parent 2bd64cb748
commit 12bedaf6a8
4 changed files with 107 additions and 10 deletions

View File

@@ -18,6 +18,40 @@ class Equipment(models.Model):
"AircraftBase", on_delete=models.CASCADE, related_name="aircraft"
)
owner = models.ForeignKey("Carrier", on_delete=models.CASCADE, related_name="fleet")
base_location = models.ForeignKey(
"Aerodrome",
on_delete=models.PROTECT,
related_name="based_equipment",
help_text="Home base for this equipment",
null=True,
blank=True,
)
def __str__(self):
return f"{self.registration} ({self.model})"
@property
def current_location(self):
"""Get the current location of this equipment based on flight history."""
from .flight import Flight
from django.utils import timezone
now = timezone.now()
# Get the most recent flight relative to now
last_flight = (
self.flights.filter(departure_time__lte=now)
.order_by("-departure_time")
.first()
)
if last_flight:
# Check if plane is currently in flight
if last_flight.departure_time <= now < last_flight.arrival_time:
return "In-Flight"
# Plane has arrived at destination
elif last_flight.arrival_time <= now:
return last_flight.destination
# No flights yet or no flights have departed, equipment is at base
return self.base_location