mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-07-23 23:09:37 +02:00
Add capital to countries
This commit is contained in:
parent
4dc11db21d
commit
6ac3f0541f
6 changed files with 39 additions and 5 deletions
|
@ -261,3 +261,5 @@ LOGGING = {
|
|||
},
|
||||
},
|
||||
}
|
||||
|
||||
COUNTRY_REGION_JSON_VERSION = 'v2.4'
|
|
@ -7,6 +7,8 @@ import json
|
|||
|
||||
from django.conf import settings
|
||||
|
||||
COUNTRY_REGION_JSON_VERSION = settings.COUNTRY_REGION_JSON_VERSION
|
||||
|
||||
media_root = settings.MEDIA_ROOT
|
||||
|
||||
def saveCountryFlag(country_code):
|
||||
|
@ -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
|
||||
|
|
|
@ -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),
|
||||
),
|
||||
]
|
|
@ -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"
|
||||
|
|
|
@ -5,6 +5,8 @@
|
|||
import { createEventDispatcher } from 'svelte';
|
||||
const dispatch = createEventDispatcher();
|
||||
|
||||
import MapMarkerStar from '~icons/mdi/map-marker-star';
|
||||
|
||||
export let country: Country;
|
||||
|
||||
async function nav() {
|
||||
|
@ -21,7 +23,12 @@
|
|||
</figure>
|
||||
<div class="card-body">
|
||||
<h2 class="card-title overflow-ellipsis">{country.name}</h2>
|
||||
<div class="badge badge-primary">{country.subregion}</div>
|
||||
{#if country.subregion}
|
||||
<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">
|
||||
<!-- <button class="btn btn-info" on:click={moreInfo}>More Info</button> -->
|
||||
<button class="btn btn-primary" on:click={nav}>Open</button>
|
||||
|
|
|
@ -41,6 +41,7 @@ export type Country = {
|
|||
country_code: string;
|
||||
subregion: string;
|
||||
flag_url: string;
|
||||
capital: string;
|
||||
};
|
||||
|
||||
export type Region = {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue