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
1 changed files with 11 additions and 9 deletions

View File

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