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

feat: Add flag URL to Country type and update CountryCard component

This commit is contained in:
Sean Morley 2024-08-05 14:17:41 -04:00
parent 77c11fefea
commit d9e554ad42
14 changed files with 96 additions and 56 deletions

View file

@ -0,0 +1,18 @@
# Generated by Django 5.0.7 on 2024-08-05 17:51
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('adventures', '0018_note_links'),
]
operations = [
migrations.AddField(
model_name='collection',
name='updated_at',
field=models.DateTimeField(auto_now=True),
),
]

View file

@ -71,6 +71,8 @@ class Collection(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
start_date = models.DateField(blank=True, null=True)
end_date = models.DateField(blank=True, null=True)
updated_at = models.DateTimeField(auto_now=True)
# if connected adventures are private and collection is public, raise an error
def clean(self):

View file

@ -98,4 +98,5 @@ class CollectionSerializer(serializers.ModelSerializer):
class Meta:
model = Collection
# fields are all plus the adventures field
fields = ['id', 'description', 'user_id', 'name', 'is_public', 'adventures', 'created_at', 'start_date', 'end_date', 'transportations', 'notes']
fields = ['id', 'description', 'user_id', 'name', 'is_public', 'adventures', 'created_at', 'start_date', 'end_date', 'transportations', 'notes', 'updated_at']
read_only_fields = ['id', 'created_at', 'updated_at']

View file

@ -217,9 +217,9 @@ class CollectionViewSet(viewsets.ModelViewSet):
order_by = self.request.query_params.get('order_by', 'name')
order_direction = self.request.query_params.get('order_direction', 'asc')
valid_order_by = ['name']
valid_order_by = ['name', 'upated_at']
if order_by not in valid_order_by:
order_by = 'name'
order_by = 'updated_at'
if order_direction not in ['asc', 'desc']:
order_direction = 'asc'
@ -228,13 +228,15 @@ class CollectionViewSet(viewsets.ModelViewSet):
if order_by == 'name':
queryset = queryset.annotate(lower_name=Lower('name'))
ordering = 'lower_name'
if order_direction == 'desc':
ordering = f'-{ordering}'
else:
ordering = order_by
order_by == 'updated_at'
ordering = 'updated_at'
if order_direction == 'asc':
ordering = '-updated_at'
if order_direction == 'desc':
ordering = f'-{ordering}'
print(f"Ordering by: {ordering}") # For debugging
#print(f"Ordering by: {ordering}") # For debugging
return queryset.order_by(ordering)

View file

@ -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):

View file

@ -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

View file

@ -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)