1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-08-04 20:55:19 +02:00

Refactor geocoding and integration handling: remove debug print, streamline reverse geocoding logic, and enhance integration response structure

This commit is contained in:
Sean Morley 2025-05-25 22:13:18 -04:00
parent c123231bab
commit e56335d30f
16 changed files with 95 additions and 46 deletions

View file

@ -166,7 +166,6 @@ def is_host_resolvable(hostname: str) -> bool:
def reverse_geocode(lat, lon, user):
if getattr(settings, 'GOOGLE_MAPS_API_KEY', None):
print("Using Google Maps API for reverse geocoding")
return reverse_geocode_google(lat, lon, user)
return reverse_geocode_osm(lat, lon, user)

View file

@ -46,7 +46,7 @@ class ReverseGeocodeViewSet(viewsets.ViewSet):
return Response({"error": "An internal error occurred while processing the request"}, status=500)
@action(detail=False, methods=['post'])
def mark_visited_region(self, request):
def mark_visited_region(self, _):
# searches through all of the users adventures, if the serialized data is_visited, is true, runs reverse geocode on the adventures and if a region is found, marks it as visited. Use the extractIsoCode function to get the region
new_region_count = 0
new_regions = {}
@ -60,14 +60,13 @@ class ReverseGeocodeViewSet(viewsets.ViewSet):
lon = adventure.longitude
if not lat or not lon:
continue
url = f"https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat={lat}&lon={lon}"
headers = {'User-Agent': 'AdventureLog Server'}
response = requests.get(url, headers=headers)
try:
data = response.json()
except requests.exceptions.JSONDecodeError:
return Response({"error": "Invalid response from geocoding service"}, status=400)
extracted_region = extractIsoCode(self.request.user,data)
# Use the existing reverse_geocode function which handles both Google and OSM
data = reverse_geocode(lat, lon, self.request.user)
if 'error' in data:
continue
extracted_region = extractIsoCode(self.request.user, data)
if 'error' not in extracted_region:
region = Region.objects.filter(id=extracted_region['region_id']).first()
visited_region = VisitedRegion.objects.filter(region=region, user_id=self.request.user).first()

View file

@ -1,13 +1,13 @@
import os
from rest_framework.response import Response
from rest_framework import viewsets, status
from .serializers import ImmichIntegrationSerializer
from .models import ImmichIntegration
from rest_framework.decorators import action
from rest_framework.permissions import IsAuthenticated
import requests
from rest_framework.pagination import PageNumberPagination
from django.conf import settings
class IntegrationView(viewsets.ViewSet):
permission_classes = [IsAuthenticated]
@ -16,15 +16,16 @@ class IntegrationView(viewsets.ViewSet):
RESTful GET method for listing all integrations.
"""
immich_integrations = ImmichIntegration.objects.filter(user=request.user)
google_map_integration = settings.GOOGLE_MAPS_API_KEY is not ''
return Response(
{
'immich': immich_integrations.exists()
'immich': immich_integrations.exists(),
'google_maps': google_map_integration
},
status=status.HTTP_200_OK
)
class StandardResultsSetPagination(PageNumberPagination):
page_size = 25
page_size_query_param = 'page_size'

View file

@ -61,6 +61,7 @@ class Command(BaseCommand):
return
elif os.path.getsize(countries_json_path) == 0:
self.stdout.write(self.style.ERROR('countries+regions+states.json is empty'))
return
elif Country.objects.count() == 0 or Region.objects.count() == 0 or City.objects.count() == 0:
self.stdout.write(self.style.WARNING('Some region data is missing. Re-importing all data.'))
else: