summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThibault Delavallée <tde@odoo.com>2020-04-15 09:36:59 +0000
committerThibault Delavallée <tde@odoo.com>2020-04-22 08:40:04 +0000
commit42b5ec03845ecd5b31c0f9202cb4be47329bd240 (patch)
tree555ef62bf4f87cfccdd1b7c39da19230ef8663ab
parent7c3a0daaa1a2ed9c23267874842ca431819e0a10 (diff)
[LNT] mail: lint and reorder mail_notification files
In this commit we order fields more logically to ease reading and prepare future modifications. We also rename the class and some field strings to be more user friendly. LINKS Prepares Task ID 2238597 (clean notification models) Prepares Task ID 2083854 (improve mass mailing technical flows) PR #49891 Co-Authored-By: Rémy Voet <ryv@odoo.com> Co-Authored-By: Thibault Delavallée <tde@odoo.com>
-rw-r--r--addons/mail/models/mail_notification.py70
-rw-r--r--addons/mail/tests/common.py6
-rw-r--r--addons/sms/models/mail_notification.py6
3 files changed, 42 insertions, 40 deletions
diff --git a/addons/mail/models/mail_notification.py b/addons/mail/models/mail_notification.py
index b1dfe410b7c..c4b8161f0e7 100644
--- a/addons/mail/models/mail_notification.py
+++ b/addons/mail/models/mail_notification.py
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
from dateutil.relativedelta import relativedelta
@@ -6,72 +7,73 @@ from odoo import api, fields, models
from odoo.tools.translate import _
-class Notification(models.Model):
+class MailNotification(models.Model):
_name = 'mail.notification'
_table = 'mail_message_res_partner_needaction_rel'
_rec_name = 'res_partner_id'
_log_access = False
_description = 'Message Notifications'
- mail_message_id = fields.Many2one(
- 'mail.message', 'Message', index=True, ondelete='cascade', required=True)
- res_partner_id = fields.Many2one(
- 'res.partner', 'Needaction Recipient', index=True, ondelete='cascade', required=False)
- is_read = fields.Boolean('Is Read', index=True)
+ # origin
+ mail_message_id = fields.Many2one('mail.message', 'Message', index=True, ondelete='cascade', required=True)
+ mail_id = fields.Many2one('mail.mail', 'Mail', index=True, help='Optional mail_mail ID. Used mainly to optimize searches.')
+ # recipient
+ res_partner_id = fields.Many2one('res.partner', 'Recipient', index=True, ondelete='cascade')
+ # status
notification_type = fields.Selection([
- ('inbox', 'Inbox'), ('email', 'Email')], string='Notification Type',
- default='inbox', index=True, required=True)
+ ('inbox', 'Inbox'), ('email', 'Email')
+ ], string='Notification Type', default='inbox', index=True, required=True)
notification_status = fields.Selection([
('ready', 'Ready to Send'),
('sent', 'Sent'),
('bounce', 'Bounced'),
('exception', 'Exception'),
- ('canceled', 'Canceled')], 'Email Status',
- default='ready', index=True)
- mail_id = fields.Many2one('mail.mail', 'Mail', index=True)
- # it would be technically possible to find notification from mail without adding a mail_id field on notification,
- # comparing partner_ids and message_ids, but this will involve to search notifications one by one since we want to match
- # bot value. Working with set inclusion, we could have a notif matching message from mail 1 and partner from mail 2, we dont want that.
- # The solution would be to iterate over mail or to filter mail after search,... or add a mail_id field on notification to KISS
+ ('canceled', 'Canceled')
+ ], string='Status', default='ready', index=True)
+ is_read = fields.Boolean('Is Read', index=True)
+ read_date = fields.Datetime('Read Date', copy=False)
failure_type = fields.Selection(selection=[
- ("SMTP", "Connection failed (outgoing mail server problem)"),
- ("RECIPIENT", "Invalid email address"),
- ("BOUNCE", "Email address rejected by destination"),
- ("UNKNOWN", "Unknown error"),
- ], string='Failure type')
+ ("SMTP", "Connection failed (outgoing mail server problem)"),
+ ("RECIPIENT", "Invalid email address"),
+ ("BOUNCE", "Email address rejected by destination"),
+ ("UNKNOWN", "Unknown error"),
+ ], string='Failure type')
failure_reason = fields.Text('Failure reason', copy=False)
- read_date = fields.Datetime('Read Date', copy=False)
_sql_constraints = [
# email notification;: partner is required
('notification_partner_required',
- "CHECK(notification_type NOT IN ('email', 'inbox') OR res_partner_id IS NOT NULL)",
- 'Customer is required for inbox / email notification'),
+ "CHECK(notification_type NOT IN ('email', 'inbox') OR res_partner_id IS NOT NULL)",
+ 'Customer is required for inbox / email notification'),
]
def init(self):
- self._cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = %s', ('mail_notification_res_partner_id_is_read_notification_status_mail_message_id',))
+ self._cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = %s',
+ ('mail_notification_res_partner_id_is_read_notification_status_mail_message_id',))
if not self._cr.fetchone():
- self._cr.execute('CREATE INDEX mail_notification_res_partner_id_is_read_notification_status_mail_message_id ON mail_message_res_partner_needaction_rel (res_partner_id, is_read, notification_status, mail_message_id)')
-
- def format_failure_reason(self):
- self.ensure_one()
- if self.failure_type != 'UNKNOWN':
- return dict(type(self).failure_type.selection).get(self.failure_type, _('No Error'))
- else:
- return _("Unknown error") + ": %s" % (self.failure_reason or '')
+ self._cr.execute("""
+ CREATE INDEX mail_notification_res_partner_id_is_read_notification_status_mail_message_id
+ ON mail_message_res_partner_needaction_rel (res_partner_id, is_read, notification_status, mail_message_id)
+ """)
@api.model_create_multi
def create(self, vals_list):
for vals in vals_list:
if vals.get('is_read'):
vals['read_date'] = fields.Datetime.now()
- return super(Notification, self).create(vals_list)
+ return super(MailNotification, self).create(vals_list)
def write(self, vals):
if vals.get('is_read'):
vals['read_date'] = fields.Datetime.now()
- return super(Notification, self).write(vals)
+ return super(MailNotification, self).write(vals)
+
+ def format_failure_reason(self):
+ self.ensure_one()
+ if self.failure_type != 'UNKNOWN':
+ return dict(type(self).failure_type.selection).get(self.failure_type, _('No Error'))
+ else:
+ return _("Unknown error") + ": %s" % (self.failure_reason or '')
@api.model
def _gc_notifications(self, max_age_days=180):
diff --git a/addons/mail/tests/common.py b/addons/mail/tests/common.py
index cc5f9b0d67b..366d1a84870 100644
--- a/addons/mail/tests/common.py
+++ b/addons/mail/tests/common.py
@@ -17,7 +17,7 @@ from odoo.addons.base.models.ir_mail_server import IrMailServer, MailDeliveryExc
from odoo.addons.bus.models.bus import ImBus, json_dump
from odoo.addons.mail.models.mail_mail import MailMail
from odoo.addons.mail.models.mail_message import Message
-from odoo.addons.mail.models.mail_notification import Notification
+from odoo.addons.mail.models.mail_notification import MailNotification
from odoo.tests import common, new_test_user
from odoo.tools import formataddr, pycompat
@@ -284,7 +284,7 @@ class MailCase(MockEmail):
@contextmanager
def mock_mail_app(self):
message_create_origin = Message.create
- notification_create_origin = Notification.create
+ notification_create_origin = MailNotification.create
self._init_mock_mail()
def _mail_message_create(model, *args, **kwargs):
@@ -298,7 +298,7 @@ class MailCase(MockEmail):
return res
with patch.object(Message, 'create', autospec=True, wraps=Message, side_effect=_mail_message_create) as _mail_message_create_mock, \
- patch.object(Notification, 'create', autospec=True, wraps=Notification, side_effect=_mail_notification_create) as _mail_notification_create_mock:
+ patch.object(MailNotification, 'create', autospec=True, wraps=MailNotification, side_effect=_mail_notification_create) as _mail_notification_create_mock:
yield
def _init_mock_mail(self):
diff --git a/addons/sms/models/mail_notification.py b/addons/sms/models/mail_notification.py
index 3e00399fcc3..5558527aac2 100644
--- a/addons/sms/models/mail_notification.py
+++ b/addons/sms/models/mail_notification.py
@@ -1,10 +1,10 @@
# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
-from odoo import api, fields, models
-from odoo.tools.translate import _
+from odoo import fields, models
-class Notification(models.Model):
+class MailNotification(models.Model):
_inherit = 'mail.notification'
notification_type = fields.Selection(selection_add=[