@ -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 ) :