From 42b5ec03845ecd5b31c0f9202cb4be47329bd240 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Wed, 15 Apr 2020 09:36:59 +0000 Subject: [PATCH] [LNT] mail: lint and reorder mail_notification files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Co-Authored-By: Thibault Delavallée --- addons/mail/models/mail_notification.py | 78 +++++++++++++------------ addons/mail/tests/common.py | 6 +- addons/sms/models/mail_notification.py | 6 +- 3 files changed, 46 insertions(+), 44 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,53 +7,66 @@ 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 - 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') - failure_reason = fields.Text('Failure reason', copy=False) + ('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') + failure_reason = fields.Text('Failure reason', 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)') + 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(MailNotification, self).create(vals_list) + + def write(self, vals): + if vals.get('is_read'): + vals['read_date'] = fields.Datetime.now() + return super(MailNotification, self).write(vals) def format_failure_reason(self): self.ensure_one() @@ -61,18 +75,6 @@ class Notification(models.Model): else: return _("Unknown error") + ": %s" % (self.failure_reason or '') - @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) - - def write(self, vals): - if vals.get('is_read'): - vals['read_date'] = fields.Datetime.now() - return super(Notification, self).write(vals) - @api.model def _gc_notifications(self, max_age_days=180): domain = [ 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=[