From bc1f36bf6c4a18699141fafe1ee2a8cdf8d9d8e7 Mon Sep 17 00:00:00 2001 From: JYYYeung Date: Wed, 26 Feb 2025 10:34:48 +0000 Subject: [PATCH 1/2] feat: show country name in current locale if translation exists --- .gitignore | 1 + .vscode/settings.json | 13 ++++++- Makefile | 35 +++++++++++++++++++ backend/entrypoint.sh | 2 -- .../management/commands/download-countries.py | 7 ++-- .../migrations/0016_country_translations.py | 18 ++++++++++ backend/server/worldtravel/models.py | 3 +- backend/server/worldtravel/serializers.py | 2 +- .../src/lib/components/CountryCard.svelte | 34 ++++++++++++++++-- frontend/src/lib/types.ts | 1 + 10 files changed, 106 insertions(+), 10 deletions(-) create mode 100644 Makefile create mode 100644 backend/server/worldtravel/migrations/0016_country_translations.py diff --git a/.gitignore b/.gitignore index fd9d0df39..314a123f4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ # Ignore everything in the .venv folder .venv/ .vscode/settings.json +.pnpm-store/ \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 11f6a2d01..e3c0ffe24 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -25,5 +25,16 @@ "backend/server/backend/lib/python3.12/site-packages/django/contrib/sites/locale", "backend/server/backend/lib/python3.12/site-packages/rest_framework/templates/rest_framework/docs/langs" ], - "i18n-ally.keystyle": "nested" + "i18n-ally.keystyle": "nested", + "sqltools.connections": [ + { + "previewLimit": 50, + "server": "localhost", + "port": 5432, + "driver": "PostgreSQL", + "name": "default", + "database": "adventurelog", + "username": "admin" + } + ] } diff --git a/Makefile b/Makefile new file mode 100644 index 000000000..48a5be79f --- /dev/null +++ b/Makefile @@ -0,0 +1,35 @@ +# Makefile for AdventureLog project +DOCKER_COMPOSE = docker compose -f docker-compose.yml +DOCKER_COMPOSE_TRAEFIK = docker compose -f docker-compose-traefik.yaml + +.PHONY: help +help: ## Display this help + @awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m\033[0m\n\nTargets:\n"} /^[a-zA-Z_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 }' $(MAKEFILE_LIST) + +all: dev ## Build all services (alias for dev) + +download-countries: ## Download countries + @cd backend/server && mkdir -p media + @cd backend/server && python manage.py download-countries --force + +dev-db: dev ## Start development database + # If adventurelog-development container not exists, create it + @docker ps -q -f name=adventurelog-development || docker run --name adventurelog-development -e POSTGRES_USER=admin -e POSTGRES_PASSWORD=admin -e POSTGRES_DB=adventurelog -p 5432:5432 -d postgis/postgis:15-3.3 + @echo "Please update the backend/.env file with the correct database credentials (uncomment the lines starting with # PGHOST, PGDATABASE, PGUSER, PGPASSWORD)" + +web: dev ## Start web service + @cd frontend && pnpm dev + +django: dev ## Start Django server + @cd backend/server && python manage.py migrate + @cd frontend && pnpm django + +dev: download-countries ## Setup Development Environment + @[ -f backend/server/.env ] || cp backend/server/.env.example backend/server/.env + @[ -f frontend/.env ] || cp frontend/.env.example frontend/.env + @cd frontend && pnpm install + @[ -d .venv ] || python -m venv .venv + @source .venv/bin/activate + @pip install -r backend/server/requirements.txt + + diff --git a/backend/entrypoint.sh b/backend/entrypoint.sh index 9ca926cc6..84de7b62e 100644 --- a/backend/entrypoint.sh +++ b/backend/entrypoint.sh @@ -51,8 +51,6 @@ else: EOF fi - -# Sync the countries and world travel regions # Sync the countries and world travel regions python manage.py download-countries if [ $? -eq 137 ]; then diff --git a/backend/server/worldtravel/management/commands/download-countries.py b/backend/server/worldtravel/management/commands/download-countries.py index f5c5702d9..056c56833 100644 --- a/backend/server/worldtravel/management/commands/download-countries.py +++ b/backend/server/worldtravel/management/commands/download-countries.py @@ -92,6 +92,7 @@ def handle(self, **options): country_capital = country['capital'] longitude = round(float(country['longitude']), 6) if country['longitude'] else None latitude = round(float(country['latitude']), 6) if country['latitude'] else None + translations = country['translations'] processed_country_codes.add(country_code) @@ -102,6 +103,7 @@ def handle(self, **options): country_obj.capital = country_capital country_obj.longitude = longitude country_obj.latitude = latitude + country_obj.translations = translations countries_to_update.append(country_obj) else: country_obj = Country( @@ -110,7 +112,8 @@ def handle(self, **options): subregion=country_subregion, capital=country_capital, longitude=longitude, - latitude=latitude + latitude=latitude, + translations=translations ) countries_to_create.append(country_obj) @@ -213,7 +216,7 @@ def handle(self, **options): batch = countries_to_update[i:i + batch_size] for i in tqdm(range(0, len(countries_to_update), batch_size), desc="Updating countries"): batch = countries_to_update[i:i + batch_size] - Country.objects.bulk_update(batch, ['name', 'subregion', 'capital', 'longitude', 'latitude']) + Country.objects.bulk_update(batch, ['name', 'subregion', 'capital', 'longitude', 'latitude', 'translations']) for i in tqdm(range(0, len(regions_to_update), batch_size), desc="Updating regions"): batch = regions_to_update[i:i + batch_size] diff --git a/backend/server/worldtravel/migrations/0016_country_translations.py b/backend/server/worldtravel/migrations/0016_country_translations.py new file mode 100644 index 000000000..cbd0a4533 --- /dev/null +++ b/backend/server/worldtravel/migrations/0016_country_translations.py @@ -0,0 +1,18 @@ +# Generated by Django 5.0.8 on 2025-01-13 17:50 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('worldtravel', '0015_city_insert_id_country_insert_id_region_insert_id'), + ] + + operations = [ + migrations.AddField( + model_name='country', + name='translations', + field=models.JSONField(default=dict, blank=True), + ), + ] diff --git a/backend/server/worldtravel/models.py b/backend/server/worldtravel/models.py index 6c7ebb883..a6ad58c27 100644 --- a/backend/server/worldtravel/models.py +++ b/backend/server/worldtravel/models.py @@ -18,7 +18,8 @@ class Country(models.Model): 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) insert_id = models.UUIDField(unique=False, blank=True, null=True) - + translations = models.JSONField(default=dict, blank=True) + class Meta: verbose_name = "Country" verbose_name_plural = "Countries" diff --git a/backend/server/worldtravel/serializers.py b/backend/server/worldtravel/serializers.py index 99c737982..7252a09f0 100644 --- a/backend/server/worldtravel/serializers.py +++ b/backend/server/worldtravel/serializers.py @@ -29,7 +29,7 @@ def get_num_visits(self, obj): class Meta: model = Country fields = '__all__' - read_only_fields = ['id', 'name', 'country_code', 'subregion', 'flag_url', 'num_regions', 'num_visits', 'longitude', 'latitude', 'capital'] + read_only_fields = ['id', 'name', 'country_code', 'subregion', 'flag_url', 'num_regions', 'num_visits', 'longitude', 'latitude', 'capital', 'translations'] class RegionSerializer(serializers.ModelSerializer): diff --git a/frontend/src/lib/components/CountryCard.svelte b/frontend/src/lib/components/CountryCard.svelte index 3894631f7..811a70d93 100644 --- a/frontend/src/lib/components/CountryCard.svelte +++ b/frontend/src/lib/components/CountryCard.svelte @@ -1,13 +1,41 @@