Trainline stations are imported using a raw query

This commit is contained in:
Emmy D'Anello 2024-05-12 13:49:23 +02:00
parent 68b8606688
commit 0ab4aa7976
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85

View File

@ -3,7 +3,7 @@ from time import time
import requests
from django.core.management import BaseCommand
from django.db import transaction
from django.db import connection, transaction
from tqdm import tqdm
from trainvel.core.models import Station
@ -23,13 +23,15 @@ class Command(BaseCommand):
values = {k.replace(':', '_').replace('normalised_code', 'normalized_code_trainline')
.replace('same_as', 'same_as_id'): convert_value(v)
for k, v in row.items()}
stations.append(Station(**values))
stations.append(values)
Station.objects.all().delete()
if options['verbosity'] >= 1:
self.stdout.write("Deleted all stations.")
start_time = time()
with transaction.atomic():
Station.objects.bulk_create(stations)
if options['verbosity'] >= 1:
self.stdout.write(f"Inserted {len(stations)} stations in {time() - start_time:.2f} seconds.")
print("Deleted all stations.")
with connection.cursor() as cursor:
cursor.execute("BEGIN;")
keys = ", ".join(stations[0].keys())
cursor.executemany(
f"INSERT INTO core_station ({keys}) VALUES ({', '.join(['%s'] * len(values))});",
[list(station.values()) for station in stations],
)
cursor.execute("COMMIT;")