Browse Source

Add module base_location_geonames_import

pull/22/head
Alexis de Lattre 11 years ago
committed by Lorenzo Battistini
parent
commit
47a3f0a301
  1. 23
      base_location_geonames_import/__init__.py
  2. 47
      base_location_geonames_import/__openerp__.py
  3. 23
      base_location_geonames_import/wizard/__init__.py
  4. 104
      base_location_geonames_import/wizard/geonames_import.py
  5. 44
      base_location_geonames_import/wizard/geonames_import_view.xml

23
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 <alexis.delattre@akretion.com>
#
# 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 <http://www.gnu.org/licenses/>.
#
##############################################################################
from . import wizard

47
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 <alexis.delattre@akretion.com>
#
# 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 <http://www.gnu.org/licenses/>.
#
##############################################################################
{
'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 <alexis.delattre@akretion.com> 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,
}

23
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 <alexis.delattre@akretion.com>
#
# 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 <http://www.gnu.org/licenses/>.
#
##############################################################################
from . import geonames_import

104
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 <alexis.delattre@akretion.com>
#
# 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 <http://www.gnu.org/licenses/>.
#
##############################################################################
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

44
base_location_geonames_import/wizard/geonames_import_view.xml

@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2014 Akretion (http://www.akretion.com/)
@author: Alexis de Lattre <alexis.delattre@akretion.com>
The licence is in the file __openerp__.py
-->
<openerp>
<data>
<record id="better_zip_geonames_import_form" model="ir.ui.view">
<field name="name">asterisk.server.company</field>
<field name="model">better.zip.geonames.import</field>
<field name="arch" type="xml">
<form string="Import Geonames" version="7.0">
<group name="main">
<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"
class="oe_highlight" string="Import"/>
<button special="cancel" string="Cancel" class="oe_link"/>
</footer>
</form>
</field>
</record>
<record id="better_zip_geonames_import_action" model="ir.actions.act_window">
<field name="name">Import Geonames</field>
<field name="res_model">better.zip.geonames.import</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="target">new</field>
</record>
<menuitem id="better_zip_geonames_import_menu"
action="better_zip_geonames_import_action"
parent="base.menu_localisation"
sequence="50"/>
</data>
</openerp>
Loading…
Cancel
Save