Merge branch 'mig/12.0/l10n_us_partner_zipcode' into '12.0'

mig/12.0/l10n_us_partner_zipcode into 12.0

See merge request hibou-io/hibou-odoo/suite!113
This commit is contained in:
Kaylie Kipe
2020-10-09 13:30:09 +00:00
3 changed files with 77 additions and 0 deletions

View File

@@ -0,0 +1 @@
from . import res_partner

View File

@@ -0,0 +1,37 @@
{
'name': 'US ZIP Code to City/State',
'summary': 'Determines the City and State from a provided ZIP code.',
'version': '12.0.1.0.0',
'author': "Hibou Corp.",
'category': 'Localization',
'license': 'AGPL-3',
'complexity': 'easy',
'images': [],
'website': "https://hibou.io",
'description': """
US ZIP Code to City/State
=========================
Determines the City and State from a provided ZIP code. Requires the `uszipcode` python package.
Does not require `base_geolocalize`, but will fill the related fields if possible.
Contributors
------------
* Jared Kipe <jared@hibou.io>
""",
'depends': [
'base',
],
'external_dependencies': {
'python': ['uszipcode']
},
'demo': [],
'data': [
],
'auto_install': False,
'installable': True,
}

View File

@@ -0,0 +1,39 @@
import logging
_logger = logging.getLogger(__name__)
try:
from uszipcode import SearchEngine
except ImportError:
_logger.warn('module "uszipcode" cannot be loaded, you will be unable to detect Cities and States by ZIP')
SearchEngine = None
from odoo import api, fields, models
class Partner(models.Model):
_inherit = 'res.partner'
@api.onchange('zip')
def _zip_to_city_state(self):
if SearchEngine and self.zip and not self.city:
country_us = self.env['res.country'].search([('code', '=', 'US')], limit=1)
state_obj = self.env['res.country.state']
if not self.country_id or self.country_id.id == country_us.id:
with SearchEngine() as search:
zipcode = search.by_zipcode(self.zip)
if zipcode:
if not self.country_id:
self.country_id = country_us
self.city = zipcode.city
self.state_id = state_obj.search([
('code', '=', zipcode.state),
('country_id', '=', country_us.id),
], limit=1)
if hasattr(self, 'partner_latitude') and not self.partner_latitude:
self.partner_latitude = zipcode.lat
self.partner_longitude = zipcode.lng
self.date_localization = fields.Date.context_today(self)
return {}