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

  1. import logging
  2. from odoo import models, fields, api, tools
  3. _logger = logging.getLogger(__name__)
  4. class ResPartner(models.Model):
  5. _inherit = "res.partner"
  6. email2 = fields.Char("Email 2")
  7. email2_formatted = fields.Char(
  8. "Formatted Email 2",
  9. compute="_compute_email2_formatted",
  10. help='Format email address "Name <email@domain>"',
  11. )
  12. @api.depends("name", "email2")
  13. def _compute_email2_formatted(self):
  14. """Compute formatted email for partner, using formataddr. Be defensive
  15. in computation, notably
  16. * double format: if email already holds a formatted email like
  17. 'Name' <email@domain.com> we should not use it as it to compute
  18. email formatted like "Name <'Name' <email@domain.com>>";
  19. * multi emails: sometimes this field is used to hold several addresses
  20. like email1@domain.com, email2@domain.com. We currently let this value
  21. untouched, but remove any formatting from multi emails;
  22. * invalid email: if something is wrong, keep it in email2_formatted as
  23. this eases management and understanding of failures at mail.mail,
  24. mail.notification and mailing.trace level;
  25. * void email: email2_formatted is False, as we cannot do anything with
  26. it;
  27. """
  28. self.email2_formatted = False
  29. for partner in self:
  30. emails_normalized = tools.email_normalize_all(partner.email)
  31. if emails_normalized:
  32. # note: multi-email input leads to invalid email like "Name" <email1, email2>
  33. # but this is current behavior in Odoo 14+ and some servers allow it
  34. partner.email2_formatted = tools.formataddr(
  35. (partner.name or "False", ",".join(emails_normalized))
  36. )
  37. elif partner.email:
  38. partner.email2_formatted = tools.formataddr(
  39. (partner.name or "False", partner.email)
  40. )