Files
FlightGrid/simulator/management/commands/import_airports.py

33 lines
1.2 KiB
Python

import csv
from django.core.management.base import BaseCommand
from simulator.models import Aerodrome
class Command(BaseCommand):
help = "Import airports from airports.csv"
def handle(self, *args, **options):
with open(
"/home/rbitton/gitea/FlightGrid/data/airports.csv",
newline="",
encoding="utf-8",
) as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
icao = row["ICAO"].strip()
if not icao: # skip entries without ICAO
continue
aerodrome, _ = Aerodrome.objects.update_or_create(
icao=icao,
defaults={
"iata": row["IATA"] or None,
"name": row["Name"],
"city": row["City"],
"country": row["Country"],
"latitude": float(row["Latitude"]),
"longitude": float(row["Longitude"]),
"altitude": int(float(row["Altitude"])),
},
)
self.stdout.write(self.style.SUCCESS("Import complete"))