mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-07-24 15:29:36 +02:00
Enhance geocoding functionality with host resolution and improved error handling; update AdventureModal for loading state management; refine LocationDropdown toast display logic; adjust world travel page for better documentation link visibility.
This commit is contained in:
parent
0d800e8986
commit
2ccb8f5e0b
5 changed files with 54 additions and 17 deletions
|
@ -1,4 +1,6 @@
|
||||||
import requests
|
import requests
|
||||||
|
import time
|
||||||
|
import socket
|
||||||
from worldtravel.models import Region, City, VisitedRegion, VisitedCity
|
from worldtravel.models import Region, City, VisitedRegion, VisitedCity
|
||||||
|
|
||||||
def extractIsoCode(user, data):
|
def extractIsoCode(user, data):
|
||||||
|
@ -56,16 +58,38 @@ def extractIsoCode(user, data):
|
||||||
return {"region_id": iso_code, "region": region.name, "country": region.country.name, "country_id": region.country.country_code, "region_visited": region_visited, "display_name": display_name, "city": city.name if city else None, "city_id": city.id if city else None, "city_visited": city_visited, 'location_name': location_name}
|
return {"region_id": iso_code, "region": region.name, "country": region.country.name, "country_id": region.country.country_code, "region_visited": region_visited, "display_name": display_name, "city": city.name if city else None, "city_id": city.id if city else None, "city_visited": city_visited, 'location_name': location_name}
|
||||||
return {"error": "No region found"}
|
return {"error": "No region found"}
|
||||||
|
|
||||||
|
def is_host_resolvable(hostname: str) -> bool:
|
||||||
|
try:
|
||||||
|
socket.gethostbyname(hostname)
|
||||||
|
return True
|
||||||
|
except socket.error:
|
||||||
|
return False # silently fail
|
||||||
|
|
||||||
def reverse_geocode(lat, lon, user):
|
def reverse_geocode(lat, lon, user):
|
||||||
"""
|
|
||||||
Reverse geocode the given latitude and longitude using Nominatim API.
|
|
||||||
Returns a dictionary containing the region name, country name, and ISO code if found.
|
|
||||||
"""
|
|
||||||
url = f"https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat={lat}&lon={lon}"
|
url = f"https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat={lat}&lon={lon}"
|
||||||
headers = {'User-Agent': 'AdventureLog Server'}
|
headers = {'User-Agent': 'AdventureLog Server'}
|
||||||
response = requests.get(url, headers=headers)
|
connect_timeout = 1 # instead of 0.3
|
||||||
|
read_timeout = 5 # instead of 2
|
||||||
|
|
||||||
|
|
||||||
|
if not is_host_resolvable("nominatim.openstreetmap.org"):
|
||||||
|
return {"error": "DNS resolution failed"}
|
||||||
|
|
||||||
|
start = time.time()
|
||||||
try:
|
try:
|
||||||
|
response = requests.get(url, headers=headers, timeout=(connect_timeout, read_timeout))
|
||||||
|
response.raise_for_status()
|
||||||
data = response.json()
|
data = response.json()
|
||||||
except requests.exceptions.JSONDecodeError:
|
elapsed = time.time() - start
|
||||||
|
return extractIsoCode(user, data)
|
||||||
|
|
||||||
|
except requests.exceptions.ConnectionError as e:
|
||||||
|
return {"error": "Could not connect to geocoding service"}
|
||||||
|
except requests.exceptions.Timeout as e:
|
||||||
|
return {"error": "Geocoding service timed out"}
|
||||||
|
except requests.exceptions.HTTPError as e:
|
||||||
|
return {"error": f"HTTP error from geocoding service: {e}"}
|
||||||
|
except requests.exceptions.JSONDecodeError as e:
|
||||||
return {"error": "Invalid response from geocoding service"}
|
return {"error": "Invalid response from geocoding service"}
|
||||||
return extractIsoCode(user, data)
|
except Exception as e:
|
||||||
|
return {"error": f"Unexpected geocoding error: {str(e)}"}
|
|
@ -167,7 +167,6 @@ class AdventureSerializer(CustomModelSerializer):
|
||||||
if category_data:
|
if category_data:
|
||||||
category = self.get_or_create_category(category_data)
|
category = self.get_or_create_category(category_data)
|
||||||
instance.category = category
|
instance.category = category
|
||||||
instance.save()
|
|
||||||
|
|
||||||
if has_visits:
|
if has_visits:
|
||||||
current_visits = instance.visits.all()
|
current_visits = instance.visits.all()
|
||||||
|
|
|
@ -177,6 +177,8 @@
|
||||||
let wikiImageError: string = '';
|
let wikiImageError: string = '';
|
||||||
let triggerMarkVisted: boolean = false;
|
let triggerMarkVisted: boolean = false;
|
||||||
|
|
||||||
|
let isLoading: boolean = false;
|
||||||
|
|
||||||
images = adventure.images || [];
|
images = adventure.images || [];
|
||||||
$: {
|
$: {
|
||||||
if (!adventure.rating) {
|
if (!adventure.rating) {
|
||||||
|
@ -414,6 +416,7 @@
|
||||||
async function handleSubmit(event: Event) {
|
async function handleSubmit(event: Event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
triggerMarkVisted = true;
|
triggerMarkVisted = true;
|
||||||
|
isLoading = true;
|
||||||
|
|
||||||
// if category icon is empty, set it to the default icon
|
// if category icon is empty, set it to the default icon
|
||||||
if (adventure.category?.icon == '' || adventure.category?.icon == null) {
|
if (adventure.category?.icon == '' || adventure.category?.icon == null) {
|
||||||
|
@ -477,6 +480,7 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
imageSearch = adventure.name;
|
imageSearch = adventure.name;
|
||||||
|
isLoading = false;
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
@ -669,7 +673,14 @@
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
<div class="flex flex-row gap-2">
|
<div class="flex flex-row gap-2">
|
||||||
<button type="submit" class="btn btn-primary">{$t('adventures.save_next')}</button>
|
{#if !isLoading}
|
||||||
|
<button type="submit" class="btn btn-primary">{$t('adventures.save_next')}</button
|
||||||
|
>
|
||||||
|
{:else}
|
||||||
|
<button type="button" class="btn btn-primary"
|
||||||
|
><span class="loading loading-spinner loading-md"></span></button
|
||||||
|
>
|
||||||
|
{/if}
|
||||||
<button type="button" class="btn" on:click={close}>{$t('about.close')}</button>
|
<button type="button" class="btn" on:click={close}>{$t('about.close')}</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -55,7 +55,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
$: if (triggerMarkVisted && willBeMarkedVisited) {
|
$: if (triggerMarkVisted && willBeMarkedVisited) {
|
||||||
displaySuccessToast(); // since the server will trigger the geocode automatically, we just need to show the toast and let the server handle the rest. It's kinda a placebo effect...
|
displaySuccessToast(); // since the server will trigger the geocode automatically, we just need to show the toast and let the server handle the rest. It's kinda a placebo effect
|
||||||
triggerMarkVisted = false;
|
triggerMarkVisted = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -165,13 +165,16 @@
|
||||||
{#if filteredCountries.length === 0}
|
{#if filteredCountries.length === 0}
|
||||||
<p class="text-center font-bold text-2xl mt-12">{$t('worldtravel.no_countries_found')}</p>
|
<p class="text-center font-bold text-2xl mt-12">{$t('worldtravel.no_countries_found')}</p>
|
||||||
|
|
||||||
<div class="text-center mt-4">
|
<!-- If there are no allCountries then likely the download country command has not run on the server -->
|
||||||
<a
|
{#if allCountries.length === 0}
|
||||||
class="link link-primary"
|
<div class="text-center mt-4">
|
||||||
href="https://adventurelog.app/docs/configuration/updating.html#updating-the-region-data"
|
<a
|
||||||
target="_blank">{$t('settings.documentation_link')}</a
|
class="link link-primary"
|
||||||
>
|
href="https://adventurelog.app/docs/configuration/updating.html#updating-the-region-data"
|
||||||
</div>
|
target="_blank">{$t('settings.documentation_link')}</a
|
||||||
|
>
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
<svelte:head>
|
<svelte:head>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue