[MOV] core,base: move logic of O2MIdMapper

O2MIdMapper Class inherit of 'base' Model to change the behavior of
`create`.
Because it is in base and the `_import_current_module` is set in
models.py, move it at the end of create in models.py.

closes odoo/odoo#99550

Related: odoo/enterprise#33063
Signed-off-by: Vincent Schippefilt (vsc) <vsc@odoo.com>
This commit is contained in:
Rémy Voet (ryv)
2022-11-02 15:34:30 +01:00
parent 3353dfdb29
commit f66fa9433f
2 changed files with 24 additions and 36 deletions
-34
View File
@@ -640,37 +640,3 @@ class IrFieldsConverter(models.AbstractModel):
commands.append(Command.create(writable))
return commands, warnings
class O2MIdMapper(models.AbstractModel):
"""
Updates the base class to support setting xids directly in create by
providing an "id" key (otherwise stripped by create) during an import
(which should strip 'id' from the input data anyway)
"""
_inherit = 'base'
# sadly _load_records_create is only called for the toplevel record so we
# can't hook into that
@api.model_create_multi
@api.returns('self', lambda value: value.id)
def create(self, vals_list):
recs = super().create(vals_list)
import_module = self.env.context.get('_import_current_module')
if not import_module: # not an import -> bail
return recs
noupdate = self.env.context.get('noupdate', False)
xids = (v.get('id') for v in vals_list)
self.env['ir.model.data']._update_xmlids([
{
'xml_id': xid if '.' in xid else ('%s.%s' % (import_module, xid)),
'record': rec,
# note: this is not used when updating o2ms above...
'noupdate': noupdate,
}
for rec, xid in zip(recs, xids)
if xid and isinstance(xid, str)
])
return recs
+24 -2
View File
@@ -3856,13 +3856,13 @@ class BaseModel(metaclass=MetaModel):
self = self.browse()
self.check_access_rights('create')
vals_list = self._prepare_create_values(vals_list)
new_vals_list = self._prepare_create_values(vals_list)
# classify fields for each record
data_list = []
determine_inverses = defaultdict(set) # {inverse: fields}
for vals in vals_list:
for vals in new_vals_list:
precomputed = vals.pop('__precomputed__', ())
# distribute fields into sets for various purposes
@@ -3957,6 +3957,28 @@ class BaseModel(metaclass=MetaModel):
if self._check_company_auto:
records._check_company()
import_module = self.env.context.get('_import_current_module')
if not import_module: # not an import -> bail
return records
# It is to support setting xids directly in create by
# providing an "id" key (otherwise stripped by create) during an import
# (which should strip 'id' from the input data anyway)
noupdate = self.env.context.get('noupdate', False)
xids = (v.get('id') for v in vals_list)
self.env['ir.model.data']._update_xmlids([
{
'xml_id': xid if '.' in xid else ('%s.%s' % (import_module, xid)),
'record': rec,
# note: this is not used when updating o2ms above...
'noupdate': noupdate,
}
for rec, xid in zip(records, xids)
if xid and isinstance(xid, str)
])
return records
def _prepare_create_values(self, vals_list):