fill aircraft type/registration and terminal/gate from AeroDataBox

This commit is contained in:
2026-09-15 22:58:54 +10:00
parent 480b896d39
commit 4ed4dd1ec2
6 changed files with 391 additions and 29 deletions
+30 -3
View File
@@ -131,8 +131,14 @@ def build_save_payload(row: dict, existing_id: int | None) -> dict:
"datePrecision": "day",
"flightNumber": flight_number or None,
"airline": row.get("operating_carrier_icao"),
"aircraft": None,
"aircraftReg": None,
# ICAO *type* code (e.g. 'B38M'); AirTrail validates it against its
# own aircraft table and 500s the whole save if it doesn't know it,
# so save_flight() drops it and retries rather than losing the import.
"aircraft": row.get("aircraft_icao"),
"aircraftReg": row.get("aircraft_reg"),
# Seat position and flight reason are deliberately left unset - the
# BCBP carries neither and no API knows them, so they stay a manual
# choice in AirTrail rather than a guess made here.
"flightReason": None,
"note": f"Imported via boarding-pass-pipeline (PNR {row.get('pnr') or 'unknown'})",
"seats": [
@@ -171,6 +177,15 @@ def build_save_payload(row: dict, existing_id: int | None) -> dict:
payload["landingActual"] = row["landing_actual"]
payload["landingActualTime"] = _hhmm(row["landing_actual"])
for payload_key, row_key in (
("departureTerminal", "departure_terminal"),
("departureGate", "departure_gate"),
("arrivalTerminal", "arrival_terminal"),
("arrivalGate", "arrival_gate"),
):
if row.get(row_key):
payload[payload_key] = row[row_key]
coordinates = row.get("track_coordinates")
if coordinates:
times = row.get("track_times")
@@ -192,7 +207,19 @@ def build_save_payload(row: dict, existing_id: int | None) -> dict:
def save_flight(payload: dict) -> dict:
return _request("POST", "/api/flight/save", payload)
try:
return _request("POST", "/api/flight/save", payload)
except AirTrailError as e:
# AirTrail resolves `aircraft` against the `icao` column of its own
# aircraft table and fails the ENTIRE save if the type code isn't
# there (apiError('Invalid aircraft'), HTTP 500). The type is a
# nicety; the flight, its times and its track are not. Drop it and
# retry once rather than losing the import over an unknown variant.
if "Invalid aircraft" in str(e) and payload.get("aircraft"):
retry = dict(payload)
retry["aircraft"] = None
return _request("POST", "/api/flight/save", retry)
raise
def approve_flight(flight_id: int) -> tuple[bool, str]: