Odoo modules extending contacts / partners
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
2.0 KiB

import logging
from odoo import models, fields, api, tools
_logger = logging.getLogger(__name__)
class ResPartner(models.Model):
_inherit = "res.partner"
email2 = fields.Char("Email 2")
email2_formatted = fields.Char(
"Formatted Email 2",
compute="_compute_email2_formatted",
help='Format email address "Name <email@domain>"',
)
@api.depends("name", "email2")
def _compute_email2_formatted(self):
"""Compute formatted email for partner, using formataddr. Be defensive
in computation, notably
* double format: if email already holds a formatted email like
'Name' <email@domain.com> we should not use it as it to compute
email formatted like "Name <'Name' <email@domain.com>>";
* multi emails: sometimes this field is used to hold several addresses
like email1@domain.com, email2@domain.com. We currently let this value
untouched, but remove any formatting from multi emails;
* invalid email: if something is wrong, keep it in email2_formatted as
this eases management and understanding of failures at mail.mail,
mail.notification and mailing.trace level;
* void email: email2_formatted is False, as we cannot do anything with
it;
"""
self.email2_formatted = False
for partner in self:
emails_normalized = tools.email_normalize_all(partner.email)
if emails_normalized:
# note: multi-email input leads to invalid email like "Name" <email1, email2>
# but this is current behavior in Odoo 14+ and some servers allow it
partner.email2_formatted = tools.formataddr(
(partner.name or "False", ",".join(emails_normalized))
)
elif partner.email:
partner.email2_formatted = tools.formataddr(
(partner.name or "False", partner.email)
)