[IMP] Model emulator: better support method calls

Since we have the model fields loaded up, we can just check into that
and assume anything that's not a field is a method.

That avoids having to go through `_call`, making things way less awkward.
This commit is contained in:
Xavier Morel 2021-10-06 13:03:03 +02:00
parent c036c7a28f
commit d32ca9a1b3
2 changed files with 7 additions and 6 deletions

View File

@ -1120,17 +1120,18 @@ class Model:
def __getattr__(self, fieldname):
if fieldname in ['__dataclass_fields__', '__attrs_attrs__']:
raise AttributeError('%r is invalid on %s' % (fieldname, self._model))
field_description = self._fields.get(fieldname)
if field_description is None:
return functools.partial(self._call, fieldname)
if not self._ids:
return False
assert len(self._ids) == 1
if fieldname == 'id':
return self._ids[0]
try:
val = self.read([fieldname])[0][fieldname]
except Exception:
raise AttributeError('%r is invalid on %s' % (fieldname, self._model))
val = self.read([fieldname])[0][fieldname]
field_description = self._fields[fieldname]
if field_description['type'] in ('many2one', 'one2many', 'many2many'):
val = val or []

View File

@ -19,7 +19,7 @@ def test_partner_merge(env):
'state': 'selection',
'partner_ids': (p_src + p_dest).ids,
'dst_partner_id': p_dest.id,
})._call('action_merge')
}).action_merge()
assert not p_src.exists()
assert p_dest.name == 'Partner P. Partnersson'
assert p_dest.github_login == 'xxx'