Added importation of aerodromes
This commit is contained in:
32
simulator/management/commands/import_airports.py
Normal file
32
simulator/management/commands/import_airports.py
Normal file
@@ -0,0 +1,32 @@
|
||||
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"))
|
Reference in New Issue
Block a user