Browse Source

[IMP] base_location_geonames_import

* Speed improvement applying cache for not doing duplicated searches
  over states.
* Tests change to SavepointCase, which only passes over setUp one time.
pull/494/head
Pedro M. Baeza 8 years ago
parent
commit
3a5389368f
  1. 13
      base_location_geonames_import/tests/test_base_location_geonames_import.py
  2. 38
      base_location_geonames_import/wizard/geonames_import.py

13
base_location_geonames_import/tests/test_base_location_geonames_import.py

@ -5,12 +5,13 @@
from odoo.tests import common
class TestBaseLocationGeonamesImport(common.TransactionCase):
def setUp(self):
super(TestBaseLocationGeonamesImport, self).setUp()
self.country = self.env.ref('base.mc')
self.wizard = self.env['better.zip.geonames.import'].create({
'country_id': self.country.id,
class TestBaseLocationGeonamesImport(common.SavepointCase):
@classmethod
def setUpClass(cls):
super(TestBaseLocationGeonamesImport, cls).setUpClass()
cls.country = cls.env.ref('base.mc')
cls.wizard = cls.env['better.zip.geonames.import'].create({
'country_id': cls.country.id,
})
def test_import_country(self):

38
base_location_geonames_import/wizard/geonames_import.py

@ -4,7 +4,7 @@
# © 2016 Pedro M. Baeza <pedro.baeza@serviciosbaeza.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import models, fields, api, _
from odoo import _, api, fields, models, tools
from odoo.exceptions import UserError
import requests
import tempfile
@ -86,28 +86,30 @@ class BetterZipGeonamesImport(models.TransientModel):
vals = self._prepare_better_zip(row, country)
if vals:
return zip_model.create(vals)
else:
else: # pragma: no cover
return False
@tools.ormcache('country_id', 'code')
def _get_state(self, country_id, code, name):
state = self.env['res.country.state'].search(
[('country_id', '=', country_id),
('code', '=', code)], limit=1,
)
if state: # pragma: no cover
return state
else:
return self.env['res.country.state'].create({
'name': name,
'code': code,
'country_id': country_id,
})
@api.model
def select_or_create_state(
self, row, country, 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 UserError(
_("Too many states with code %s for country %s")
% (row[code_row_index], country.code))
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
})
return self._get_state(
country.id, row[code_row_index], row[name_row_index],
)
@api.multi
def run_import(self):

Loading…
Cancel
Save