ir_model.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import fields, models, _
  4. class IrModel(models.Model):
  5. _inherit = 'ir.model'
  6. is_mailing_enabled = fields.Boolean(
  7. string="Mailing Enabled",
  8. compute='_compute_is_mailing_enabled', search='_search_is_mailing_enabled',
  9. help="Whether this model supports marketing mailing capabilities (notably email and SMS).",
  10. )
  11. def _compute_is_mailing_enabled(self):
  12. for model in self:
  13. model.is_mailing_enabled = getattr(self.env[model.model], '_mailing_enabled', False)
  14. def _search_is_mailing_enabled(self, operator, value):
  15. if operator not in ('=', '!='):
  16. raise ValueError(_("Searching Mailing Enabled models supports only direct search using '='' or '!='."))
  17. valid_models = self.env['ir.model']
  18. for model in self.search([]):
  19. if model.model not in self.env or model.is_transient():
  20. continue
  21. if getattr(self.env[model.model], '_mailing_enabled', False):
  22. valid_models |= model
  23. search_is_mailing_enabled = (operator == '=' and value) or (operator == '!=' and not value)
  24. if search_is_mailing_enabled:
  25. return [('id', 'in', valid_models.ids)]
  26. return [('id', 'not in', valid_models.ids)]