Files
FlightGrid/simulator/models/aircraft.py

58 lines
1.8 KiB
Python

from django.db import models
class AircraftBase(models.Model):
name = models.CharField(max_length=20)
manufacturer = models.CharField(max_length=50)
range_nm = models.IntegerField()
capacity = models.IntegerField()
cruise_speed_kt = models.IntegerField()
def __str__(self):
return f"{self.manufacturer} {self.name}"
class Equipment(models.Model):
registration = models.CharField(max_length=10, unique=True)
model = models.ForeignKey(
"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