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/__init__.py b/addons/mail/tests/__init__.py index 4706daea1c6..15ce69a9bd7 100644 --- a/addons/mail/tests/__init__.py +++ b/addons/mail/tests/__init__.py @@ -2,3 +2,4 @@ from . import common from . import test_res_partner +from . import test_update_notification 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/test_mail/tests/test_update_notification.py b/addons/mail/tests/test_update_notification.py similarity index 100% rename from addons/test_mail/tests/test_update_notification.py rename to addons/mail/tests/test_update_notification.py 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=[ diff --git a/addons/test_mail/data/subtype_data.xml b/addons/test_mail/data/subtype_data.xml index f97e60e72f7..9a91ee77610 100644 --- a/addons/test_mail/data/subtype_data.xml +++ b/addons/test_mail/data/subtype_data.xml @@ -9,26 +9,26 @@ - - Umbrella Changed Subtype - Umbrella Changed - mail.test.full + + Container Changed Subtype + Container Changed + mail.test.ticket - - Umbrella Default Subtype - mail.test + + Container Default Subtype + mail.test.container - - - Umbrella Child Full Subtype - mail.test - - umbrella_id + + + Container Child Full Subtype + mail.test.container + + container_id diff --git a/addons/test_mail/data/template_data.xml b/addons/test_mail/data/template_data.xml index 35bdbedd7a2..4716060d444 100644 --- a/addons/test_mail/data/template_data.xml +++ b/addons/test_mail/data/template_data.xml @@ -1,24 +1,24 @@ - + Mail Test Full: Tracking Template Test Template ${object.customer_id.id | safe}

Hello ${object.name}

- +
- + Mail Test: Template Post on ${object.name} ${object.customer_id.id}

Adding stuff on ${object.name}

- +
-