From c0450feaa3fb19dadb15d9f04212831bb7703f07 Mon Sep 17 00:00:00 2001 From: "Antoine Dupuis (andu)" Date: Mon, 18 Dec 2023 14:20:43 +0100 Subject: [PATCH] [REF] l10n_ec: Refactor _l10n_ec_get_identification_type _l10n_ec_get_identification_type will get called quite often in a loop in the ATS report, so it's important that it gets orm-cached. This reimplementation removes calls to ref() and replaces them with calls to _xmlid_to_res_model_res_id() which is entirely orm-cached. closes odoo/odoo#147723 X-original-commit: f95540bb8b92e795b1331910049d0d6de7da120c Signed-off-by: Josse Colpaert Signed-off-by: Antoine Dupuis (andu) --- addons/l10n_ec/models/res_partner.py | 31 ++++++++++++++++++---------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/addons/l10n_ec/models/res_partner.py b/addons/l10n_ec/models/res_partner.py index 5441fad26f8..048cccb4596 100644 --- a/addons/l10n_ec/models/res_partner.py +++ b/addons/l10n_ec/models/res_partner.py @@ -101,15 +101,24 @@ class ResPartner(models.Model): """ self.ensure_one() - def id_type_in(*args): - return any([self.l10n_latam_identification_type_id == self.env.ref(arg) for arg in args]) + id_types_by_xmlid = { + 'l10n_ec.ec_dni': 'cedula', # DNI + 'l10n_ec.ec_ruc': 'ruc', # RUC + 'l10n_ec.ec_passport': 'ec_passport', # EC passport + 'l10n_latam_base.it_pass': 'passport', # Passport + 'l10n_latam_base.it_fid': 'foreign', # Foreign ID + 'l10n_latam_base.it_vat': 'foreign', + } - if id_type_in('l10n_ec.ec_dni'): - return 'cedula' # DNI - elif id_type_in('l10n_ec.ec_ruc'): - return 'ruc' # RUC - elif id_type_in('l10n_latam_base.it_pass'): - return 'passport' # Pasaporte - elif id_type_in('l10n_latam_base.it_fid', 'l10n_latam_base.it_vat') \ - or self.l10n_latam_identification_type_id.country_id != self.env.ref('base.ec'): - return 'foreign' # Identificacion del exterior + # This method is orm-cached, which makes it more efficient in loops than get_external_id() + xmlid_by_res_id = { + self.env['ir.model.data']._xmlid_to_res_model_res_id(xmlid, raise_if_not_found=True)[1]: xmlid + for xmlid in id_types_by_xmlid + } + + id_type_xmlid = xmlid_by_res_id.get(self.l10n_latam_identification_type_id.id) + if id_type_xmlid in id_types_by_xmlid: + return id_types_by_xmlid[id_type_xmlid] + + if self.l10n_latam_identification_type_id.country_id.code != 'EC': + return 'foreign'