Browse Source

Add module base_location_geonames_import

pull/22/head
Alexis de Lattre 10 years ago
committed by Lorenzo Battistini
parent
commit
7887672941
  1. 2
      base_location_geonames_import/__openerp__.py
  2. 43
      base_location_geonames_import/wizard/geonames_import.py
  3. 4
      base_location_geonames_import/wizard/geonames_import_view.xml

2
base_location_geonames_import/__openerp__.py

@ -31,7 +31,7 @@
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/).
Please contact Alexis de Lattre from Akretion <alexis.delattre@akretion.com> for any help or question about this module.
""",

43
base_location_geonames_import/wizard/geonames_import.py

@ -42,36 +42,27 @@ class better_zip_geonames_import(orm.TransientModel):
}
def _prepare_better_zip(
self, cr, uid, row, country_id, states, context=None):
self, cr, uid, row, country_id, context=None):
'''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()]
vals = {
return {
'name': row[1],
'city': row[2],
'state_id': state_id,
'country_id': country_id,
}
return vals
def create_better_zip(
self, cr, uid, row, country_id, country_code, states,
context=None):
self, cr, uid, row, country_id, country_code, context=None):
bzip_id = False
if row[0] != country_code:
raise orm.except_orm(
_('Error:'),
_("The country code inside the file (%s) doesn't "
"correspond to the selected country (%s).")
% (row[0], country_code))
_("The content file doesn't correspond to the country"))
logger.debug('ZIP = %s - City = %s' % (row[1], row[2]))
if row[1] and row[2]:
if row[1] and row[2] and 'CEDEX' not in row[1]:
vals = self._prepare_better_zip(
cr, uid, row, country_id, states, context=context)
if vals:
bzip_id = self.pool['res.better.zip'].create(
cr, uid, vals, context=context)
cr, uid, row, country_id, context=context)
bzip_id = self.pool['res.better.zip'].create(
cr, uid, vals, context=context)
return bzip_id
def run_import(self, cr, uid, ids, context=None):
@ -91,35 +82,21 @@ class better_zip_geonames_import(orm.TransientModel):
bzip_ids_to_delete = bzip_obj.search(
cr, uid, [('country_id', '=', country_id)], context=context)
if bzip_ids_to_delete:
cr.execute('SELECT id FROM res_better_zip WHERE id in %s '
'FOR UPDATE NOWAIT', (tuple(bzip_ids_to_delete), ))
bzip_obj.unlink(cr, uid, bzip_ids_to_delete, context=context)
logger.info(
'%d better zip entries deleted for country %s'
% (len(bzip_ids_to_delete), wizard.country_id.name))
state_ids = self.pool['res.country.state'].search(
cr, uid, [('country_id', '=', country_id)], context=context)
states = {}
# key = code of the state ; value = ID of the state in OpenERP
if state_ids:
states_r = self.pool['res.country.state'].read(
cr, uid, state_ids, ['code', 'country_id'], context=context)
for state in states_r:
states[state['code'].upper()] = state['id']
f_geonames = zipfile.ZipFile(StringIO.StringIO(res_request.content))
tempdir = tempfile.mkdtemp(prefix='openerp')
f_geonames.extract('%s.txt' % country_code, tempdir)
logger.info('The geonames zipfile has been decompressed')
data_file = open(os.path.join(tempdir, '%s.txt' % country_code), 'r')
data_file.seek(0)
logger.info(
'Starting to create the better zip entries %s state information'
% (states and 'with' or 'without'))
logger.info('Starting to create the better zip entries')
for row in unicodecsv.reader(
data_file, encoding='utf-8', delimiter=' '):
self.create_better_zip(
cr, uid, row, country_id, country_code, states,
context=context)
cr, uid, row, country_id, country_code, context=context)
data_file.close()
logger.info(
'The wizard to create better zip entries from geonames '

4
base_location_geonames_import/wizard/geonames_import_view.xml

@ -14,9 +14,9 @@
<field name="arch" type="xml">
<form string="Import Geonames" version="7.0">
<group name="main">
<field name="country_id"/>
<label string="For the country selected above, this wizard 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."
<label string="This wizard will delete all the better zip entries for the selected country, download the latest version from geonames.org for that country and create new better zip entries."
colspan="2" name="import-help"/>
<field name="country_id"/>
</group>
<footer>
<button name="run_import" type="object"

Loading…
Cancel
Save