Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# porción del base_geolocalize/models/res_partner.py cambiada:
#try:
result1 = requests.get(url)
_logger.critical("respuesta raw %s" % result1)
result = json.loads(result1.text)
_logger.critical(("respuesta json: %s.") % result)
#except Exception, e:
# raise osv.except_osv(_('Network error.'),.....
#
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2013_Today OpenERP SA (<http://www.openerp.com>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
try:
import simplejson as json
except ImportError:
import json # noqa
#import urllib
import requests
from openerp.osv import osv, fields
from openerp import tools
from openerp.tools.translate import _
import logging
global _logger
_logger = logging.getLogger(__name__)
def geo_find(addr):
_logger.critical("pasa por geo_find")
url = 'https://maps.googleapis.com/maps/api/geocode/json?sensor=false&address='
_logger.critical("ya tiene la url principal")
url += addr.encode('utf8')
_logger.critical(("usa la siguiente url para solicitar el servicio: %s.") % url)
#try:
result1 = requests.get(url)
_logger.critical("respuesta raw %s" % result1)
result = json.loads(result1.text)
_logger.critical(("respuesta json: %s.") % result)
#except Exception, e:
# raise osv.except_osv(_('Network error.'),
# _('Cannot contact geolocation servers. Please make sure that your internet connection is up and running (%s).') % e)
if result['status'] != 'OK':
return None
try:
_logger.critical("pasa por geo = result")
geo = result['results'][0]['geometry']['location']
return float(geo['lat']), float(geo['lng'])
except (KeyError, ValueError):
return None
def geo_query_address(street=None, zip=None, city=None, state=None, country=None):
if country and ',' in country and (country.endswith(' of') or country.endswith(' of the')):
# put country qualifier in front, otherwise GMap gives wrong results,
# e.g. 'Congo, Democratic Republic of the' => 'Democratic Republic of the Congo'
country = '{1} {0}'.format(*country.split(',', 1))
return tools.ustr(', '.join(filter(None, [street,
("%s %s" % (zip or '', city or '')).strip(),
state,
country])))
class res_partner(osv.osv):
_inherit = "res.partner"
_columns = {
'partner_latitude': fields.float('Geo Latitude'),
'partner_longitude': fields.float('Geo Longitude'),
'date_localization': fields.date('Geo Localization Date'),
}
def geo_localize(self, cr, uid, ids, context=None):
# Don't pass context to browse()! We need country names in english below
for partner in self.browse(cr, uid, ids):
if not partner:
continue
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:
self.write(cr, uid, [partner.id], {
'partner_latitude': result[0],
'partner_longitude': result[1],
'date_localization': fields.date.context_today(self, cr, uid, context=context)
}, context=context)
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment