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.

47 lines
2.0 KiB

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