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.

99 lines
3.2 KiB

  1. # Copyright 2019 Komit <https://komit-consulting.com>
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  3. import logging
  4. from odoo import _, api, models
  5. from odoo.exceptions import UserError, ValidationError
  6. _logger = logging.getLogger(__name__)
  7. try:
  8. from email_validator import (
  9. EmailSyntaxError,
  10. EmailUndeliverableError,
  11. validate_email,
  12. )
  13. except ImportError:
  14. _logger.debug('Cannot import "email_validator".')
  15. validate_email = None
  16. class ResPartner(models.Model):
  17. _inherit = "res.partner"
  18. def copy_data(self, default=None):
  19. res = super(ResPartner, self).copy_data(default=default)
  20. if self._should_filter_duplicates():
  21. for copy_vals in res:
  22. copy_vals.pop("email", None)
  23. return res
  24. @api.model
  25. def email_check(self, emails):
  26. return ",".join(
  27. self._normalize_email(email.strip())
  28. for email in emails.split(",")
  29. if email.strip()
  30. )
  31. @api.constrains("email")
  32. def _check_email_unique(self):
  33. if self._should_filter_duplicates():
  34. for rec in self.filtered("email"):
  35. if "," in rec.email:
  36. raise UserError(
  37. _(
  38. "Field contains multiple email addresses. This is "
  39. "not supported when duplicate email addresses are "
  40. "not allowed."
  41. )
  42. )
  43. if self.search_count([("email", "=", rec.email), ("id", "!=", rec.id)]):
  44. raise UserError(
  45. _("Email '%s' is already in use.") % rec.email.strip()
  46. )
  47. def _normalize_email(self, email):
  48. if not self._should_check_syntax():
  49. return email
  50. if validate_email is None:
  51. _logger.warning(
  52. "Can not validate email, "
  53. 'python dependency required "email_validator"'
  54. )
  55. return email
  56. try:
  57. result = validate_email(
  58. email,
  59. check_deliverability=self._should_check_deliverability(),
  60. )
  61. except EmailSyntaxError:
  62. raise ValidationError(_("%s is an invalid email") % email.strip())
  63. except EmailUndeliverableError:
  64. raise ValidationError(
  65. _("Cannot deliver to email address %s") % email.strip()
  66. )
  67. return result["local"].lower() + "@" + result["domain_i18n"]
  68. def _should_check_syntax(self):
  69. return self.env.company.partner_email_check_syntax
  70. def _should_filter_duplicates(self):
  71. return self.env.company.partner_email_check_filter_duplicates
  72. def _should_check_deliverability(self):
  73. return self.env.company.partner_email_check_check_deliverability
  74. @api.model
  75. def create(self, vals):
  76. if vals.get("email"):
  77. vals["email"] = self.email_check(vals["email"])
  78. return super(ResPartner, self).create(vals)
  79. def write(self, vals):
  80. if vals.get("email"):
  81. vals["email"] = self.email_check(vals["email"])
  82. return super(ResPartner, self).write(vals)