Browse Source

[REF] select_or_create_state

and tests
pull/22/head
Lorenzo Battistini 10 years ago
parent
commit
b118c762d4
  1. 14
      base_location_geonames_import/__openerp__.py
  2. 27
      base_location_geonames_import/test/import.yml
  3. 50
      base_location_geonames_import/wizard/geonames_import.py

14
base_location_geonames_import/__openerp__.py

@ -4,6 +4,8 @@
# Base Location Geonames Import module for OpenERP # Base Location Geonames Import module for OpenERP
# Copyright (C) 2014 Akretion (http://www.akretion.com) # Copyright (C) 2014 Akretion (http://www.akretion.com)
# @author Alexis de Lattre <alexis.delattre@akretion.com> # @author Alexis de Lattre <alexis.delattre@akretion.com>
# Copyright (C) 2014 Agile Business Group (http://www.agilebg.com)
# @author Lorenzo Battistini <lorenzo.battistini@agilebg.com>
# #
# This program is free software: you can redistribute it and/or modify # This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as # it under the terms of the GNU Affero General Public License as
@ -23,7 +25,7 @@
{ {
'name': 'Base Location Geonames Import', 'name': 'Base Location Geonames Import',
'version': '0.1',
'version': '0.2',
'category': 'Extra Tools', 'category': 'Extra Tools',
'license': 'AGPL-3', 'license': 'AGPL-3',
'summary': 'Import better zip entries from Geonames', 'summary': 'Import better zip entries from Geonames',
@ -34,6 +36,13 @@ Base Location Geonames Import
This module adds a wizard to import better zip entries from Geonames (http://download.geonames.org/export/zip/). When you start the wizard, it will ask you to select a country ; then, for the selected country, it will delete all the current better zip entries, download the latest version of the list of cities from geonames.org and create new better zip entries. This module adds a wizard to import better zip entries from Geonames (http://download.geonames.org/export/zip/). When you start the wizard, it will ask you to select a country ; then, for the selected country, it will delete all the current better zip entries, download the latest version of the list of cities from geonames.org and create new better zip entries.
Please contact Alexis de Lattre from Akretion <alexis.delattre@akretion.com> for any help or question about this module. Please contact Alexis de Lattre from Akretion <alexis.delattre@akretion.com> for any help or question about this module.
Contributors
------------
- Alexis de Lattre <alexis.delattre@akretion.com>
- Lorenzo Battistini <lorenzo.battistini@agilebg.com>
""", """,
'author': 'Akretion', 'author': 'Akretion',
'website': 'http://www.akretion.com', 'website': 'http://www.akretion.com',
@ -42,6 +51,9 @@ Please contact Alexis de Lattre from Akretion <alexis.delattre@akretion.com> for
'data': [ 'data': [
'wizard/geonames_import_view.xml', 'wizard/geonames_import_view.xml',
], ],
'test': [
'test/import.yml'
],
'installable': True, 'installable': True,
'active': False, 'active': False,
} }

27
base_location_geonames_import/test/import.yml

@ -0,0 +1,27 @@
-
I create the wizard
-
!record {model: better.zip.geonames.import, id: import_wizard_1, view: better_zip_geonames_import_form}:
country_id: base.it
-
I run the import
-
!python {model: better.zip.geonames.import}: |
self.run_import(cr, uid, [ref('import_wizard_1')], context=context)
-
I check the data
-
!python {model: res.better.zip}: |
state_obj = self.pool['res.country.state']
state_ids = state_obj.search(cr, uid, [
('code', '=', 'LG'),
('country_id', '=', ref('base.it')),
], context=context)
assert len(state_ids) == 1, "There must be 1 LG"
zip_ids = self.search(cr, uid, [
('name', '=', '16017'),
('city', '=', 'Isola Del Cantone'),
('state_id', '=', state_ids[0]),
('country_id', '=', ref('base.it'))
], context=context)
assert len(zip_ids) == 1, "There must be 1 'Isola Del Cantone'"

50
base_location_geonames_import/wizard/geonames_import.py

@ -4,6 +4,8 @@
# Base Location Geonames Import module for OpenERP # Base Location Geonames Import module for OpenERP
# Copyright (C) 2014 Akretion (http://www.akretion.com) # Copyright (C) 2014 Akretion (http://www.akretion.com)
# @author Alexis de Lattre <alexis.delattre@akretion.com> # @author Alexis de Lattre <alexis.delattre@akretion.com>
# Copyright (C) 2014 Agile Business Group (http://www.agilebg.com)
# @author Lorenzo Battistini <lorenzo.battistini@agilebg.com>
# #
# This program is free software: you can redistribute it and/or modify # This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as # it under the terms of the GNU Affero General Public License as
@ -41,22 +43,19 @@ class better_zip_geonames_import(models.TransientModel):
country_id = fields.Many2one('res.country', 'Country', required=True) country_id = fields.Many2one('res.country', 'Country', required=True)
@api.model @api.model
def _prepare_better_zip(self, row, country_id, states):
'''This function is designed to be inherited'''
state_id = False
if states and row[4] and row[4] in states:
state_id = states[row[4].upper()]
def _prepare_better_zip(self, row, country_id):
state = self.select_or_create_state(row, country_id)
vals = { vals = {
'name': row[1], 'name': row[1],
'city': row[2], 'city': row[2],
'state_id': state_id,
'state_id': state.id,
'country_id': country_id, 'country_id': country_id,
} }
return vals return vals
@api.model @api.model
def create_better_zip( def create_better_zip(
self, row, country_id, country_code, states):
self, row, country_id, country_code):
bzip_id = False bzip_id = False
if row[0] != country_code: if row[0] != country_code:
raise Warning( raise Warning(
@ -66,11 +65,32 @@ class better_zip_geonames_import(models.TransientModel):
% (row[0], country_code)) % (row[0], country_code))
logger.debug('ZIP = %s - City = %s' % (row[1], row[2])) logger.debug('ZIP = %s - City = %s' % (row[1], row[2]))
if row[1] and row[2]: if row[1] and row[2]:
vals = self._prepare_better_zip(row, country_id, states)
vals = self._prepare_better_zip(row, country_id)
if vals: if vals:
bzip_id = self.env['res.better.zip'].create(vals) bzip_id = self.env['res.better.zip'].create(vals)
return bzip_id return bzip_id
@api.model
def select_or_create_state(
self, row, country_id, code_row_index=4, name_row_index=3
):
states = self.env['res.country.state'].search([
('country_id', '=', country_id),
('code', '=', row[code_row_index]),
])
if len(states) > 1:
raise Warning(
_("Too many states with code %s for counrty %s")
% (row[code_row_index], country_id))
if len(states) == 1:
return states[0]
else:
return self.env['res.country.state'].create({
'name': row[name_row_index],
'code': row[code_row_index],
'country_id': country_id
})
@api.one @api.one
def run_import(self): def run_import(self):
bzip_obj = self.env['res.better.zip'] bzip_obj = self.env['res.better.zip']
@ -90,15 +110,6 @@ class better_zip_geonames_import(models.TransientModel):
logger.info( logger.info(
'%d better zip entries deleted for country %s' '%d better zip entries deleted for country %s'
% (len(bzip_ids_to_delete), self.country_id.name)) % (len(bzip_ids_to_delete), self.country_id.name))
state_ids = self.env['res.country.state'].search(
[('country_id', '=', country_id)])
states = {}
# key = code of the state ; value = ID of the state in OpenERP
if state_ids:
states_r = self.env['res.country.state'].read(
state_ids, ['code', 'country_id'])
for state in states_r:
states[state['code'].upper()] = state['id']
f_geonames = zipfile.ZipFile(StringIO.StringIO(res_request.content)) f_geonames = zipfile.ZipFile(StringIO.StringIO(res_request.content))
tempdir = tempfile.mkdtemp(prefix='openerp') tempdir = tempfile.mkdtemp(prefix='openerp')
f_geonames.extract('%s.txt' % country_code, tempdir) f_geonames.extract('%s.txt' % country_code, tempdir)
@ -106,11 +117,10 @@ class better_zip_geonames_import(models.TransientModel):
data_file = open(os.path.join(tempdir, '%s.txt' % country_code), 'r') data_file = open(os.path.join(tempdir, '%s.txt' % country_code), 'r')
data_file.seek(0) data_file.seek(0)
logger.info( logger.info(
'Starting to create the better zip entries %s state information'
% (states and 'with' or 'without'))
'Starting to create the better zip entries')
for row in unicodecsv.reader( for row in unicodecsv.reader(
data_file, encoding='utf-8', delimiter=' '): data_file, encoding='utf-8', delimiter=' '):
self.create_better_zip(row, country_id, country_code, states)
self.create_better_zip(row, country_id, country_code)
data_file.close() data_file.close()
logger.info( logger.info(
'The wizard to create better zip entries from geonames ' 'The wizard to create better zip entries from geonames '

Loading…
Cancel
Save