from django.db import models # from .flight import Flight from django.utils import timezone 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="Starting location for this equipment", null=True, blank=True, ) cycles = models.PositiveIntegerField( default=0, help_text="Total number of flight cycles" ) air_time_hours = models.DecimalField( max_digits=10, decimal_places=2, default=0, help_text="Total air time in hours", ) 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.""" now = timezone.now() last_flight = ( self.flights.filter(departure_time__lte=now) .order_by("-departure_time") .first() ) if last_flight: if last_flight.departure_time <= now < last_flight.arrival_time: return "In-Flight" elif last_flight.arrival_time <= now: return last_flight.destination return self.base_location