diff --git a/base_location_geonames_import/test/import.yml b/base_location_geonames_import/test/import.yml index 66243d24a..9dc93c2f0 100644 --- a/base_location_geonames_import/test/import.yml +++ b/base_location_geonames_import/test/import.yml @@ -7,20 +7,22 @@ I run the import - !python {model: better.zip.geonames.import}: | - self.run_import(cr, uid, [ref('import_wizard_1')], context=context) + ctx = context.copy() + ctx['max_import'] = 10 + self.run_import(cr, uid, [ref('import_wizard_1')], context=ctx) - 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'), + ('code', '=', 'AB'), ('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'), + ('name', '=', '67010'), + ('city', '=', 'Barete'), ('state_id', '=', state_ids[0]), ('country_id', '=', ref('base.it')) ], context=context) @@ -34,20 +36,22 @@ I run the import again - !python {model: better.zip.geonames.import}: | - self.run_import(cr, uid, [ref('import_wizard_2')], context=context) + ctx = context.copy() + ctx['max_import'] = 10 + self.run_import(cr, uid, [ref('import_wizard_2')], context=ctx) - 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'), + ('code', '=', 'AB'), ('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'), + ('name', '=', '67010'), + ('city', '=', 'Barete'), ('state_id', '=', state_ids[0]), ('country_id', '=', ref('base.it')) ], context=context) diff --git a/base_location_geonames_import/wizard/geonames_import.py b/base_location_geonames_import/wizard/geonames_import.py index a2aa28315..7613b7466 100644 --- a/base_location_geonames_import/wizard/geonames_import.py +++ b/base_location_geonames_import/wizard/geonames_import.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- # © 2014 Alexis de Lattre # © 2014 Lorenzo Battistini +# © 2016 Pedro M. Baeza # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from openerp import models, fields, api, _ @@ -101,8 +102,9 @@ class BetterZipGeonamesImport(models.TransientModel): 'country_id': country.id }) - @api.one + @api.multi def run_import(self): + self.ensure_one() zip_model = self.env['res.better.zip'] country_code = self.country_id.code config_url = self.env['ir.config_parameter'].get_param( @@ -125,13 +127,16 @@ class BetterZipGeonamesImport(models.TransientModel): 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=' '): - zip = self.create_better_zip(row, self.country_id) - if zip in zips_to_delete: - zips_to_delete -= zip + max_import = self.env.context.get('max_import', 0) + reader = unicodecsv.reader(data_file, encoding='utf-8', delimiter=' ') + for i, row in enumerate(reader): + zip_code = self.create_better_zip(row, self.country_id) + if zip_code in zips_to_delete: + zips_to_delete -= zip_code + if max_import and i == max_import: + break data_file.close() - if zips_to_delete: + if zips_to_delete and not max_import: zips_to_delete.unlink() logger.info('%d better zip entries deleted for country %s' % (len(zips_to_delete), self.country_id.name))