1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-07-23 14:59:36 +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

@ -261,3 +261,5 @@ LOGGING = {
}, },
}, },
} }
COUNTRY_REGION_JSON_VERSION = 'v2.4'

View file

@ -7,6 +7,8 @@ import json
from django.conf import settings from django.conf import settings
COUNTRY_REGION_JSON_VERSION = settings.COUNTRY_REGION_JSON_VERSION
media_root = settings.MEDIA_ROOT media_root = settings.MEDIA_ROOT
def saveCountryFlag(country_code): def saveCountryFlag(country_code):
@ -38,7 +40,7 @@ class Command(BaseCommand):
def handle(self, *args, **options): def handle(self, *args, **options):
countries_json_path = os.path.join(settings.MEDIA_ROOT, 'countries+regions.json') countries_json_path = os.path.join(settings.MEDIA_ROOT, 'countries+regions.json')
if not os.path.exists(countries_json_path): 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: if res.status_code == 200:
with open(countries_json_path, 'w') as f: with open(countries_json_path, 'w') as f:
f.write(res.text) f.write(res.text)
@ -65,6 +67,7 @@ class Command(BaseCommand):
country_code = country['iso2'] country_code = country['iso2']
country_name = country['name'] country_name = country['name']
country_subregion = country['subregion'] country_subregion = country['subregion']
country_capital = country['capital']
processed_country_codes.add(country_code) processed_country_codes.add(country_code)
@ -72,12 +75,14 @@ class Command(BaseCommand):
country_obj = existing_countries[country_code] country_obj = existing_countries[country_code]
country_obj.name = country_name country_obj.name = country_name
country_obj.subregion = country_subregion country_obj.subregion = country_subregion
country_obj.capital = country_capital
countries_to_update.append(country_obj) countries_to_update.append(country_obj)
else: else:
country_obj = Country( country_obj = Country(
name=country_name, name=country_name,
country_code=country_code, country_code=country_code,
subregion=country_subregion subregion=country_subregion,
capital=country_capital
) )
countries_to_create.append(country_obj) countries_to_create.append(country_obj)
@ -132,7 +137,7 @@ class Command(BaseCommand):
Region.objects.bulk_create(regions_to_create) Region.objects.bulk_create(regions_to_create)
# Bulk update existing countries and regions # 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']) Region.objects.bulk_update(regions_to_update, ['name', 'country', 'longitude', 'latitude'])
# Delete countries and regions that are no longer in the data # 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) name = models.CharField(max_length=100)
country_code = models.CharField(max_length=2, unique=True) #iso2 code country_code = models.CharField(max_length=2, unique=True) #iso2 code
subregion = models.CharField(max_length=100, blank=True, null=True) subregion = models.CharField(max_length=100, blank=True, null=True)
capital = models.CharField(max_length=100, blank=True, null=True)
class Meta: class Meta:
verbose_name = "Country" verbose_name = "Country"

View file

@ -5,6 +5,8 @@
import { createEventDispatcher } from 'svelte'; import { createEventDispatcher } from 'svelte';
const dispatch = createEventDispatcher(); const dispatch = createEventDispatcher();
import MapMarkerStar from '~icons/mdi/map-marker-star';
export let country: Country; export let country: Country;
async function nav() { async function nav() {
@ -21,7 +23,12 @@
</figure> </figure>
<div class="card-body"> <div class="card-body">
<h2 class="card-title overflow-ellipsis">{country.name}</h2> <h2 class="card-title overflow-ellipsis">{country.name}</h2>
{#if country.subregion}
<div class="badge badge-primary">{country.subregion}</div> <div class="badge badge-primary">{country.subregion}</div>
{/if}
{#if country.capital}
<div class="badge badge-secondary"><MapMarkerStar class="-ml-1 mr-1" />{country.capital}</div>
{/if}
<div class="card-actions justify-end"> <div class="card-actions justify-end">
<!-- <button class="btn btn-info" on:click={moreInfo}>More Info</button> --> <!-- <button class="btn btn-info" on:click={moreInfo}>More Info</button> -->
<button class="btn btn-primary" on:click={nav}>Open</button> <button class="btn btn-primary" on:click={nav}>Open</button>

View file

@ -41,6 +41,7 @@ export type Country = {
country_code: string; country_code: string;
subregion: string; subregion: string;
flag_url: string; flag_url: string;
capital: string;
}; };
export type Region = { export type Region = {