Browse Source

Make method recieve the level as int.

Fix linter.

Fix typo.
14.0
Jairo Llopis 9 years ago
committed by Andrii Skrypka
parent
commit
697ecc74cd
  1. 2
      base_location_nuts/README.rst
  2. 3
      base_location_nuts/__manifest__.py
  3. BIN
      base_location_nuts/images/new_fields.png
  4. 18
      base_location_nuts/models/res_partner.py
  5. 19
      base_location_nuts/wizard/nuts_import.py

2
base_location_nuts/README.rst

@ -19,7 +19,7 @@ Installation
============
We recommend to install another addon (one for each country) in order to relate
NUTS with states define by each localization addon, for example:
NUTS with states defined by each localization addon, for example:
* l10n_es_location_nuts : Spanish Provinces (NUTS level 4) related to Partner State
* l10n_de_location_nuts : German states (NUTS level 2) related to Partner State

3
base_location_nuts/__manifest__.py

@ -35,6 +35,9 @@
'wizard/nuts_import_view.xml',
'security/ir.model.access.csv',
],
'images': [
'images/new_fields.png',
],
'author': 'Antiun Ingeniería S.L., '
'Odoo Community Association (OCA)',
'website': 'http://www.antiun.com',

BIN
base_location_nuts/images/new_fields.png

After

Width: 582  |  Height: 215  |  Size: 17 KiB

18
base_location_nuts/models/res_partner.py

@ -23,16 +23,16 @@ class ResPartner(models.Model):
string="NUTS L4")
@api.multi
def _onchange_nuts(self, field):
country_id = self[field].country_id.id
state_id = self[field].state_id.id
def _onchange_nuts(self, level):
field = self["nuts%d_id" % level]
country_id = field.country_id.id
state_id = field.state_id.id
if country_id and self.country_id.id != country_id:
self.country_id = country_id
if state_id and self.state_id.id != state_id:
self.state_id = state_id
level = int(field[:5][-1])
if (level - 1) > 0:
parent_id = self[field].parent_id.id
parent_id = field.parent_id.id
if parent_id:
parent_field = 'nuts%d_id' % (level - 1)
if self[parent_field].id != parent_id:
@ -54,22 +54,22 @@ class ResPartner(models.Model):
@api.multi
@api.onchange('nuts4_id')
def _onchange_nuts4_id(self):
return self._onchange_nuts('nuts4_id')
return self._onchange_nuts(4)
@api.multi
@api.onchange('nuts3_id')
def _onchange_nuts3_id(self):
return self._onchange_nuts('nuts3_id')
return self._onchange_nuts(3)
@api.multi
@api.onchange('nuts2_id')
def _onchange_nuts2_id(self):
return self._onchange_nuts('nuts2_id')
return self._onchange_nuts(2)
@api.multi
@api.onchange('nuts1_id')
def _onchange_nuts1_id(self):
return self._onchange_nuts('nuts1_id')
return self._onchange_nuts(1)
@api.multi
@api.onchange('country_id')

19
base_location_nuts/wizard/nuts_import.py

@ -4,15 +4,13 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from openerp import models, api, _
from openerp.exceptions import Warning
from openerp.exceptions import Warning as UserError
import requests
import re
import logging
from lxml import etree
from collections import OrderedDict
from pprint import pformat
logger = logging.getLogger(__name__)
@ -93,7 +91,7 @@ class NutsImport(models.TransientModel):
else:
logger.debug("xpath = '%s', not found" % field_xpath)
if field_required and not value:
raise Warning(
raise UserError(
_('Value not found for mandatory field %s' % k))
item[k] = value
return item
@ -114,11 +112,11 @@ class NutsImport(models.TransientModel):
try:
res_request = requests.get(url)
except Exception, e:
raise Warning(
raise UserError(
_('Got an error when trying to download the file: %s.') %
str(e))
if res_request.status_code != requests.codes.ok:
raise Warning(
raise UserError(
_('Got an error %d when trying to download the file %s.')
% (res_request.status_code, url))
logger.info('Download successfully %d bytes' %
@ -127,7 +125,7 @@ class NutsImport(models.TransientModel):
pattern = re.compile(r'^.*<\?xml', re.DOTALL)
content_fixed = re.sub(pattern, '<?xml', res_request.content)
if not re.match(r'<\?xml', content_fixed):
raise Warning(_('Downloaded file is not a valid XML file'))
raise UserError(_('Downloaded file is not a valid XML file'))
return content_fixed
@api.model
@ -187,9 +185,10 @@ class NutsImport(models.TransientModel):
xmlcontent = self._download_nuts()
dom = etree.fromstring(xmlcontent)
for node in dom.iter('Item'):
logger.debug('Reading level=%s, id=%s' %
(node.get('idLevel', 'N/A'),
node.get('id', 'N/A')))
logger.debug(
'Reading level=%s, id=%s',
node.get('idLevel', 'N/A'),
node.get('id', 'N/A'))
nuts = self.create_or_update_nuts(node)
if nuts and nuts in nuts_to_delete:
nuts_to_delete -= nuts

Loading…
Cancel
Save