Skip to content

Commit

Permalink
[MIG] geoengine_base_geolocalize migration
Browse files Browse the repository at this point in the history
  • Loading branch information
benwillig committed Oct 2, 2017
1 parent 58a8df6 commit fa0bbc0
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 160 deletions.
67 changes: 26 additions & 41 deletions geoengine_base_geolocalize/__manifest__.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,27 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Author: Laurent Mignon
# Copyright (c) 2015 Acsone SA/NV (http://www.acsone.eu)
#
# 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/>.
#
##############################################################################
{'name': 'Geospatial support for base_geolocalize',
'version': '8.0.0.1.0',
'category': 'GeoBI',
'author': "ACSONE SA/NV, Odoo Community Association (OCA)",
'license': 'AGPL-3',
'website': 'http://www.acsone.eu',
'depends': [
'base',
'geoengine_partner',
'base_geolocalize',
],
'external_dependencies': {
'python': ['requests'],
},
'data': [
'views/res_partner_view.xml'
],
'installable': False,
'application': True,
'autoinstall': True,
'active': False,
}
# Copyright 2015-2017 ACSONE SA/NV (<http://acsone.eu>)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
{
'name': 'Geospatial support for base_geolocalize',
'version': '10.0.1.0.0',
'category': 'GeoBI',
'author': "ACSONE SA/NV, Odoo Community Association (OCA)",
'license': 'AGPL-3',
'website': 'https://github.com/OCA/geospatial',
'depends': [
'base',
'geoengine_partner',
'base_geolocalize',
'base_geolocalize_openstreetmap',
],
'external_dependencies': {
'python': [
'requests'
],
},
'data': [
'views/res_partner_view.xml'
],
'application': True,
'autoinstall': True,
}
20 changes: 0 additions & 20 deletions geoengine_base_geolocalize/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,21 +1 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Author: Laurent Mignon
# Copyright (c) 2015 Acsone SA/NV (http://www.acsone.eu)
#
# 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/>.
#
##############################################################################
from . import res_partner
95 changes: 32 additions & 63 deletions geoengine_base_geolocalize/models/res_partner.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,11 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Author: Laurent Mignon
# Copyright (c) 2015 Acsone SA/NV (http://www.acsone.eu)
#
# 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/>.
#
##############################################################################
# Copyright 2015 ACSONE SA/NV (<http://acsone.eu>)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).

import logging
from openerp import api, fields
from openerp import exceptions
from openerp.tools.translate import _
from openerp.addons.base_geoengine import geo_model
from openerp.addons.base_geoengine import fields as geo_fields

try:
import requests
except ImportError:
logger = logging.getLogger(__name__)
logger.warning('requests is not available in the sys path')
from odoo import api, fields
from odoo.addons.base_geoengine import geo_model
from odoo.addons.base_geoengine import fields as geo_fields

_logger = logging.getLogger(__name__)

Expand All @@ -39,56 +14,50 @@ class ResPartner(geo_model.GeoModel):
"""Add geo_point to partner using a function field"""
_inherit = "res.partner"

@api.one
@api.multi
def geocode_address(self):
"""Get the latitude and longitude by requesting "mapquestapi"
see http://open.mapquestapi.com/geocoding/
"""
url = 'http://nominatim.openstreetmap.org/search'
pay_load = {
'limit': 1,
'format': 'json',
'street': self.street or '',
'postalCode': self.zip or '',
'city': self.city or '',
'state': self.state_id and self.state_id.name or '',
'country': self.country_id and self.country_id.name or '',
'countryCodes': self.country_id and self.country_id.code or ''}

request_result = requests.get(url, params=pay_load)
try:
request_result.raise_for_status()
except Exception as e:
_logger.exception('Geocoding error')
raise exceptions.Warning(_(
'Geocoding error. \n %s') % e.message)
vals = request_result.json()
vals = vals and vals[0] or {}
self.ensure_one()
values = self.env[
'geoengine.geolocalize.openstreetmap'
].geocode_address(
self.street or '',
self.zip or '',
self.city or '',
self.state_id and self.state_id.name or '',
self.country_id and self.country_id.name or '',
self.country_id and self.country_id.code or '',
)
self.write({
'partner_latitude': vals.get('lat'),
'partner_longitude': vals.get('lon'),
'date_localization': fields.Date.today()})
'partner_latitude': values.get('lat'),
'partner_longitude': values.get('lon'),
'date_localization': fields.Date.today()
})

@api.one
@api.multi
def geo_localize(self):
self.ensure_one()
self.geocode_address()
return True

@api.one
@api.multi
@api.depends('partner_latitude', 'partner_longitude')
def _get_geo_point(self):
def _compute_geo_point(self):
"""
Set the `geo_point` of the partner depending of its `partner_latitude`
and its `partner_longitude`
**Notes**
If one of those parameters is not set then reset the partner's
geo_point and do not recompute it
"""
if not self.partner_latitude or not self.partner_longitude:
self.geo_point = False
else:
self.geo_point = geo_fields.GeoPoint.from_latlon(
self.env.cr, self.partner_latitude, self.partner_longitude)
for rec in self:
if not rec.partner_latitude or not rec.partner_longitude:
rec.geo_point = False
else:
rec.geo_point = geo_fields.GeoPoint.from_latlon(
rec.env.cr, rec.partner_latitude, rec.partner_longitude)

geo_point = geo_fields.GeoPoint(
readonly=True, store=True, compute='_get_geo_point')
readonly=True, store=True, compute='_compute_geo_point')
25 changes: 3 additions & 22 deletions geoengine_base_geolocalize/tests/test_geoengine_partner.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,11 @@
# -*- coding: utf-8 -*-
#
#
# Authors: Jonathan Nemry
# Copyright (c) 2015 Acsone SA/NV (http://www.acsone.eu)
#
# 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/>.
#
import openerp.tests.common as common
# Copyright 2015-2017 ACSONE SA/NV (<http://acsone.eu>)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
import odoo.tests.common as common


class TestGeoenginePartner(common.TransactionCase):

def setUp(self):
common.TransactionCase.setUp(self)

def test_get_geo_point(self):
partner_id = self.env.ref('base.user_root').partner_id
partner_id.partner_longitude = False
Expand Down
28 changes: 14 additions & 14 deletions geoengine_base_geolocalize/views/res_partner_view.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<record model="ir.ui.view" id="geoengine_partner.geo_partner_view_form">
<field name="name">geo_partner_form</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="base_geolocalize.view_crm_partner_geo_form"/>
<field name="arch" type="xml">
<field name="date_localization" position="after">
<field name="geo_point" widget="geo_edit_map"
groups="base_geoengine.group_geoengine_user,base_geoengine.group_geoengine_admin"/>
</field>
<odoo>

<record model="ir.ui.view" id="geoengine_partner.geo_partner_view_form">
<field name="name">geo_partner_form</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="base_geolocalize.view_crm_partner_geo_form"/>
<field name="arch" type="xml">
<field name="date_localization" position="after">
<field name="geo_point" widget="geo_edit_map"
groups="base_geoengine.group_geoengine_user,base_geoengine.group_geoengine_admin"/>
</field>
</record>
</data>
</openerp>
</field>
</record>

</odoo>
6 changes: 6 additions & 0 deletions setup/geoengine_base_geolocalize/.eggs/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
This directory contains eggs that were downloaded by setuptools to build, test, and run plug-ins.

This directory caches those eggs to prevent repeated downloads.

However, it is safe to delete this directory.

1 change: 1 addition & 0 deletions setup/geoengine_base_geolocalize/odoo/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__import__('pkg_resources').declare_namespace(__name__)
1 change: 1 addition & 0 deletions setup/geoengine_base_geolocalize/odoo/addons/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__import__('pkg_resources').declare_namespace(__name__)
6 changes: 6 additions & 0 deletions setup/geoengine_base_geolocalize/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)

0 comments on commit fa0bbc0

Please sign in to comment.