11# Part of OpenSPP. See LICENSE file for full copyright and licensing details.
22import logging
3+ import threading
34
45from odoo import api , models
56
67_logger = logging .getLogger (__name__ )
78
9+ # Thread-local storage to pass import match counts from load() to execute_import()
10+ _import_match_local = threading .local ()
11+
812
913class Base (models .AbstractModel ):
1014 _inherit = "base"
@@ -16,14 +20,13 @@ def load(self, fields, data):
1620 fields ,
1721 option_config_ids = self .env .context .get ("import_match_ids" , []),
1822 )
19- model_id = self .env ["ir.model" ].search ([("model" , "=" , self ._name )])
20- overwrite_match = True
21- import_match = self .env ["spp.import.match" ].search ([("model_id" , "=" , model_id .id )])
22- if import_match :
23- overwrite_match = import_match .overwrite_match
23+ overwrite_match = self .env .context .get ("overwrite_match" , False )
2424
2525 if usable :
2626 newdata = list ()
27+ match_created = 0
28+ match_skipped = 0
29+ match_overwritten = 0
2730 if ".id" in fields :
2831 column = fields .index (".id" )
2932 fields [column ] = "id"
@@ -70,6 +73,7 @@ def load(self, fields, data):
7073 row ["id" ] = ext_id [match .id ] if match else row .get ("id" , "" )
7174 if match :
7275 if overwrite_match :
76+ match_overwritten += 1
7377 flat_fields_to_remove = [item for sublist in field_to_match for item in sublist ]
7478 for fields_pop in flat_fields_to_remove :
7579 # Set one2many and many2many fields to False if matched
@@ -80,19 +84,25 @@ def load(self, fields, data):
8084 ]:
8185 row [fields_pop ] = False
8286 newdata .append (tuple (row [f ] for f in clean_fields ))
87+ else :
88+ match_skipped += 1
8389 else :
90+ match_created += 1
8491 newdata .append (tuple (row [f ] for f in fields ))
8592 data = newdata
93+ if self .env .context .get ("import_match_ids" ):
94+ _import_match_local .counts = {
95+ "created" : match_created ,
96+ "skipped" : match_skipped ,
97+ "overwritten" : match_overwritten ,
98+ }
8699 return super ().load (fields , data )
87100
88101 def write (self , vals ):
89- # nosemgrep: odoo-sudo-without-context - reading model metadata requires sudo
90- model = self .env ["ir.model" ].sudo ().search ([("model" , "=" , self ._name )])
91102 new_vals = vals .copy ()
92- for rec in vals :
93- field_name = rec
94- if not vals [field_name ]:
95- field = self .env ["ir.model.fields" ].search ([("model_id" , "=" , model .id ), ("name" , "=" , field_name )])
96- if field and field .ttype in ("one2many" , "many2many" ):
97- new_vals .pop (rec )
103+ for field_name , value in vals .items ():
104+ if not value and field_name in self ._fields :
105+ field_meta = self ._fields [field_name ]
106+ if field_meta .type in ("one2many" , "many2many" ):
107+ new_vals .pop (field_name )
98108 return super ().write (new_vals )
0 commit comments