From b358c3debb378d5152a6abd24e28c477045e6c9c Mon Sep 17 00:00:00 2001 From: anhe-odoo Date: Tue, 7 Dec 2021 15:02:50 +0000 Subject: [PATCH] [FIX] mail: hide 'View' button in mail Expected Behaviour When a user get a mail from the data cleaning about records to examine, the View button should be linked to the same url as the 'here' hyperlink or not be in the e-mail. Observed behaviour Clicking the link in the View button lead to an error page Reproducibility This bug can be reproduced following these steps: 1. Duplicate a contact 2. Wait for the scheduled task to launch 3. Click on the View button of the mail you'll get about te duplicate Problem Root Cause This issue is coming from the way we create the 'View' and 'View Task' button, and can't be changed easily. The easier way to fix this issue is then to hide these button Related Issues/PR opw-2674138 closes odoo/odoo#83284 X-original-commit: 3a61747b366a315488837e2e560b305e89217dbd Signed-off-by: Hendrickx Anthony (anhe) Signed-off-by: Thibault Delavallee (tde) --- addons/mail/models/mail_thread.py | 15 +++++++-- addons/test_mail/tests/test_message_post.py | 34 +++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/addons/mail/models/mail_thread.py b/addons/mail/models/mail_thread.py index 59c72e55bf5..2b3ba2337ea 100644 --- a/addons/mail/models/mail_thread.py +++ b/addons/mail/models/mail_thread.py @@ -2558,11 +2558,12 @@ class MailThread(models.AbstractModel): Groups has a default value that you can find in mail_thread ``_notify_get_recipients_classify`` method. """ + is_thread_notification = self._notify_get_recipients_thread_info(msg_vals=msg_vals)['is_thread_notification'] return [ [ 'user', lambda pdata: pdata['type'] == 'user', - {'has_button_access': True} + {'has_button_access': is_thread_notification} ], [ 'portal', lambda pdata: pdata['type'] == 'portal', @@ -2624,9 +2625,10 @@ class MailThread(models.AbstractModel): # fill group_data with default_values if they are not complete for group_name, group_func, group_data in groups: + is_thread_notification = self._notify_get_recipients_thread_info(msg_vals=msg_vals)['is_thread_notification'] group_data.setdefault('active', True) group_data.setdefault('actions', list()) - group_data.setdefault('has_button_access', True) + group_data.setdefault('has_button_access', is_thread_notification) group_data.setdefault('notification_is_customer', False) group_data.setdefault('notification_group_name', group_name) group_data.setdefault('recipients', list()) @@ -2645,6 +2647,15 @@ class MailThread(models.AbstractModel): return [group_data for _group_name, _group_func, group_data in groups if group_data['recipients']] + def _notify_get_recipients_thread_info(self, msg_vals=None): + """ Tool method to compute thread info used in ``_notify_classify_recipients`` + and its sub-methods. """ + res_model = msg_vals['model'] if msg_vals and 'model' in msg_vals else self._name + res_id = msg_vals['res_id'] if msg_vals and 'res_id' in msg_vals else self.ids[0] if self.ids else False + return { + 'is_thread_notification': res_model and (res_model != 'mail.thread') and res_id + } + @api.model def _notify_encode_link(self, base_link, params): secret = self.env['ir.config_parameter'].sudo().get_param('database.secret') diff --git a/addons/test_mail/tests/test_message_post.py b/addons/test_mail/tests/test_message_post.py index c95502b5ef5..98a25c61c95 100644 --- a/addons/test_mail/tests/test_message_post.py +++ b/addons/test_mail/tests/test_message_post.py @@ -177,6 +177,40 @@ class TestMessagePost(TestMailCommon, TestRecipients): self.assertNotIn('body', emp_info['button_access']['url']) self.assertNotIn('subject', emp_info['button_access']['url']) + # test when notifying on non-records (e.g. MailThread._message_notify()) + for model, res_id in ((self.test_record._name, False), + (self.test_record._name, 0), # browse(0) does not return a valid recordset + (False, self.test_record.id), + (False, False), + ('mail.thread', False), + ('mail.thread', self.test_record.id)): + msg_vals.update({ + 'model': model, + 'res_id': res_id, + }) + # note that msg_vals wins over record on which method is called + notify_msg_vals = dict(msg_vals, **link_vals) + classify_res = self.test_record._notify_get_recipients_classify( + pdata, 'Test', msg_vals=notify_msg_vals) + # find back information for partner + partner_info = next(item for item in classify_res if item['recipients'] == self.partner_1.ids) + emp_info = next(item for item in classify_res if item['recipients'] == self.partner_employee.ids) + # check there is no access button + self.assertFalse(partner_info['has_button_access']) + self.assertFalse(emp_info['has_button_access']) + + # test on falsy records (False model cannot be browsed, skipped) + if model: + record_falsy = self.env[model].browse(res_id) + classify_res = record_falsy._notify_get_recipients_classify( + pdata, 'Test', msg_vals=notify_msg_vals) + # find back information for partner + partner_info = next(item for item in classify_res if item['recipients'] == self.partner_1.ids) + emp_info = next(item for item in classify_res if item['recipients'] == self.partner_employee.ids) + # check there is no access button + self.assertFalse(partner_info['has_button_access']) + self.assertFalse(emp_info['has_button_access']) + @mute_logger('odoo.addons.mail.models.mail_mail') def test_post_needaction(self): (self.user_employee | self.user_admin).write({'notification_type': 'inbox'})