mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-07-24 15:29:36 +02:00
Merge pull request #314 from seanmorley15/worldtravel-v2
WorldTravel v2
This commit is contained in:
commit
3c43f3cfad
20 changed files with 387 additions and 97 deletions
|
@ -34,7 +34,7 @@ EOF
|
|||
fi
|
||||
|
||||
# Sync the countries and world travel regions
|
||||
python manage.py worldtravel-seed --force
|
||||
python manage.py download-countries
|
||||
|
||||
# Start Django server
|
||||
python manage.py runserver 0.0.0.0:8000
|
||||
|
|
|
@ -21,8 +21,8 @@ class AdventureAdmin(admin.ModelAdmin):
|
|||
|
||||
|
||||
class CountryAdmin(admin.ModelAdmin):
|
||||
list_display = ('name', 'country_code', 'continent', 'number_of_regions')
|
||||
list_filter = ('continent', 'country_code')
|
||||
list_display = ('name', 'country_code', 'number_of_regions')
|
||||
list_filter = ('subregion',)
|
||||
|
||||
def number_of_regions(self, obj):
|
||||
return Region.objects.filter(country=obj).count()
|
||||
|
@ -32,6 +32,7 @@ class CountryAdmin(admin.ModelAdmin):
|
|||
|
||||
class RegionAdmin(admin.ModelAdmin):
|
||||
list_display = ('name', 'country', 'number_of_visits')
|
||||
list_filter = ('country',)
|
||||
# list_filter = ('country', 'number_of_visits')
|
||||
|
||||
def number_of_visits(self, obj):
|
||||
|
|
|
@ -260,4 +260,6 @@ LOGGING = {
|
|||
'propagate': False,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
COUNTRY_REGION_JSON_VERSION = 'v2.4'
|
|
@ -0,0 +1,147 @@
|
|||
import os
|
||||
from django.core.management.base import BaseCommand
|
||||
import requests
|
||||
from worldtravel.models import Country, Region
|
||||
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
|
||||
|
||||
def saveCountryFlag(country_code):
|
||||
# For standards, use the lowercase country_code
|
||||
country_code = country_code.lower()
|
||||
flags_dir = os.path.join(media_root, 'flags')
|
||||
|
||||
# Check if the flags directory exists, if not, create it
|
||||
if not os.path.exists(flags_dir):
|
||||
os.makedirs(flags_dir)
|
||||
|
||||
# Check if the flag already exists in the media folder
|
||||
flag_path = os.path.join(flags_dir, f'{country_code}.png')
|
||||
if os.path.exists(flag_path):
|
||||
print(f'Flag for {country_code} already exists')
|
||||
return
|
||||
|
||||
res = requests.get(f'https://flagcdn.com/h240/{country_code}.png'.lower())
|
||||
if res.status_code == 200:
|
||||
with open(flag_path, 'wb') as f:
|
||||
f.write(res.content)
|
||||
print(f'Flag for {country_code} downloaded')
|
||||
else:
|
||||
print(f'Error downloading flag for {country_code}')
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Imports the world travel data'
|
||||
|
||||
def handle(self, *args, **options):
|
||||
countries_json_path = os.path.join(settings.MEDIA_ROOT, f'countries+regions-{COUNTRY_REGION_JSON_VERSION}.json')
|
||||
if not os.path.exists(countries_json_path):
|
||||
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)
|
||||
else:
|
||||
self.stdout.write(self.style.ERROR('Error downloading countries+regions.json'))
|
||||
return
|
||||
|
||||
with open(countries_json_path, 'r') as f:
|
||||
data = json.load(f)
|
||||
|
||||
with transaction.atomic():
|
||||
existing_countries = {country.country_code: country for country in Country.objects.all()}
|
||||
existing_regions = {region.id: region for region in Region.objects.all()}
|
||||
|
||||
countries_to_create = []
|
||||
regions_to_create = []
|
||||
countries_to_update = []
|
||||
regions_to_update = []
|
||||
|
||||
processed_country_codes = set()
|
||||
processed_region_ids = set()
|
||||
|
||||
for country in data:
|
||||
country_code = country['iso2']
|
||||
country_name = country['name']
|
||||
country_subregion = country['subregion']
|
||||
country_capital = country['capital']
|
||||
|
||||
processed_country_codes.add(country_code)
|
||||
|
||||
if country_code in existing_countries:
|
||||
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,
|
||||
capital=country_capital
|
||||
)
|
||||
countries_to_create.append(country_obj)
|
||||
|
||||
saveCountryFlag(country_code)
|
||||
self.stdout.write(self.style.SUCCESS(f'Country {country_name} prepared'))
|
||||
|
||||
if country['states']:
|
||||
for state in country['states']:
|
||||
name = state['name']
|
||||
state_id = f"{country_code}-{state['state_code']}"
|
||||
latitude = round(float(state['latitude']), 6) if state['latitude'] else None
|
||||
longitude = round(float(state['longitude']), 6) if state['longitude'] else None
|
||||
|
||||
processed_region_ids.add(state_id)
|
||||
|
||||
if state_id in existing_regions:
|
||||
region_obj = existing_regions[state_id]
|
||||
region_obj.name = name
|
||||
region_obj.country = country_obj
|
||||
region_obj.longitude = longitude
|
||||
region_obj.latitude = latitude
|
||||
regions_to_update.append(region_obj)
|
||||
else:
|
||||
region_obj = Region(
|
||||
id=state_id,
|
||||
name=name,
|
||||
country=country_obj,
|
||||
longitude=longitude,
|
||||
latitude=latitude
|
||||
)
|
||||
regions_to_create.append(region_obj)
|
||||
self.stdout.write(self.style.SUCCESS(f'State {state_id} prepared'))
|
||||
else:
|
||||
state_id = f"{country_code}-00"
|
||||
processed_region_ids.add(state_id)
|
||||
if state_id in existing_regions:
|
||||
region_obj = existing_regions[state_id]
|
||||
region_obj.name = country_name
|
||||
region_obj.country = country_obj
|
||||
regions_to_update.append(region_obj)
|
||||
else:
|
||||
region_obj = Region(
|
||||
id=state_id,
|
||||
name=country_name,
|
||||
country=country_obj
|
||||
)
|
||||
regions_to_create.append(region_obj)
|
||||
self.stdout.write(self.style.SUCCESS(f'Region {state_id} prepared for {country_name}'))
|
||||
|
||||
# Bulk create new countries and regions
|
||||
Country.objects.bulk_create(countries_to_create)
|
||||
Region.objects.bulk_create(regions_to_create)
|
||||
|
||||
# Bulk update existing countries and regions
|
||||
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
|
||||
Country.objects.exclude(country_code__in=processed_country_codes).delete()
|
||||
Region.objects.exclude(id__in=processed_region_ids).delete()
|
||||
|
||||
self.stdout.write(self.style.SUCCESS('All data imported successfully'))
|
|
@ -0,0 +1,22 @@
|
|||
# Generated by Django 5.0.8 on 2024-09-11 02:16
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('worldtravel', '0005_remove_country_geometry_region_geometry'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(
|
||||
model_name='country',
|
||||
name='continent',
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='country',
|
||||
name='subregion',
|
||||
field=models.CharField(blank=True, max_length=100, null=True),
|
||||
),
|
||||
]
|
|
@ -0,0 +1,21 @@
|
|||
# Generated by Django 5.0.8 on 2024-09-11 02:20
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('worldtravel', '0006_remove_country_continent_country_subregion'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(
|
||||
model_name='region',
|
||||
name='geometry',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='region',
|
||||
name='name_en',
|
||||
),
|
||||
]
|
|
@ -0,0 +1,23 @@
|
|||
# Generated by Django 5.0.8 on 2024-09-11 02:29
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('worldtravel', '0007_remove_region_geometry_remove_region_name_en'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='region',
|
||||
name='latitude',
|
||||
field=models.DecimalField(blank=True, decimal_places=6, max_digits=9, null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='region',
|
||||
name='longitude',
|
||||
field=models.DecimalField(blank=True, decimal_places=6, max_digits=9, null=True),
|
||||
),
|
||||
]
|
|
@ -0,0 +1,18 @@
|
|||
# Generated by Django 5.0.8 on 2024-09-11 02:52
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('worldtravel', '0008_region_latitude_region_longitude'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='country',
|
||||
name='country_code',
|
||||
field=models.CharField(max_length=2, unique=True),
|
||||
),
|
||||
]
|
|
@ -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),
|
||||
),
|
||||
]
|
|
@ -9,33 +9,12 @@ User = get_user_model()
|
|||
default_user_id = 1 # Replace with an actual user ID
|
||||
|
||||
class Country(models.Model):
|
||||
AFRICA = 'AF'
|
||||
ANTARCTICA = 'AN'
|
||||
ASIA = 'AS'
|
||||
EUROPE = 'EU'
|
||||
NORTH_AMERICA = 'NA'
|
||||
OCEANIA = 'OC'
|
||||
SOUTH_AMERICA = 'SA'
|
||||
|
||||
CONTINENT_CHOICES = [
|
||||
(AFRICA, 'Africa'),
|
||||
(ANTARCTICA, 'Antarctica'),
|
||||
(ASIA, 'Asia'),
|
||||
(EUROPE, 'Europe'),
|
||||
(NORTH_AMERICA, 'North America'),
|
||||
(OCEANIA, 'Oceania'),
|
||||
(SOUTH_AMERICA, 'South America'),
|
||||
]
|
||||
|
||||
id = models.AutoField(primary_key=True)
|
||||
name = models.CharField(max_length=100)
|
||||
country_code = models.CharField(max_length=2)
|
||||
continent = models.CharField(
|
||||
max_length=2,
|
||||
choices=CONTINENT_CHOICES,
|
||||
default=AFRICA
|
||||
)
|
||||
|
||||
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"
|
||||
|
@ -47,9 +26,9 @@ class Country(models.Model):
|
|||
class Region(models.Model):
|
||||
id = models.CharField(primary_key=True)
|
||||
name = models.CharField(max_length=100)
|
||||
name_en = models.CharField(max_length=100, blank=True, null=True)
|
||||
country = models.ForeignKey(Country, on_delete=models.CASCADE)
|
||||
geometry = gis_models.MultiPolygonField(srid=4326, null=True, blank=True)
|
||||
longitude = models.DecimalField(max_digits=9, decimal_places=6, null=True, blank=True)
|
||||
latitude = models.DecimalField(max_digits=9, decimal_places=6, null=True, blank=True)
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
@ -66,4 +45,4 @@ class VisitedRegion(models.Model):
|
|||
def save(self, *args, **kwargs):
|
||||
if VisitedRegion.objects.filter(user_id=self.user_id, region=self.region).exists():
|
||||
raise ValidationError("Region already visited by user.")
|
||||
super().save(*args, **kwargs)
|
||||
super().save(*args, **kwargs)
|
|
@ -2,7 +2,6 @@ import os
|
|||
from .models import Country, Region, VisitedRegion
|
||||
from rest_framework import serializers
|
||||
|
||||
|
||||
class CountrySerializer(serializers.ModelSerializer):
|
||||
def get_public_url(self, obj):
|
||||
return os.environ.get('PUBLIC_URL', 'http://127.0.0.1:8000').rstrip('/').replace("'", "")
|
||||
|
@ -11,21 +10,25 @@ class CountrySerializer(serializers.ModelSerializer):
|
|||
|
||||
def get_flag_url(self, obj):
|
||||
public_url = self.get_public_url(obj)
|
||||
return public_url + '/media/' + 'flags/' + obj.country_code + '.png'
|
||||
return public_url + '/media/' + 'flags/' + obj.country_code.lower() + '.png'
|
||||
|
||||
class Meta:
|
||||
model = Country
|
||||
fields = '__all__' # Serialize all fields of the Adventure model
|
||||
read_only_fields = ['id', 'name', 'country_code', 'continent', 'flag_url']
|
||||
fields = '__all__'
|
||||
read_only_fields = ['id', 'name', 'country_code', 'subregion', 'flag_url']
|
||||
|
||||
class RegionSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Region
|
||||
fields = '__all__' # Serialize all fields of the Adventure model
|
||||
read_only_fields = ['id', 'name', 'country', 'name_en', 'geometry']
|
||||
fields = '__all__'
|
||||
read_only_fields = ['id', 'name', 'country', 'longitude', 'latitude']
|
||||
|
||||
class VisitedRegionSerializer(serializers.ModelSerializer):
|
||||
longitude = serializers.DecimalField(source='region.longitude', max_digits=9, decimal_places=6, read_only=True)
|
||||
latitude = serializers.DecimalField(source='region.latitude', max_digits=9, decimal_places=6, read_only=True)
|
||||
name = serializers.CharField(source='region.name', read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = VisitedRegion
|
||||
fields = '__all__' # Serialize all fields of the Adventure model
|
||||
read_only_fields = ['user_id', 'id']
|
||||
fields = ['id', 'user_id', 'region', 'longitude', 'latitude', 'name']
|
||||
read_only_fields = ['user_id', 'id', 'longitude', 'latitude', 'name']
|
|
@ -34,7 +34,7 @@ def visits_by_country(request, country_code):
|
|||
return Response(serializer.data)
|
||||
|
||||
class CountryViewSet(viewsets.ReadOnlyModelViewSet):
|
||||
queryset = Country.objects.all()
|
||||
queryset = Country.objects.all().order_by('name')
|
||||
serializer_class = CountrySerializer
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue