Skip to content
Extraits de code Groupes Projets
Valider 265c9050 rédigé par Benjamin's avatar Benjamin
Parcourir les fichiers

initialisation

parent
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
.*
*.pyc
!.gitignore
Ce diff est replié.
.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg
:target: http://www.gnu.org/licenses/agpl
:alt: License: AGPL-3
======================
Nominatim Geolocalize
======================
Get partner coordinate using Nominatim API
Alternative to Google API
Credits
=======
Contributors ------------
* Benjamin Rivier <benjamin@le-filament.com>
Maintainer ----------
.. image:: https://le-filament.com/images/logo-lefilament.png
:alt: Le Filament
:target: https://le-filament.com
This module is maintained by Le Filament
\ No newline at end of file
# -*- coding: utf-8 -*-
# © 2017 Le Filament (<http://www.le-filament.com>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
import models
# -*- coding: utf-8 -*-
# © 2017 Le Filament (<http://www.le-filament.com>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
{
'name': 'Nominatim Geolocalize',
'version': '1.0',
'category': 'Partner',
'description': """
Nominatim Geolocalize
========================
Get partner coordinate using Nominatim API
""",
'depends': ['base'],
'data': [
'views/res_partner.xml',
],
'installable': True,
}
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import res_partner
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import json
import urllib2
from odoo import api, fields, models, tools, _
from odoo.exceptions import UserError
def geo_find(addr):
if not addr:
return None
url = 'https://nominatim.openstreetmap.org/search.php/'
url += urllib2.quote(addr.encode('utf8'))
url += '?format=json'
try:
result = json.load(urllib2.urlopen(url))
except Exception as e:
raise UserError(_('Cannot contact geolocation servers. Please make sure that your Internet connection is up and running (%s).') % e)
try:
geo = result[0]
return [float(geo['lat']), float(geo['lon'])]
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 ResPartner(models.Model):
_inherit = "res.partner"
partner_latitude = fields.Float(string='Geo Latitude', digits=(16, 5))
partner_longitude = fields.Float(string='Geo Longitude', digits=(16, 5))
date_localization = fields.Date(string='Geolocation Date')
@api.multi
def geo_localize(self):
# We need country names in English below
for partner in self.with_context(lang='en_US'):
result = geo_find(geo_query_address(street=partner.street,
zip=partner.zip,
city=partner.city,
country=partner.country_id.name))
if result is None:
result = geo_find(geo_query_address(
city=partner.city,
country=partner.country_id.name
))
if result:
partner.write({
'partner_latitude': result[0],
'partner_longitude': result[1],
'date_localization': fields.Date.context_today(partner)
})
return True
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="res_partner_nominatim_form" model="ir.ui.view">
<field name="name">res.partner.geolocation.inherit</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_partner_form"/>
<field name="arch" type="xml">
<xpath expr="//notebook[last()]" position="inside">
<page string="Geolocation" name="geo_location">
<group>
<group>
<button string="Geolocate" name="geo_localize" icon="fa-check" type="object"/>
</group>
<group>
<field name="date_localization" />
<field name="partner_latitude" />
<field name="partner_longitude" />
</group>
</group>
</page>
</xpath>
</field>
</record>
</odoo>
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter