Make sure reverse geocoding uses correct key for cities (#938)

* Reorder town and county checks in geocoding.py

Fix detection if only town exists for a location but county is no city name

* Use address keys only if city is found
This commit is contained in:
Lars Lehmann
2025-12-28 22:54:54 +01:00
committed by GitHub
parent 6f923f0181
commit a92029f310

View File

@@ -157,12 +157,7 @@ def extractIsoCode(user, data):
for key in keys:
if key.find("ISO") != -1:
iso_code = data['address'][key]
if 'town' in keys:
town_city_or_county = data['address']['town']
if 'county' in keys:
town_city_or_county = data['address']['county']
if 'city' in keys:
town_city_or_county = data['address']['city']
if not iso_code:
return {"error": "No region found"}
@@ -174,9 +169,15 @@ def extractIsoCode(user, data):
country_code = iso_code[:2]
if region:
if town_city_or_county:
display_name = f"{town_city_or_county}, {region.name}, {country_code}"
city = City.objects.filter(name__contains=town_city_or_county, region=region).first()
if 'city' in keys:
city = City.objects.filter(name__contains=data['address']['city'], region=region).first()
if 'county' in keys and not city:
city = City.objects.filter(name__contains=data['address']['county'], region=region).first()
if 'town' in keys and not city:
city = City.objects.filter(name__contains=data['address']['town'], region=region).first()
if city:
display_name = f"{city.name}, {region.name}, {country_code}"
visited_city = VisitedCity.objects.filter(city=city, user=user).first()
if visited_region:
@@ -274,4 +275,4 @@ def _parse_google_address_components(components):
if country_code and state_code:
parsed["ISO3166-2-lvl1"] = f"{country_code}-{state_code}"
return parsed
return parsed