Files
odoo_source/addons/mass_mailing/models/ir_model.py
T
Thibault Delavallée ed508ee1bd [REF] mass_mailing: move mailing capability on model
PURPOSE

Remove hardcoded list of models on which mailing is possible. Indeed it is
not modular and not really smart with enterprise code not being reachable
in community.

SPECIFICATIONS

Replace hardcoded list of models on which mailing (both mail or sms) is
possible by a computed searchable field on ``ir.model`` based on a class
attribute.

It allows to cleanly define models having mass mailing capabilities and add
this attribute in bridge modules (when existing) or directly on base model
definition to avoid bridge modules.

In this commit we introduce a basic ``_mailing_enabled`` class attribute
activating mailing on model.

Mailing models may also have a ``_mailing_get_default_domain`` method allowing
to define a custom default domain when sending a marketing mailing on records
on this class.

Mailing models can now define a ``_mailing_get_opt_out_list(_sms)`` method
allowing to define custom behavior to fetch opt-outed records. Instead of
defining a model-based behavior on Mailing itself, it now calls the model
defined one. We still have two methods, one for mailing and one for SMS
opt out computation as it relies on different underlying models and fields.

LINKS

Task ID-2431217
COM PR odoo/odoo#67322
ENT PR odoo/enterprise#16876
UPG PR odoo/upgrade#2236
2021-05-26 07:57:47 +00:00

35 lines
1.3 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import fields, models, _
class IrModel(models.Model):
_inherit = 'ir.model'
is_mailing_enabled = fields.Boolean(
string="Mailing Enabled",
compute='_compute_is_mailing_enabled', search='_search_is_mailing_enabled',
help="Whether this model supports marketing mailing capabilities (notably email and SMS).",
)
def _compute_is_mailing_enabled(self):
for model in self:
model.is_mailing_enabled = getattr(self.env[model.model], '_mailing_enabled', False)
def _search_is_mailing_enabled(self, operator, value):
if operator not in ('=', '!='):
raise ValueError(_("Searching Mailing Enabled models supports only direct search using '='' or '!='."))
valid_models = self.env['ir.model']
for model in self.search([]):
if model.model not in self.env or model.is_transient():
continue
if getattr(self.env[model.model], '_mailing_enabled', False):
valid_models |= model
search_is_mailing_enabled = (operator == '=' and value) or (operator == '!=' and not value)
if search_is_mailing_enabled:
return [('id', 'in', valid_models.ids)]
return [('id', 'not in', valid_models.ids)]