@ -4,7 +4,7 @@
# © 2016 Pedro M. Baeza <pedro.baeza@serviciosbaeza.com>
# © 2016 Pedro M. Baeza <pedro.baeza@serviciosbaeza.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
# 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
from odoo.exceptions import UserError
import requests
import requests
import tempfile
import tempfile
@ -70,10 +70,10 @@ class BetterZipGeonamesImport(models.TransientModel):
" correspond to the selected country ( %s ). " )
" correspond to the selected country ( %s ). " )
% ( row [ 0 ] , country . code ) )
% ( row [ 0 ] , country . code ) )
logger . debug ( ' ZIP = %s - City = %s ' % ( row [ 1 ] , row [ 2 ] ) )
logger . debug ( ' ZIP = %s - City = %s ' % ( row [ 1 ] , row [ 2 ] ) )
if self . letter_case . title :
if self . letter_case == ' title ' :
row [ 2 ] = row [ 2 ] . title ( )
row [ 2 ] = row [ 2 ] . title ( )
row [ 3 ] = row [ 3 ] . title ( )
row [ 3 ] = row [ 3 ] . title ( )
elif self . letter_case . upper :
elif self . letter_case == ' upper ' :
row [ 2 ] = row [ 2 ] . upper ( )
row [ 2 ] = row [ 2 ] . upper ( )
row [ 3 ] = row [ 3 ] . upper ( )
row [ 3 ] = row [ 3 ] . upper ( )
if row [ 1 ] and row [ 2 ] :
if row [ 1 ] and row [ 2 ] :
@ -86,28 +86,30 @@ class BetterZipGeonamesImport(models.TransientModel):
vals = self . _prepare_better_zip ( row , country )
vals = self . _prepare_better_zip ( row , country )
if vals :
if vals :
return zip_model . create ( vals )
return zip_model . create ( vals )
else :
else : # pragma: no cover
return False
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
@api.model
def select_or_create_state (
def select_or_create_state (
self , row , country , code_row_index = 4 , name_row_index = 3 ) :
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
@api.multi
def run_import ( self ) :
def run_import ( self ) :