From 741a31092c5cc8d16d92d6653fc9f4ebc3743a0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= <100228798+clementcharmillot@users.noreply.github.com> Date: Thu, 8 Aug 2024 11:00:32 +0200 Subject: [PATCH] T1567 wordpress import (#32) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: add call to parent on exception * chore: format --------- Co-authored-by: Clément --- .../models/res_partner_match.py | 44 ++++++++++++------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/website_sponsorship/models/res_partner_match.py b/website_sponsorship/models/res_partner_match.py index 7c1b595..affda3c 100644 --- a/website_sponsorship/models/res_partner_match.py +++ b/website_sponsorship/models/res_partner_match.py @@ -16,23 +16,33 @@ def _get_valid_create_fields(self): def _match_email_and_name(self, vals): # Replace the rule with fuzzy search and using firstname and lastname - email = vals["email"].strip() - name = vals["firstname"] + " " + vals["lastname"] - return self.env["res.partner"].search( - [ - ("name", "%", name), - ("email", "=ilike", email), - ], - limit=1, - ) + try: + email = vals["email"].strip() + name = vals["firstname"] + " " + vals["lastname"] + return self.env["res.partner"].search( + [ + ("name", "%", name), + ("email", "=ilike", email), + ], + limit=1, + ) + except KeyError: + # No "firstname" or "lastname", the caller probably expected the initial + # behavior of the parent with "name" + return super()._match_email_and_name(vals) def _match_name_and_zip(self, vals): # Replace the rule for using firstname and lastname - name = vals["firstname"] + " " + vals["lastname"] - return self.env["res.partner"].search( - [ - ("name", "ilike", name), - ("zip", "=", vals["zip"]), - ], - limit=1, - ) + try: + name = vals["firstname"] + " " + vals["lastname"] + return self.env["res.partner"].search( + [ + ("name", "ilike", name), + ("zip", "=", vals["zip"]), + ], + limit=1, + ) + except KeyError: + # No "firstname" or "lastname", the caller probably expected the initial + # behavior of the parent with "name" + return super()._match_name_and_zip(vals)