Skip to content

Commit

Permalink
[FIX] base_geolocalize, website_crm_partner_assign: API keys are now …
Browse files Browse the repository at this point in the history
…required

Google Important Updated: API keys are now required
We began enforcing the use of API keys, effective June 11th 2018.
Keyless usage will result in a degraded experience, or an error.

https://developers.google.com/maps/billing/important-updates
  • Loading branch information
JKE-be committed Sep 12, 2018
1 parent 72d7dd8 commit e6ca846
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 28 deletions.
44 changes: 30 additions & 14 deletions addons/base_geolocalize/models/res_partner.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import json
import logging
import urllib2

from odoo import api, fields, models, tools, _
from odoo.exceptions import UserError

_logger = logging.getLogger(__name__)

def geo_find(addr):

def geo_find(addr, apikey=False):
if not addr:
return None
url = 'https://maps.googleapis.com/maps/api/geocode/json?sensor=false&address='

if not apikey:
raise UserError(_('''API key for GeoCoding (Places) required.\n
Save this key in System Parameters with key: google.api_key_geocode, value: <your api key>
Visit https://developers.google.com/maps/documentation/geocoding/get-api-key for more information.
'''))

url = "https://maps.googleapis.com/maps/api/geocode/json?key=%s&sensor=false&address=" % apikey
url += urllib2.quote(addr.encode('utf8'))

try:
Expand All @@ -19,6 +29,8 @@ def geo_find(addr):
raise UserError(_('Cannot contact geolocation servers. Please make sure that your Internet connection is up and running (%s).') % e)

if result['status'] != 'OK':
if result.get('error_message'):
_logger.error(result['error_message'])
return None

try:
Expand Down Expand Up @@ -46,22 +58,26 @@ class ResPartner(models.Model):
partner_longitude = fields.Float(string='Geo Longitude', digits=(16, 5))
date_localization = fields.Date(string='Geolocation Date')

@classmethod
def _geo_localize(cls, apikey, street='', zip='', city='', state='', country=''):
search = geo_query_address(street=street, zip=zip, city=city, state=state, country=country)
result = geo_find(search, apikey)
if result is None:
search = geo_query_address(city=city, state=state, country=country)
result = geo_find(search, apikey)
return result

@api.multi
def geo_localize(self):
# We need country names in English below
apikey = self.env['ir.config_parameter'].sudo().get_param('google.api_key_geocode')
for partner in self.with_context(lang='en_US'):
result = geo_find(geo_query_address(street=partner.street,
zip=partner.zip,
city=partner.city,
state=partner.state_id.name,
country=partner.country_id.name))
if result is None:
result = geo_find(geo_query_address(
city=partner.city,
state=partner.state_id.name,
country=partner.country_id.name
))

result = partner._geo_localize(apikey,
partner.street,
partner.zip,
partner.city,
partner.state_id.name,
partner.country_id.name)
if result:
partner.write({
'partner_latitude': result[0],
Expand Down
17 changes: 4 additions & 13 deletions addons/website_crm_partner_assign/models/crm_lead.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,10 @@ def assign_geo_localize(self, latitude=False, longitude=False):
if lead.partner_latitude and lead.partner_longitude:
continue
if lead.country_id:
result = geo_find(geo_query_address(street=lead.street,
zip=lead.zip,
city=lead.city,
state=lead.state_id.name,
country=lead.country_id.name))

if result is None:
result = geo_find(geo_query_address(
city=lead.city,
state=lead.state_id.name,
country=lead.country_id.name
))

apikey = self.env['ir.config_parameter'].sudo().get_param('google.api_key_geocode')
result = self.env['res.partner']._geo_localize(apikey,
lead.street, lead.zip, lead.city,
lead.state_id.name, lead.country_id.name)
if result:
lead.write({
'partner_latitude': result[0],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class TestPartnerAssign(TransactionCase):
def setUp(self):
super(TestPartnerAssign, self).setUp()

def geo_find(addr):
def geo_find(addr, apikey):
return {
'Wavre, Belgium': (50.7158956, 4.6128075),
'Cannon Hill Park, B46 3AG Birmingham, United Kingdom': (52.45216, -1.898578),
Expand Down

0 comments on commit e6ca846

Please sign in to comment.