Skip to content
Snippets Groups Projects
Commit 50d21df1 authored by Jeremy Kersten's avatar Jeremy Kersten
Browse files

[FIX] base_geolocalize: allow to force country in request

Before this commit, using google geoloc, we only provide a string as address
withtout more information.

Now by defaut, we force the country as components:
https://developers.google.com/maps/documentation/geocoding/intro#geocoding

Related to task-2030886

**setup:**
```python
url = "https://maps.googleapis.com/maps/api/geocode/json

"
```

**before:**
```python
params = {'address': 'Georgia', 'key': apikey}
requests.get(url, params).json()
```

>       ==> Return coordinate of "Georgia, United States"
>           What is completely wrong
>

**after:**
```python
params = {'address': 'Georgia', 'components':'country:Georgia', 'key': apikey}
requests.get(url, params).json()
```

>       ==> Return Nothing
>          What is strange but less wrong than US
>

```python
params = {'address': 'Belgium', 'components':'country:Belgium', 'key': apikey}
requests.get(url, params).json()
```

>       ==> Return coordinate of Belgium

closes odoo/odoo#34582

Signed-off-by: default avatarChristophe Simonis <chs@odoo.com>
parent 3431751c
No related branches found
No related tags found
No related merge requests found
......@@ -55,7 +55,7 @@ class GeoCoder(models.AbstractModel):
return self._geo_query_address_default(street=street, zip=zip, city=city, state=state, country=country)
@api.model
def geo_find(self, addr):
def geo_find(self, addr, **kw):
"""Use a location provider API to convert an address string into a latitude, longitude tuple.
Here we use Openstreetmap Nominatim by default.
:param addr: Address string passed to API
......@@ -64,7 +64,7 @@ class GeoCoder(models.AbstractModel):
provider = self._get_provider().tech_name
try:
service = getattr(self, '_call_' + provider)
result = service(addr)
result = service(addr, **kw)
except AttributeError:
raise UserError(_(
'Provider %s is not implemented for geolocation service.'
......@@ -77,7 +77,7 @@ class GeoCoder(models.AbstractModel):
return result
@api.model
def _call_openstreetmap(self, addr):
def _call_openstreetmap(self, addr, **kw):
"""
Use Openstreemap Nominatim service to retrieve location
:return: (latitude, longitude) or None if not found
......@@ -95,7 +95,7 @@ class GeoCoder(models.AbstractModel):
return float(geo['lat']), float(geo['lon'])
@api.model
def _call_googlemap(self, addr):
def _call_googlemap(self, addr, **kw):
""" Use google maps API. It won't work without a valid API key.
:return: (latitude, longitude) or None if not found
"""
......@@ -106,8 +106,11 @@ class GeoCoder(models.AbstractModel):
"Visit https://developers.google.com/maps/documentation/geocoding/get-api-key for more information."
))
url = "https://maps.googleapis.com/maps/api/geocode/json"
params = {'sensor': 'false', 'address': addr, 'key': apikey}
if kw.get('force_country'):
params['components'] = 'country:%s' % kw['force_country']
try:
result = requests.get(url, params={'sensor': 'false', 'address': addr, 'key': apikey}).json()
result = requests.get(url, params).json()
except Exception as e:
self._raise_query_error(e)
......@@ -151,4 +154,3 @@ class GeoCoder(models.AbstractModel):
def _raise_query_error(self, error):
raise UserError(_('Error with geolocation server:') + ' %s' % error)
......@@ -12,10 +12,10 @@ class ResPartner(models.Model):
def _geo_localize(self, street='', zip='', city='', state='', country=''):
geo_obj = self.env['base.geocoder']
search = geo_obj.geo_query_address(street=street, zip=zip, city=city, state=state, country=country)
result = geo_obj.geo_find(search)
result = geo_obj.geo_find(search, force_country=country)
if result is None:
search = geo_obj.geo_query_address(city=city, state=state, country=country)
result = geo_obj.geo_find(search)
result = geo_obj.geo_find(search, force_country=country)
return result
@api.multi
......
......@@ -29,7 +29,7 @@ class TestPartnerAssign(TransactionCase):
'partner_id': self.customer_uk.id
})
def geo_find(addr):
def geo_find(addr, **kw):
return {
'Wavre, Belgium': (50.7158956, 4.6128075),
'Cannon Hill Park, B46 3AG Birmingham, United Kingdom': (52.45216, -1.898578),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment