Files
odoo_source/addons/base_geolocalize/models/res_partner.py
T
Yannick Tivisse dc3ad1381f [FIX] base_geolocalize: Partial revert of #84266
Purpose
=======

This @depends could be 'dangerous' because it would trigger a
lot of calls to the GeoIP API.

It would be more reasonable to make it lazy instead.

So we just reset the geolocation to 0 when the address is modified
without providing the new values, and the user can manually trigger
the geolocation again when needed.

For FSM, it will be updated when needed as the computation is
triggered when the geolocation information is empty.

Part-of: odoo/odoo#84890
2022-02-18 14:29:20 +00:00

62 lines
2.7 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, fields, models, _
from odoo.tools import config
class ResPartner(models.Model):
_inherit = "res.partner"
date_localization = fields.Date(string='Geolocation Date')
def write(self, vals):
# Reset latitude/longitude in case we modify the address without
# updating the related geolocation fields
if any(field in vals for field in ['street', 'zip', 'city', 'state_id', 'country_id']) \
and not all('partner_%s' % field in vals for field in ['latitude', 'longitude']):
vals.update({
'partner_latitude': 0.0,
'partner_longitude': 0.0,
})
return super().write(vals)
@api.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, 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, force_country=country)
return result
def geo_localize(self):
# We need country names in English below
if not self._context.get('force_geo_localize') \
and (self._context.get('import_file') \
or any(config[key] for key in ['test_enable', 'test_file', 'init', 'update'])):
return False
partners_not_geo_localized = self.env['res.partner']
for partner in self.with_context(lang='en_US'):
result = self._geo_localize(partner.street,
partner.zip,
partner.city,
partner.state_id.name,
partner.country_id.name)
if result:
partner.write({
'partner_latitude': result[0],
'partner_longitude': result[1],
'date_localization': fields.Date.context_today(partner)
})
else:
partners_not_geo_localized |= partner
if partners_not_geo_localized:
self.env['bus.bus']._sendone(self.env.user.partner_id, 'simple_notification', {
'title': _("Warning"),
'message': _('No address found for %(partner_names)s.', partner_names=', '.join(partners_not_geo_localized.mapped('name')))
})
return True