1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-07-24 23:39:37 +02:00

fix: switch from json to ijson for efficient country data processing

This commit is contained in:
Sean Morley 2025-01-13 11:23:57 -05:00
parent 6252f3cf14
commit 7f2c3c33a5

View file

@ -3,12 +3,12 @@ from django.core.management.base import BaseCommand
import requests
from worldtravel.models import Country, Region, City
from django.db import transaction
import json
import ijson
from django.conf import settings
COUNTRY_REGION_JSON_VERSION = settings.COUNTRY_REGION_JSON_VERSION
media_root = settings.MEDIA_ROOT
def saveCountryFlag(country_code):
@ -64,10 +64,10 @@ class Command(BaseCommand):
self.stdout.write(self.style.SUCCESS('Latest country, region, and state data already downloaded.'))
return
with open(countries_json_path, 'r') as f:
data = json.load(f)
with transaction.atomic():
# Process data in chunks using ijson
f = open(countries_json_path, 'rb')
parser = ijson.items(f, 'item')
existing_countries = {country.country_code: country for country in Country.objects.all()}
existing_regions = {region.id: region for region in Region.objects.all()}
existing_cities = {city.id: city for city in City.objects.all()}
@ -83,7 +83,7 @@ class Command(BaseCommand):
processed_region_ids = set()
processed_city_ids = set()
for country in data:
for country in parser:
country_code = country['iso2']
country_name = country['name']
country_subregion = country['subregion']