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

Add capital to countries

This commit is contained in:
Sean Morley 2024-09-11 16:08:10 -04:00
parent 4dc11db21d
commit 6ac3f0541f
6 changed files with 39 additions and 5 deletions

View file

@ -260,4 +260,6 @@ LOGGING = {
'propagate': False,
},
},
}
}
COUNTRY_REGION_JSON_VERSION = 'v2.4'

View file

@ -6,6 +6,8 @@ from django.db import transaction
import json
from django.conf import settings
COUNTRY_REGION_JSON_VERSION = settings.COUNTRY_REGION_JSON_VERSION
media_root = settings.MEDIA_ROOT
@ -38,7 +40,7 @@ class Command(BaseCommand):
def handle(self, *args, **options):
countries_json_path = os.path.join(settings.MEDIA_ROOT, 'countries+regions.json')
if not os.path.exists(countries_json_path):
res = requests.get('https://raw.githubusercontent.com/dr5hn/countries-states-cities-database/master/countries%2Bstates.json')
res = requests.get(f'https://raw.githubusercontent.com/dr5hn/countries-states-cities-database/{COUNTRY_REGION_JSON_VERSION}/countries%2Bstates.json')
if res.status_code == 200:
with open(countries_json_path, 'w') as f:
f.write(res.text)
@ -65,6 +67,7 @@ class Command(BaseCommand):
country_code = country['iso2']
country_name = country['name']
country_subregion = country['subregion']
country_capital = country['capital']
processed_country_codes.add(country_code)
@ -72,12 +75,14 @@ class Command(BaseCommand):
country_obj = existing_countries[country_code]
country_obj.name = country_name
country_obj.subregion = country_subregion
country_obj.capital = country_capital
countries_to_update.append(country_obj)
else:
country_obj = Country(
name=country_name,
country_code=country_code,
subregion=country_subregion
subregion=country_subregion,
capital=country_capital
)
countries_to_create.append(country_obj)
@ -132,7 +137,7 @@ class Command(BaseCommand):
Region.objects.bulk_create(regions_to_create)
# Bulk update existing countries and regions
Country.objects.bulk_update(countries_to_update, ['name', 'subregion'])
Country.objects.bulk_update(countries_to_update, ['name', 'subregion', 'capital'])
Region.objects.bulk_update(regions_to_update, ['name', 'country', 'longitude', 'latitude'])
# Delete countries and regions that are no longer in the data

View file

@ -0,0 +1,18 @@
# Generated by Django 5.0.8 on 2024-09-11 19:59
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('worldtravel', '0009_alter_country_country_code'),
]
operations = [
migrations.AddField(
model_name='country',
name='capital',
field=models.CharField(blank=True, max_length=100, null=True),
),
]

View file

@ -14,6 +14,7 @@ class Country(models.Model):
name = models.CharField(max_length=100)
country_code = models.CharField(max_length=2, unique=True) #iso2 code
subregion = models.CharField(max_length=100, blank=True, null=True)
capital = models.CharField(max_length=100, blank=True, null=True)
class Meta:
verbose_name = "Country"