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