mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-08-02 19:55:18 +02:00
feat: Add flag URL to Country type and update CountryCard component
This commit is contained in:
parent
77c11fefea
commit
d9e554ad42
14 changed files with 96 additions and 56 deletions
|
@ -1,10 +1,36 @@
|
|||
# myapp/management/commands/seed.py
|
||||
|
||||
import os
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.contrib.auth import get_user_model
|
||||
import requests
|
||||
from worldtravel.models import Country, Region
|
||||
from django.db import transaction
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
media_root = settings.MEDIA_ROOT
|
||||
|
||||
def saveCountryFlag(country_code):
|
||||
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')
|
||||
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'
|
||||
|
@ -528,8 +554,10 @@ class Command(BaseCommand):
|
|||
defaults={'name': name, 'continent': continent}
|
||||
)
|
||||
if created:
|
||||
saveCountryFlag(country_code)
|
||||
self.stdout.write(f'Inserted {name} into worldtravel countries')
|
||||
else:
|
||||
saveCountryFlag(country_code)
|
||||
self.stdout.write(f'Updated {name} in worldtravel countries')
|
||||
|
||||
def sync_regions(self, regions):
|
||||
|
|
|
@ -1,13 +1,24 @@
|
|||
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('/')
|
||||
|
||||
flag_url = serializers.SerializerMethodField()
|
||||
|
||||
def get_flag_url(self, obj):
|
||||
public_url = self.get_public_url(obj)
|
||||
return public_url + '/media/' + 'flags/' + obj.country_code + '.png'
|
||||
|
||||
class Meta:
|
||||
model = Country
|
||||
fields = '__all__' # Serialize all fields of the Adventure model
|
||||
|
||||
class RegionSerializer(serializers.ModelSerializer):
|
||||
flag_url = ''
|
||||
class Meta:
|
||||
model = Region
|
||||
fields = '__all__' # Serialize all fields of the Adventure model
|
||||
|
|
|
@ -16,7 +16,7 @@ from django.contrib.staticfiles import finders
|
|||
def regions_by_country(request, country_code):
|
||||
# require authentication
|
||||
country = get_object_or_404(Country, country_code=country_code)
|
||||
regions = Region.objects.filter(country=country)
|
||||
regions = Region.objects.filter(country=country).order_by('name')
|
||||
serializer = RegionSerializer(regions, many=True)
|
||||
return Response(serializer.data)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue