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 time
|
||||
import socket
|
||||
from worldtravel.models import Region, City, VisitedRegion, VisitedCity
|
||||
|
||||
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 {"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):
|
||||
"""
|
||||
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}"
|
||||
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:
|
||||
response = requests.get(url, headers=headers, timeout=(connect_timeout, read_timeout))
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
except requests.exceptions.JSONDecodeError:
|
||||
return {"error": "Invalid response from geocoding service"}
|
||||
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"}
|
||||
except Exception as e:
|
||||
return {"error": f"Unexpected geocoding error: {str(e)}"}
|
|
@ -167,7 +167,6 @@ class AdventureSerializer(CustomModelSerializer):
|
|||
if category_data:
|
||||
category = self.get_or_create_category(category_data)
|
||||
instance.category = category
|
||||
instance.save()
|
||||
|
||||
if has_visits:
|
||||
current_visits = instance.visits.all()
|
||||
|
|
|
@ -177,6 +177,8 @@
|
|||
let wikiImageError: string = '';
|
||||
let triggerMarkVisted: boolean = false;
|
||||
|
||||
let isLoading: boolean = false;
|
||||
|
||||
images = adventure.images || [];
|
||||
$: {
|
||||
if (!adventure.rating) {
|
||||
|
@ -414,6 +416,7 @@
|
|||
async function handleSubmit(event: Event) {
|
||||
event.preventDefault();
|
||||
triggerMarkVisted = true;
|
||||
isLoading = true;
|
||||
|
||||
// if category icon is empty, set it to the default icon
|
||||
if (adventure.category?.icon == '' || adventure.category?.icon == null) {
|
||||
|
@ -477,6 +480,7 @@
|
|||
}
|
||||
}
|
||||
imageSearch = adventure.name;
|
||||
isLoading = false;
|
||||
}
|
||||
</script>
|
||||
|
||||
|
@ -669,7 +673,14 @@
|
|||
</div>
|
||||
{/if}
|
||||
<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>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -55,7 +55,7 @@
|
|||
}
|
||||
|
||||
$: 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;
|
||||
}
|
||||
|
||||
|
|
|
@ -165,6 +165,8 @@
|
|||
{#if filteredCountries.length === 0}
|
||||
<p class="text-center font-bold text-2xl mt-12">{$t('worldtravel.no_countries_found')}</p>
|
||||
|
||||
<!-- If there are no allCountries then likely the download country command has not run on the server -->
|
||||
{#if allCountries.length === 0}
|
||||
<div class="text-center mt-4">
|
||||
<a
|
||||
class="link link-primary"
|
||||
|
@ -173,6 +175,7 @@
|
|||
>
|
||||
</div>
|
||||
{/if}
|
||||
{/if}
|
||||
|
||||
<svelte:head>
|
||||
<title>Countries | World Travel</title>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue