3232class BaseImportImport (models .TransientModel ):
3333 _inherit = "base_import.import"
3434
35- def do (self , fields , columns , options , dryrun = False ):
35+ def execute_import (self , fields , columns , options , dryrun = False ):
3636 if dryrun or not options .get (OPT_USE_QUEUE ):
3737 # normal import
38- return super ().do (fields , columns , options , dryrun = dryrun )
38+ return super ().execute_import (fields , columns , options , dryrun = dryrun )
3939
4040 # asynchronous import
4141 try :
@@ -52,19 +52,23 @@ def do(self, fields, columns, options, dryrun=False):
5252 translated_model_name = search_result [0 ][1 ]
5353 else :
5454 translated_model_name = self ._description
55- description = _ ("Import %s from file %s" ) % (
56- translated_model_name ,
57- self .file_name ,
55+ description = _ (
56+ "Import %(translated_model_name)s from file %(file_name)s" ,
57+ translated_model_name = translated_model_name ,
58+ file_name = self .file_name ,
5859 )
60+ file_name = self .file_name
61+ if not file_name .endswith (".csv" ):
62+ file_name += ".csv"
5963 attachment = self ._create_csv_attachment (
60- import_fields , data , options , self . file_name
64+ import_fields , data , options , file_name
6165 )
6266 delayed_job = self .with_delay (description = description )._split_file (
6367 model_name = self .res_model ,
6468 translated_model_name = translated_model_name ,
6569 attachment = attachment ,
6670 options = options ,
67- file_name = self . file_name ,
71+ file_name = file_name ,
6872 )
6973 self ._link_attachment_to_job (delayed_job , attachment )
7074 return []
@@ -91,7 +95,12 @@ def _create_csv_attachment(self, fields, data, options, file_name):
9195 # create attachment
9296 datas = base64 .encodebytes (f .getvalue ().encode (encoding ))
9397 attachment = self .env ["ir.attachment" ].create (
94- {"name" : file_name , "datas" : datas }
98+ {
99+ "name" : file_name ,
100+ "datas" : datas ,
101+ "type" : "binary" ,
102+ "mimetype" : "text/csv" ,
103+ }
95104 )
96105 return attachment
97106
@@ -130,7 +139,7 @@ def _split_file(
130139 options ,
131140 file_name = "file.csv" ,
132141 ):
133- """ Split a CSV attachment in smaller import jobs """
142+ """Split a CSV attachment in smaller import jobs"""
134143 model_obj = self .env [model_name ]
135144 fields , data = self ._read_csv_attachment (attachment , options )
136145 padding = len (str (len (data )))
@@ -144,13 +153,14 @@ def _split_file(
144153 model_obj , fields , data , chunk_size
145154 ):
146155 chunk = str (priority - INIT_PRIORITY ).zfill (padding )
147- description = _ ("Import %s from file %s - #%s - lines %s to %s" )
148- description = description % (
149- translated_model_name ,
150- file_name ,
151- chunk ,
152- row_from + 1 + header_offset ,
153- row_to + 1 + header_offset ,
156+ description = _ (
157+ "Import %(translated_model_name)s from file %(file_name)s"
158+ " - #%(chunk)s - lines %(row_from)s to %(row_to)s" ,
159+ translated_model_name = translated_model_name ,
160+ file_name = file_name ,
161+ chunk = chunk ,
162+ row_from = row_from + 1 + header_offset ,
163+ row_to = row_to + 1 + header_offset ,
154164 )
155165 # create a CSV attachment and enqueue the job
156166 root , ext = splitext (file_name )
0 commit comments