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.

39 lines
1.5 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright - 2013-2018 Therp BV <https://therp.nl>.
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from odoo.tools.safe_eval import safe_eval
  5. from odoo.tools.mail import email_split
  6. from .base import Base
  7. class EmailExact(Base):
  8. """Search for exactly the mailadress as noted in the email"""
  9. name = 'Exact mailadress'
  10. required_fields = ['model_field', 'mail_field']
  11. def _get_mailaddresses(self, folder, mail_message):
  12. mailaddresses = []
  13. fields = folder.mail_field.split(',')
  14. for field in fields:
  15. if field in mail_message:
  16. mailaddresses += email_split(mail_message[field])
  17. return [addr.lower() for addr in mailaddresses]
  18. def _get_mailaddress_search_domain(
  19. self, folder, mail_message, operator='=', values=None):
  20. mailaddresses = values or self._get_mailaddresses(
  21. folder, mail_message)
  22. if not mailaddresses:
  23. return [(0, '=', 1)]
  24. search_domain = ((['|'] * (len(mailaddresses) - 1)) + [
  25. (folder.model_field, operator, addr) for addr in mailaddresses] +
  26. safe_eval(folder.domain or '[]'))
  27. return search_domain
  28. def search_matches(self, folder, mail_message):
  29. """Returns recordset of matching objects."""
  30. object_model = folder.env[folder.model_id.model]
  31. search_domain = self._get_mailaddress_search_domain(
  32. folder, mail_message)
  33. return object_model.search(search_domain, order=folder.model_order)