Files
odoo_source/addons/test_mail_full/models/test_mail_models.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

135 lines
4.0 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import fields, models
class MailTestSMS(models.Model):
""" A model inheriting from mail.thread with some fields used for SMS
gateway, like a partner, a specific mobile phone, ... """
_description = 'Chatter Model for SMS Gateway'
_name = 'mail.test.sms'
_inherit = ['mail.thread']
_mailing_enabled = True
_order = 'name asc, id asc'
name = fields.Char()
subject = fields.Char()
email_from = fields.Char()
phone_nbr = fields.Char()
mobile_nbr = fields.Char()
customer_id = fields.Many2one('res.partner', 'Customer')
def _sms_get_partner_fields(self):
return ['customer_id']
def _sms_get_number_fields(self):
return ['phone_nbr', 'mobile_nbr']
class MailTestSMSBL(models.Model):
""" A model inheriting from mail.thread.phone allowing to test auto formatting
of phone numbers, blacklist, ... """
_description = 'SMS Mailing Blacklist Enabled'
_name = 'mail.test.sms.bl'
_inherit = ['mail.thread.phone']
_mailing_enabled = True
_order = 'name asc, id asc'
name = fields.Char()
subject = fields.Char()
email_from = fields.Char()
phone_nbr = fields.Char()
mobile_nbr = fields.Char()
customer_id = fields.Many2one('res.partner', 'Customer')
def _sms_get_partner_fields(self):
return ['customer_id']
def _phone_get_number_fields(self):
return ['phone_nbr', 'mobile_nbr']
class MailTestSMSOptout(models.Model):
""" Model using blacklist mechanism and a hijacked opt-out mechanism for
mass mailing features. """
_description = 'SMS Mailing Blacklist / Optout Enabled'
_name = 'mail.test.sms.bl.optout'
_inherit = ['mail.thread.phone']
_mailing_enabled = True
_order = 'name asc, id asc'
name = fields.Char()
subject = fields.Char()
email_from = fields.Char()
phone_nbr = fields.Char()
mobile_nbr = fields.Char()
customer_id = fields.Many2one('res.partner', 'Customer')
opt_out = fields.Boolean()
def _mailing_get_opt_out_list_sms(self, mailing):
res_ids = mailing._get_recipients()
return self.search([
('id', 'in', res_ids),
('opt_out', '=', True)
]).ids
def _sms_get_partner_fields(self):
return ['customer_id']
def _sms_get_number_fields(self):
# TDE note: should override _phone_get_number_fields but ok as sms in dependencies
return ['phone_nbr', 'mobile_nbr']
class MailTestSMSPartner(models.Model):
""" A model like sale order having only a customer, not specific phone
or mobile fields. """
_description = 'Chatter Model for SMS Gateway (Partner only)'
_name = 'mail.test.sms.partner'
_inherit = ['mail.thread']
_mailing_enabled = True
name = fields.Char()
customer_id = fields.Many2one('res.partner', 'Customer')
opt_out = fields.Boolean()
def _mailing_get_opt_out_list_sms(self, mailing):
res_ids = mailing._get_recipients()
return self.search([
('id', 'in', res_ids),
('opt_out', '=', True)
]).ids
def _sms_get_partner_fields(self):
return ['customer_id']
def _sms_get_number_fields(self):
return []
class MailTestSMSPartner2Many(models.Model):
""" A model like sale order having only a customer, not specific phone
or mobile fields. """
_description = 'Chatter Model for SMS Gateway (M2M Partners only)'
_name = 'mail.test.sms.partner.2many'
_inherit = ['mail.thread']
_mailing_enabled = True
name = fields.Char()
customer_ids = fields.Many2many('res.partner', string='Customers')
opt_out = fields.Boolean()
def _mailing_get_opt_out_list_sms(self, mailing):
res_ids = mailing._get_recipients()
return self.search([
('id', 'in', res_ids),
('opt_out', '=', True)
]).ids
def _sms_get_partner_fields(self):
return ['customer_ids']
def _sms_get_number_fields(self):
return []