From 30535503afb8ab5e861a8faccbd37abf1e18143c Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Sun, 30 Mar 2014 23:50:40 +0200 Subject: [PATCH 01/39] Add module base_location_geonames_import --- base_location_geonames_import/__init__.py | 23 ++++ base_location_geonames_import/__openerp__.py | 47 ++++++++ .../wizard/__init__.py | 23 ++++ .../wizard/geonames_import.py | 104 ++++++++++++++++++ .../wizard/geonames_import_view.xml | 44 ++++++++ 5 files changed, 241 insertions(+) create mode 100644 base_location_geonames_import/__init__.py create mode 100644 base_location_geonames_import/__openerp__.py create mode 100644 base_location_geonames_import/wizard/__init__.py create mode 100644 base_location_geonames_import/wizard/geonames_import.py create mode 100644 base_location_geonames_import/wizard/geonames_import_view.xml diff --git a/base_location_geonames_import/__init__.py b/base_location_geonames_import/__init__.py new file mode 100644 index 000000000..b63234221 --- /dev/null +++ b/base_location_geonames_import/__init__.py @@ -0,0 +1,23 @@ +# -*- encoding: utf-8 -*- +############################################################################## +# +# Base Location Geonames Import module for OpenERP +# Copyright (C) 2014 Akretion (http://www.akretion.com) +# @author Alexis de Lattre +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## + +from . import wizard diff --git a/base_location_geonames_import/__openerp__.py b/base_location_geonames_import/__openerp__.py new file mode 100644 index 000000000..b3d9ad11a --- /dev/null +++ b/base_location_geonames_import/__openerp__.py @@ -0,0 +1,47 @@ +# -*- encoding: utf-8 -*- +############################################################################## +# +# Base Location Geonames Import module for OpenERP +# Copyright (C) 2014 Akretion (http://www.akretion.com) +# @author Alexis de Lattre +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## + + +{ + 'name': 'Base Location Geonames Import', + 'version': '0.1', + 'category': 'Extra Tools', + 'license': 'AGPL-3', + 'summary': 'Import better zip entries from Geonames', + 'description': """ +Base Location Geonames Import +============================= + +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 for any help or question about this module. + """, + 'author': 'Akretion', + 'website': 'http://www.akretion.com', + 'depends': ['base_location'], + 'external_dependencies': {'python': ['requests', 'unicodecsv']}, + 'data': [ + 'wizard/geonames_import_view.xml', + ], + 'installable': True, + 'active': False, +} diff --git a/base_location_geonames_import/wizard/__init__.py b/base_location_geonames_import/wizard/__init__.py new file mode 100644 index 000000000..2c9462e69 --- /dev/null +++ b/base_location_geonames_import/wizard/__init__.py @@ -0,0 +1,23 @@ +# -*- encoding: utf-8 -*- +############################################################################## +# +# Base Location Geonames Import module for OpenERP +# Copyright (C) 2014 Akretion (http://www.akretion.com) +# @author Alexis de Lattre +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## + +from . import geonames_import diff --git a/base_location_geonames_import/wizard/geonames_import.py b/base_location_geonames_import/wizard/geonames_import.py new file mode 100644 index 000000000..79be0c2f7 --- /dev/null +++ b/base_location_geonames_import/wizard/geonames_import.py @@ -0,0 +1,104 @@ +# -*- encoding: utf-8 -*- +############################################################################## +# +# Base Location Geonames Import module for OpenERP +# Copyright (C) 2014 Akretion (http://www.akretion.com) +# @author Alexis de Lattre +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## + +from openerp.osv import orm, fields +from openerp.tools.translate import _ +import requests +import tempfile +import StringIO +import unicodecsv +import zipfile +import os +import logging + +logger = logging.getLogger(__name__) + + +class better_zip_geonames_import(orm.TransientModel): + _name = 'better.zip.geonames.import' + _description = 'Import Better Zip from Geonames' + + _columns = { + 'country_id': fields.many2one('res.country', 'Country', required=True), + } + + def _prepare_better_zip( + self, cr, uid, row, country_id, context=None): + '''This function is designed to be inherited''' + return { + 'name': row[1], + 'city': row[2], + 'country_id': country_id, + } + + def create_better_zip( + self, cr, uid, row, country_id, country_code, context=None): + bzip_id = False + if row[0] != country_code: + raise orm.except_orm( + _('Error:'), + _("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] and 'CEDEX' not in row[1]: + vals = self._prepare_better_zip( + 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): + assert len(ids) == 1, 'Only one ID for the better zip import wizard' + bzip_obj = self.pool['res.better.zip'] + wizard = self.browse(cr, uid, ids[0], context=context) + country_id = wizard.country_id.id + country_code = wizard.country_id.code.upper() + url = 'http://download.geonames.org/export/zip/%s.zip' % country_code + logger.info('Starting to download %s' % url) + res_request = requests.get(url) + if res_request.status_code != requests.codes.ok: + raise orm.except_orm( + _('Error:'), + _('Got an error %d when trying to download the file %s.') + % (res_request.status_code, url)) + bzip_ids_to_delete = bzip_obj.search( + cr, uid, [('country_id', '=', country_id)], context=context) + if 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)) + 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') + for row in unicodecsv.reader( + data_file, encoding='utf-8', delimiter=' '): + self.create_better_zip( + cr, uid, row, country_id, country_code, context=context) + data_file.close() + logger.info( + 'The wizard to create better zip entries from geonames ' + 'has been successfully completed.') + return True diff --git a/base_location_geonames_import/wizard/geonames_import_view.xml b/base_location_geonames_import/wizard/geonames_import_view.xml new file mode 100644 index 000000000..9608134b4 --- /dev/null +++ b/base_location_geonames_import/wizard/geonames_import_view.xml @@ -0,0 +1,44 @@ + + + + + + + + asterisk.server.company + better.zip.geonames.import + +
+ + +
+
+
+
+
+ + + Import Geonames + better.zip.geonames.import + form + form + new + + + + +
+
From e0785faa1c71bf94ea02e93df40f25b7021591ad Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Mon, 7 Apr 2014 17:12:35 +0200 Subject: [PATCH 02/39] Add support for states (if states are already present in res.country.state). Add POT file and FR translation. --- .../i18n/base_location_geonames_import.pot | 68 +++++++++++++++++++ base_location_geonames_import/i18n/fr.po | 68 +++++++++++++++++++ .../wizard/geonames_import.py | 37 +++++++--- .../wizard/geonames_import_view.xml | 4 +- 4 files changed, 167 insertions(+), 10 deletions(-) create mode 100644 base_location_geonames_import/i18n/base_location_geonames_import.pot create mode 100644 base_location_geonames_import/i18n/fr.po diff --git a/base_location_geonames_import/i18n/base_location_geonames_import.pot b/base_location_geonames_import/i18n/base_location_geonames_import.pot new file mode 100644 index 000000000..d45e4e250 --- /dev/null +++ b/base_location_geonames_import/i18n/base_location_geonames_import.pot @@ -0,0 +1,68 @@ +# Translation of OpenERP Server. +# This file contains the translation of the following modules: +# * base_location_geonames_import +# +msgid "" +msgstr "" +"Project-Id-Version: OpenERP Server 7.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2014-04-07 15:00+0000\n" +"PO-Revision-Date: 2014-04-07 15:00+0000\n" +"Last-Translator: <>\n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: base_location_geonames_import +#: view:better.zip.geonames.import:0 +msgid "Cancel" +msgstr "" + +#. module: base_location_geonames_import +#: field:better.zip.geonames.import,country_id:0 +msgid "Country" +msgstr "" + +#. module: base_location_geonames_import +#: code:addons/base_location_geonames_import/wizard/geonames_import.py:64 +#: code:addons/base_location_geonames_import/wizard/geonames_import.py:87 +#, python-format +msgid "Error:" +msgstr "" + +#. module: base_location_geonames_import +#: view:better.zip.geonames.import:0 +msgid "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." +msgstr "" + +#. module: base_location_geonames_import +#: code:addons/base_location_geonames_import/wizard/geonames_import.py:88 +#, python-format +msgid "Got an error %d when trying to download the file %s." +msgstr "" + +#. module: base_location_geonames_import +#: view:better.zip.geonames.import:0 +msgid "Import" +msgstr "" + +#. module: base_location_geonames_import +#: model:ir.model,name:base_location_geonames_import.model_better_zip_geonames_import +msgid "Import Better Zip from Geonames" +msgstr "" + +#. module: base_location_geonames_import +#: view:better.zip.geonames.import:0 +#: model:ir.actions.act_window,name:base_location_geonames_import.better_zip_geonames_import_action +#: model:ir.ui.menu,name:base_location_geonames_import.better_zip_geonames_import_menu +msgid "Import Geonames" +msgstr "" + +#. module: base_location_geonames_import +#: code:addons/base_location_geonames_import/wizard/geonames_import.py:65 +#, python-format +msgid "The content of the file doesn't correspond to the selected country." +msgstr "" + diff --git a/base_location_geonames_import/i18n/fr.po b/base_location_geonames_import/i18n/fr.po new file mode 100644 index 000000000..bfd7eebf9 --- /dev/null +++ b/base_location_geonames_import/i18n/fr.po @@ -0,0 +1,68 @@ +# Translation of OpenERP Server. +# This file contains the translation of the following modules: +# * base_location_geonames_import +# +msgid "" +msgstr "" +"Project-Id-Version: OpenERP Server 7.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2014-04-07 15:01+0000\n" +"PO-Revision-Date: 2014-04-07 15:01+0000\n" +"Last-Translator: Alexis de Lattre \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: base_location_geonames_import +#: view:better.zip.geonames.import:0 +msgid "Cancel" +msgstr "Annuler" + +#. module: base_location_geonames_import +#: field:better.zip.geonames.import,country_id:0 +msgid "Country" +msgstr "Pays" + +#. module: base_location_geonames_import +#: code:addons/base_location_geonames_import/wizard/geonames_import.py:64 +#: code:addons/base_location_geonames_import/wizard/geonames_import.py:87 +#, python-format +msgid "Error:" +msgstr "Erreur :" + +#. module: base_location_geonames_import +#: view:better.zip.geonames.import:0 +msgid "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." +msgstr "Pour le pays sélectionné ci-dessus, cet assistant va supprimer toutes les entrées better zip, télécharger la dernière version de la liste des villes depuis geonames.org et créer de nouveaux enregistrements better zip." + +#. module: base_location_geonames_import +#: code:addons/base_location_geonames_import/wizard/geonames_import.py:88 +#, python-format +msgid "Got an error %d when trying to download the file %s." +msgstr "Erreur %d reçue suite à la tentative de téléchargement du fichier %s." + +#. module: base_location_geonames_import +#: view:better.zip.geonames.import:0 +msgid "Import" +msgstr "Importer" + +#. module: base_location_geonames_import +#: model:ir.model,name:base_location_geonames_import.model_better_zip_geonames_import +msgid "Import Better Zip from Geonames" +msgstr "Import Better Zip depuis Geonames" + +#. module: base_location_geonames_import +#: view:better.zip.geonames.import:0 +#: model:ir.actions.act_window,name:base_location_geonames_import.better_zip_geonames_import_action +#: model:ir.ui.menu,name:base_location_geonames_import.better_zip_geonames_import_menu +msgid "Import Geonames" +msgstr "Importer Geonames" + +#. module: base_location_geonames_import +#: code:addons/base_location_geonames_import/wizard/geonames_import.py:65 +#, python-format +msgid "The content of the file doesn't correspond to the selected country." +msgstr "Le contenu du fichier ne correspond pas au pays sélectionné." + diff --git a/base_location_geonames_import/wizard/geonames_import.py b/base_location_geonames_import/wizard/geonames_import.py index 79be0c2f7..d94a60e7c 100644 --- a/base_location_geonames_import/wizard/geonames_import.py +++ b/base_location_geonames_import/wizard/geonames_import.py @@ -42,25 +42,34 @@ class better_zip_geonames_import(orm.TransientModel): } def _prepare_better_zip( - self, cr, uid, row, country_id, context=None): + self, cr, uid, row, country_id, states, context=None): '''This function is designed to be inherited''' - return { + state_id = False + if states and row[4] and row[4] in states: + state_id = states[row[4].upper()] + vals = { '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, context=None): + self, cr, uid, row, country_id, country_code, states, + context=None): bzip_id = False if row[0] != country_code: raise orm.except_orm( _('Error:'), - _("The content file doesn't correspond to the country")) + _("The content of the file doesn't correspond to the " + "selected country.")) logger.debug('ZIP = %s - City = %s' % (row[1], row[2])) - if row[1] and row[2] and 'CEDEX' not in row[1]: + if row[0] == 'FR' and 'CEDEX' in row[1]: + return False + if row[1] and row[2]: vals = self._prepare_better_zip( - cr, uid, row, country_id, context=context) + cr, uid, row, country_id, states, context=context) bzip_id = self.pool['res.better.zip'].create( cr, uid, vals, context=context) return bzip_id @@ -86,17 +95,29 @@ class better_zip_geonames_import(orm.TransientModel): 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') + logger.info( + 'Starting to create the better zip entries %s state information' + % (states and 'with' or 'without')) for row in unicodecsv.reader( data_file, encoding='utf-8', delimiter=' '): self.create_better_zip( - cr, uid, row, country_id, country_code, context=context) + cr, uid, row, country_id, country_code, states, + context=context) data_file.close() logger.info( 'The wizard to create better zip entries from geonames ' diff --git a/base_location_geonames_import/wizard/geonames_import_view.xml b/base_location_geonames_import/wizard/geonames_import_view.xml index 9608134b4..f86ea17da 100644 --- a/base_location_geonames_import/wizard/geonames_import_view.xml +++ b/base_location_geonames_import/wizard/geonames_import_view.xml @@ -14,9 +14,9 @@
-
+
+
+ + + + Import Geonames + better.zip.geonames.import + form + form + new + + + + + + From 828f466be94ce1841456a7ff769eb32bd59262d8 Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Mon, 7 Apr 2014 17:12:35 +0200 Subject: [PATCH 08/39] Add support for states (if states are already present in res.country.state). Add POT file and FR translation. --- .../i18n/base_location_geonames_import.pot | 68 +++++++++++++++++++ base_location_geonames_import/i18n/fr.po | 68 +++++++++++++++++++ .../wizard/geonames_import.py | 37 +++++++--- .../wizard/geonames_import_view.xml | 4 +- 4 files changed, 167 insertions(+), 10 deletions(-) create mode 100644 base_location_geonames_import/i18n/base_location_geonames_import.pot create mode 100644 base_location_geonames_import/i18n/fr.po diff --git a/base_location_geonames_import/i18n/base_location_geonames_import.pot b/base_location_geonames_import/i18n/base_location_geonames_import.pot new file mode 100644 index 000000000..d45e4e250 --- /dev/null +++ b/base_location_geonames_import/i18n/base_location_geonames_import.pot @@ -0,0 +1,68 @@ +# Translation of OpenERP Server. +# This file contains the translation of the following modules: +# * base_location_geonames_import +# +msgid "" +msgstr "" +"Project-Id-Version: OpenERP Server 7.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2014-04-07 15:00+0000\n" +"PO-Revision-Date: 2014-04-07 15:00+0000\n" +"Last-Translator: <>\n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: base_location_geonames_import +#: view:better.zip.geonames.import:0 +msgid "Cancel" +msgstr "" + +#. module: base_location_geonames_import +#: field:better.zip.geonames.import,country_id:0 +msgid "Country" +msgstr "" + +#. module: base_location_geonames_import +#: code:addons/base_location_geonames_import/wizard/geonames_import.py:64 +#: code:addons/base_location_geonames_import/wizard/geonames_import.py:87 +#, python-format +msgid "Error:" +msgstr "" + +#. module: base_location_geonames_import +#: view:better.zip.geonames.import:0 +msgid "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." +msgstr "" + +#. module: base_location_geonames_import +#: code:addons/base_location_geonames_import/wizard/geonames_import.py:88 +#, python-format +msgid "Got an error %d when trying to download the file %s." +msgstr "" + +#. module: base_location_geonames_import +#: view:better.zip.geonames.import:0 +msgid "Import" +msgstr "" + +#. module: base_location_geonames_import +#: model:ir.model,name:base_location_geonames_import.model_better_zip_geonames_import +msgid "Import Better Zip from Geonames" +msgstr "" + +#. module: base_location_geonames_import +#: view:better.zip.geonames.import:0 +#: model:ir.actions.act_window,name:base_location_geonames_import.better_zip_geonames_import_action +#: model:ir.ui.menu,name:base_location_geonames_import.better_zip_geonames_import_menu +msgid "Import Geonames" +msgstr "" + +#. module: base_location_geonames_import +#: code:addons/base_location_geonames_import/wizard/geonames_import.py:65 +#, python-format +msgid "The content of the file doesn't correspond to the selected country." +msgstr "" + diff --git a/base_location_geonames_import/i18n/fr.po b/base_location_geonames_import/i18n/fr.po new file mode 100644 index 000000000..bfd7eebf9 --- /dev/null +++ b/base_location_geonames_import/i18n/fr.po @@ -0,0 +1,68 @@ +# Translation of OpenERP Server. +# This file contains the translation of the following modules: +# * base_location_geonames_import +# +msgid "" +msgstr "" +"Project-Id-Version: OpenERP Server 7.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2014-04-07 15:01+0000\n" +"PO-Revision-Date: 2014-04-07 15:01+0000\n" +"Last-Translator: Alexis de Lattre \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: base_location_geonames_import +#: view:better.zip.geonames.import:0 +msgid "Cancel" +msgstr "Annuler" + +#. module: base_location_geonames_import +#: field:better.zip.geonames.import,country_id:0 +msgid "Country" +msgstr "Pays" + +#. module: base_location_geonames_import +#: code:addons/base_location_geonames_import/wizard/geonames_import.py:64 +#: code:addons/base_location_geonames_import/wizard/geonames_import.py:87 +#, python-format +msgid "Error:" +msgstr "Erreur :" + +#. module: base_location_geonames_import +#: view:better.zip.geonames.import:0 +msgid "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." +msgstr "Pour le pays sélectionné ci-dessus, cet assistant va supprimer toutes les entrées better zip, télécharger la dernière version de la liste des villes depuis geonames.org et créer de nouveaux enregistrements better zip." + +#. module: base_location_geonames_import +#: code:addons/base_location_geonames_import/wizard/geonames_import.py:88 +#, python-format +msgid "Got an error %d when trying to download the file %s." +msgstr "Erreur %d reçue suite à la tentative de téléchargement du fichier %s." + +#. module: base_location_geonames_import +#: view:better.zip.geonames.import:0 +msgid "Import" +msgstr "Importer" + +#. module: base_location_geonames_import +#: model:ir.model,name:base_location_geonames_import.model_better_zip_geonames_import +msgid "Import Better Zip from Geonames" +msgstr "Import Better Zip depuis Geonames" + +#. module: base_location_geonames_import +#: view:better.zip.geonames.import:0 +#: model:ir.actions.act_window,name:base_location_geonames_import.better_zip_geonames_import_action +#: model:ir.ui.menu,name:base_location_geonames_import.better_zip_geonames_import_menu +msgid "Import Geonames" +msgstr "Importer Geonames" + +#. module: base_location_geonames_import +#: code:addons/base_location_geonames_import/wizard/geonames_import.py:65 +#, python-format +msgid "The content of the file doesn't correspond to the selected country." +msgstr "Le contenu du fichier ne correspond pas au pays sélectionné." + diff --git a/base_location_geonames_import/wizard/geonames_import.py b/base_location_geonames_import/wizard/geonames_import.py index 79be0c2f7..d94a60e7c 100644 --- a/base_location_geonames_import/wizard/geonames_import.py +++ b/base_location_geonames_import/wizard/geonames_import.py @@ -42,25 +42,34 @@ class better_zip_geonames_import(orm.TransientModel): } def _prepare_better_zip( - self, cr, uid, row, country_id, context=None): + self, cr, uid, row, country_id, states, context=None): '''This function is designed to be inherited''' - return { + state_id = False + if states and row[4] and row[4] in states: + state_id = states[row[4].upper()] + vals = { '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, context=None): + self, cr, uid, row, country_id, country_code, states, + context=None): bzip_id = False if row[0] != country_code: raise orm.except_orm( _('Error:'), - _("The content file doesn't correspond to the country")) + _("The content of the file doesn't correspond to the " + "selected country.")) logger.debug('ZIP = %s - City = %s' % (row[1], row[2])) - if row[1] and row[2] and 'CEDEX' not in row[1]: + if row[0] == 'FR' and 'CEDEX' in row[1]: + return False + if row[1] and row[2]: vals = self._prepare_better_zip( - cr, uid, row, country_id, context=context) + cr, uid, row, country_id, states, context=context) bzip_id = self.pool['res.better.zip'].create( cr, uid, vals, context=context) return bzip_id @@ -86,17 +95,29 @@ class better_zip_geonames_import(orm.TransientModel): 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') + logger.info( + 'Starting to create the better zip entries %s state information' + % (states and 'with' or 'without')) for row in unicodecsv.reader( data_file, encoding='utf-8', delimiter=' '): self.create_better_zip( - cr, uid, row, country_id, country_code, context=context) + cr, uid, row, country_id, country_code, states, + context=context) data_file.close() logger.info( 'The wizard to create better zip entries from geonames ' diff --git a/base_location_geonames_import/wizard/geonames_import_view.xml b/base_location_geonames_import/wizard/geonames_import_view.xml index 9608134b4..f86ea17da 100644 --- a/base_location_geonames_import/wizard/geonames_import_view.xml +++ b/base_location_geonames_import/wizard/geonames_import_view.xml @@ -14,9 +14,9 @@
-
+
+
+ + + + Import Geonames + better.zip.geonames.import + form + form + new + + + + + + From 9f942f9b730e536b26f0050f3911ddf1076131cf Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Mon, 7 Apr 2014 17:12:35 +0200 Subject: [PATCH 20/39] Add support for states (if states are already present in res.country.state). Add POT file and FR translation. --- .../i18n/base_location_geonames_import.pot | 68 +++++++++++++++++++ base_location_geonames_import/i18n/fr.po | 68 +++++++++++++++++++ .../wizard/geonames_import.py | 37 +++++++--- .../wizard/geonames_import_view.xml | 4 +- 4 files changed, 167 insertions(+), 10 deletions(-) create mode 100644 base_location_geonames_import/i18n/base_location_geonames_import.pot create mode 100644 base_location_geonames_import/i18n/fr.po diff --git a/base_location_geonames_import/i18n/base_location_geonames_import.pot b/base_location_geonames_import/i18n/base_location_geonames_import.pot new file mode 100644 index 000000000..d45e4e250 --- /dev/null +++ b/base_location_geonames_import/i18n/base_location_geonames_import.pot @@ -0,0 +1,68 @@ +# Translation of OpenERP Server. +# This file contains the translation of the following modules: +# * base_location_geonames_import +# +msgid "" +msgstr "" +"Project-Id-Version: OpenERP Server 7.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2014-04-07 15:00+0000\n" +"PO-Revision-Date: 2014-04-07 15:00+0000\n" +"Last-Translator: <>\n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: base_location_geonames_import +#: view:better.zip.geonames.import:0 +msgid "Cancel" +msgstr "" + +#. module: base_location_geonames_import +#: field:better.zip.geonames.import,country_id:0 +msgid "Country" +msgstr "" + +#. module: base_location_geonames_import +#: code:addons/base_location_geonames_import/wizard/geonames_import.py:64 +#: code:addons/base_location_geonames_import/wizard/geonames_import.py:87 +#, python-format +msgid "Error:" +msgstr "" + +#. module: base_location_geonames_import +#: view:better.zip.geonames.import:0 +msgid "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." +msgstr "" + +#. module: base_location_geonames_import +#: code:addons/base_location_geonames_import/wizard/geonames_import.py:88 +#, python-format +msgid "Got an error %d when trying to download the file %s." +msgstr "" + +#. module: base_location_geonames_import +#: view:better.zip.geonames.import:0 +msgid "Import" +msgstr "" + +#. module: base_location_geonames_import +#: model:ir.model,name:base_location_geonames_import.model_better_zip_geonames_import +msgid "Import Better Zip from Geonames" +msgstr "" + +#. module: base_location_geonames_import +#: view:better.zip.geonames.import:0 +#: model:ir.actions.act_window,name:base_location_geonames_import.better_zip_geonames_import_action +#: model:ir.ui.menu,name:base_location_geonames_import.better_zip_geonames_import_menu +msgid "Import Geonames" +msgstr "" + +#. module: base_location_geonames_import +#: code:addons/base_location_geonames_import/wizard/geonames_import.py:65 +#, python-format +msgid "The content of the file doesn't correspond to the selected country." +msgstr "" + diff --git a/base_location_geonames_import/i18n/fr.po b/base_location_geonames_import/i18n/fr.po new file mode 100644 index 000000000..bfd7eebf9 --- /dev/null +++ b/base_location_geonames_import/i18n/fr.po @@ -0,0 +1,68 @@ +# Translation of OpenERP Server. +# This file contains the translation of the following modules: +# * base_location_geonames_import +# +msgid "" +msgstr "" +"Project-Id-Version: OpenERP Server 7.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2014-04-07 15:01+0000\n" +"PO-Revision-Date: 2014-04-07 15:01+0000\n" +"Last-Translator: Alexis de Lattre \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: base_location_geonames_import +#: view:better.zip.geonames.import:0 +msgid "Cancel" +msgstr "Annuler" + +#. module: base_location_geonames_import +#: field:better.zip.geonames.import,country_id:0 +msgid "Country" +msgstr "Pays" + +#. module: base_location_geonames_import +#: code:addons/base_location_geonames_import/wizard/geonames_import.py:64 +#: code:addons/base_location_geonames_import/wizard/geonames_import.py:87 +#, python-format +msgid "Error:" +msgstr "Erreur :" + +#. module: base_location_geonames_import +#: view:better.zip.geonames.import:0 +msgid "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." +msgstr "Pour le pays sélectionné ci-dessus, cet assistant va supprimer toutes les entrées better zip, télécharger la dernière version de la liste des villes depuis geonames.org et créer de nouveaux enregistrements better zip." + +#. module: base_location_geonames_import +#: code:addons/base_location_geonames_import/wizard/geonames_import.py:88 +#, python-format +msgid "Got an error %d when trying to download the file %s." +msgstr "Erreur %d reçue suite à la tentative de téléchargement du fichier %s." + +#. module: base_location_geonames_import +#: view:better.zip.geonames.import:0 +msgid "Import" +msgstr "Importer" + +#. module: base_location_geonames_import +#: model:ir.model,name:base_location_geonames_import.model_better_zip_geonames_import +msgid "Import Better Zip from Geonames" +msgstr "Import Better Zip depuis Geonames" + +#. module: base_location_geonames_import +#: view:better.zip.geonames.import:0 +#: model:ir.actions.act_window,name:base_location_geonames_import.better_zip_geonames_import_action +#: model:ir.ui.menu,name:base_location_geonames_import.better_zip_geonames_import_menu +msgid "Import Geonames" +msgstr "Importer Geonames" + +#. module: base_location_geonames_import +#: code:addons/base_location_geonames_import/wizard/geonames_import.py:65 +#, python-format +msgid "The content of the file doesn't correspond to the selected country." +msgstr "Le contenu du fichier ne correspond pas au pays sélectionné." + diff --git a/base_location_geonames_import/wizard/geonames_import.py b/base_location_geonames_import/wizard/geonames_import.py index 79be0c2f7..d94a60e7c 100644 --- a/base_location_geonames_import/wizard/geonames_import.py +++ b/base_location_geonames_import/wizard/geonames_import.py @@ -42,25 +42,34 @@ class better_zip_geonames_import(orm.TransientModel): } def _prepare_better_zip( - self, cr, uid, row, country_id, context=None): + self, cr, uid, row, country_id, states, context=None): '''This function is designed to be inherited''' - return { + state_id = False + if states and row[4] and row[4] in states: + state_id = states[row[4].upper()] + vals = { '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, context=None): + self, cr, uid, row, country_id, country_code, states, + context=None): bzip_id = False if row[0] != country_code: raise orm.except_orm( _('Error:'), - _("The content file doesn't correspond to the country")) + _("The content of the file doesn't correspond to the " + "selected country.")) logger.debug('ZIP = %s - City = %s' % (row[1], row[2])) - if row[1] and row[2] and 'CEDEX' not in row[1]: + if row[0] == 'FR' and 'CEDEX' in row[1]: + return False + if row[1] and row[2]: vals = self._prepare_better_zip( - cr, uid, row, country_id, context=context) + cr, uid, row, country_id, states, context=context) bzip_id = self.pool['res.better.zip'].create( cr, uid, vals, context=context) return bzip_id @@ -86,17 +95,29 @@ class better_zip_geonames_import(orm.TransientModel): 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') + logger.info( + 'Starting to create the better zip entries %s state information' + % (states and 'with' or 'without')) for row in unicodecsv.reader( data_file, encoding='utf-8', delimiter=' '): self.create_better_zip( - cr, uid, row, country_id, country_code, context=context) + cr, uid, row, country_id, country_code, states, + context=context) data_file.close() logger.info( 'The wizard to create better zip entries from geonames ' diff --git a/base_location_geonames_import/wizard/geonames_import_view.xml b/base_location_geonames_import/wizard/geonames_import_view.xml index 9608134b4..f86ea17da 100644 --- a/base_location_geonames_import/wizard/geonames_import_view.xml +++ b/base_location_geonames_import/wizard/geonames_import_view.xml @@ -14,9 +14,9 @@
-