From a653ec8eb0fe9ffeda39c86c92dceec549962daf Mon Sep 17 00:00:00 2001 From: Hugo Adan Date: Tue, 30 Oct 2018 16:17:06 +0000 Subject: [PATCH] [Add] 11.0 mail_track_diff_only [FIX] includes unknown fields: sequence [REF] Unitests --- mail_track_diff_only/README.rst | 0 mail_track_diff_only/__init__.py | 1 + mail_track_diff_only/__manifest__.py | 14 +++ mail_track_diff_only/models/__init__.py | 1 + mail_track_diff_only/models/mail_thread.py | 32 +++++ mail_track_diff_only/readme/CONTRIBUTORS.rst | 2 + mail_track_diff_only/readme/DESCRIPTION.rst | 2 + mail_track_diff_only/readme/INSTALL.rst | 0 mail_track_diff_only/readme/USAGE.rst | 0 mail_track_diff_only/tests/__init__.py | 1 + .../tests/test_mail_tracking.py | 115 ++++++++++++++++++ 11 files changed, 168 insertions(+) create mode 100644 mail_track_diff_only/README.rst create mode 100644 mail_track_diff_only/__init__.py create mode 100644 mail_track_diff_only/__manifest__.py create mode 100644 mail_track_diff_only/models/__init__.py create mode 100644 mail_track_diff_only/models/mail_thread.py create mode 100644 mail_track_diff_only/readme/CONTRIBUTORS.rst create mode 100644 mail_track_diff_only/readme/DESCRIPTION.rst create mode 100644 mail_track_diff_only/readme/INSTALL.rst create mode 100644 mail_track_diff_only/readme/USAGE.rst create mode 100644 mail_track_diff_only/tests/__init__.py create mode 100644 mail_track_diff_only/tests/test_mail_tracking.py diff --git a/mail_track_diff_only/README.rst b/mail_track_diff_only/README.rst new file mode 100644 index 00000000..e69de29b diff --git a/mail_track_diff_only/__init__.py b/mail_track_diff_only/__init__.py new file mode 100644 index 00000000..0650744f --- /dev/null +++ b/mail_track_diff_only/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/mail_track_diff_only/__manifest__.py b/mail_track_diff_only/__manifest__.py new file mode 100644 index 00000000..6e3e1747 --- /dev/null +++ b/mail_track_diff_only/__manifest__.py @@ -0,0 +1,14 @@ +{ + "name": "Mail track diff only", + "version": "11.0.1.0.0", + "author": "Vauxoo, Odoo Community Association (OCA)", + "category": "Discuss", + "website": "https://github.com/OCA/social/tree/11.0", + "license": "AGPL-3", + "depends": [ + "mail", + ], + "demo": [], + "data": [], + "installable": True, +} diff --git a/mail_track_diff_only/models/__init__.py b/mail_track_diff_only/models/__init__.py new file mode 100644 index 00000000..b70a9f2d --- /dev/null +++ b/mail_track_diff_only/models/__init__.py @@ -0,0 +1 @@ +from . import mail_thread diff --git a/mail_track_diff_only/models/mail_thread.py b/mail_track_diff_only/models/mail_thread.py new file mode 100644 index 00000000..be86ae34 --- /dev/null +++ b/mail_track_diff_only/models/mail_thread.py @@ -0,0 +1,32 @@ +# Copyright 2017-2018 Vauxoo +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). +from odoo import models, api + + +class MailThread(models.AbstractModel): + _inherit = 'mail.thread' + + @api.multi + def _message_track(self, tracked_fields, initial): + """For a given record, fields to check (column name, column info) + and initial values, return a structure that is a tuple containing : + + - a set of updated column names + - a list of changes (old value, new value, column name, column info) + """ + changes = super(MailThread, self)._message_track( + tracked_fields, initial)[0] + tracking_value_ids = [] + track_obj = self.env['mail.tracking.value'] + + for col_name, col_info in tracked_fields.items(): + initial_value = initial[col_name] + new_value = getattr(self, col_name) + + if new_value != initial_value and (new_value or initial_value): + tracking = track_obj.create_tracking_values( + initial_value, new_value, col_name, col_info) + if tracking: + tracking_value_ids.append([0, 0, tracking]) + + return changes, tracking_value_ids diff --git a/mail_track_diff_only/readme/CONTRIBUTORS.rst b/mail_track_diff_only/readme/CONTRIBUTORS.rst new file mode 100644 index 00000000..126362f1 --- /dev/null +++ b/mail_track_diff_only/readme/CONTRIBUTORS.rst @@ -0,0 +1,2 @@ +* José Manuel Robles +* Hugo Adan diff --git a/mail_track_diff_only/readme/DESCRIPTION.rst b/mail_track_diff_only/readme/DESCRIPTION.rst new file mode 100644 index 00000000..8027d067 --- /dev/null +++ b/mail_track_diff_only/readme/DESCRIPTION.rst @@ -0,0 +1,2 @@ +This module shows only the values changed of an object on email, instead of all +tracked values, even if it has track_visibility set as always. diff --git a/mail_track_diff_only/readme/INSTALL.rst b/mail_track_diff_only/readme/INSTALL.rst new file mode 100644 index 00000000..e69de29b diff --git a/mail_track_diff_only/readme/USAGE.rst b/mail_track_diff_only/readme/USAGE.rst new file mode 100644 index 00000000..e69de29b diff --git a/mail_track_diff_only/tests/__init__.py b/mail_track_diff_only/tests/__init__.py new file mode 100644 index 00000000..0f3459bc --- /dev/null +++ b/mail_track_diff_only/tests/__init__.py @@ -0,0 +1 @@ +from . import test_mail_tracking diff --git a/mail_track_diff_only/tests/test_mail_tracking.py b/mail_track_diff_only/tests/test_mail_tracking.py new file mode 100644 index 00000000..c89f10ba --- /dev/null +++ b/mail_track_diff_only/tests/test_mail_tracking.py @@ -0,0 +1,115 @@ +# coding: utf-8 +from odoo import api +from odoo.tests import common + + +class TestTracking(common.TransactionCase): + + def test_message_track(self): + self.user_group_employee = self.env.ref('base.group_user') + users = self.env['res.users'].with_context({ + 'no_reset_password': True, 'mail_create_nosubscribe': True}) + self.user_employee = users.create({ + 'name': 'Ernest Employee', + 'login': 'ernest', + 'email': 'e.e@example.com', + 'signature': '--\nErnest', + 'notification_type': 'email', + 'groups_id': [(6, 0, [self.user_group_employee.id])] + }) + test_channel = self.env['mail.channel'].create({ + 'name': 'Test', + 'channel_partner_ids': [(4, self.user_employee.partner_id.id)] + }) + + subtype = self.env['mail.message.subtype'] + data = self.env['ir.model.data'] + note_subtype = self.env.ref('mail.mt_note') + + # mt_private: public field (tracked as onchange) set to 'private' + # (selection) + mt_private = subtype.create({ + 'name': 'private', + 'description': 'Public field set to private' + }) + data.create({ + 'name': 'mt_private', + 'model': 'mail.message.subtype', + 'module': 'mail', + 'res_id': mt_private.id + }) + + # mt_name_supername: name field (tracked as always) set to 'supername' + # (char) + mt_name_supername = subtype.create({ + 'name': 'name_supername', + 'description': 'Name field set to supername' + }) + data.create({ + 'name': 'mt_name_supername', + 'model': 'mail.message.subtype', + 'module': 'mail', + 'res_id': mt_name_supername.id + }) + + # mt_group_public_set: group_public field (tracked as onchange) set to + # something (m2o) + mt_group_public_set = subtype.create({ + 'name': 'group_public_set', + 'description': 'Group_public field set' + }) + data.create({ + 'name': 'mt_group_public_set', + 'model': 'mail.message.subtype', + 'module': 'mail', + 'res_id': mt_group_public_set.id + }) + + # mt_group_public_set: group_public field (tracked as onchange) set to + # nothing (m2o) + mt_group_public_unset = subtype.create({ + 'name': 'group_public_unset', + 'description': 'Group_public field unset' + }) + data.create({ + 'name': 'mt_group_public_unset', + 'model': 'mail.message.subtype', + 'module': 'mail', + 'res_id': mt_group_public_unset.id + }) + + @api.multi + def _track_subtype(self, init_values): + if 'public' in init_values and self.public == 'private': + return 'mail.mt_private' + elif 'name' in init_values and self.name == 'supername': + return 'mail.mt_name_supername' + elif 'group_public_id' in init_values and self.group_public_id: + return 'mail.mt_group_public_set' + elif 'group_public_id' in init_values and not self.group_public_id: + return 'mail.mt_group_public_unset' + return False + self.registry('mail.channel')._patch_method( + '_track_subtype', _track_subtype) + + visibility = { + 'public': 'onchange', + 'name': 'always', + 'group_public_id': 'onchange' + } + channel = type(self.env['mail.channel']) + for key in visibility: + self.assertFalse( + hasattr(getattr(channel, key), 'track_visibility')) + getattr(channel, key).track_visibility = visibility[key] + + # Test: change name -> always tracked, not related to a subtype + test_channel.sudo(self.user_employee).write({'name': 'my_name'}) + self.assertEqual(len(test_channel.message_ids), 1) + last_msg = test_channel.message_ids[-1] + self.assertEqual(last_msg.subtype_id, note_subtype) + self.assertEqual(len(last_msg.tracking_value_ids), 1) + self.assertEqual(last_msg.tracking_value_ids.field, 'name') + self.assertEqual(last_msg.tracking_value_ids.field_desc, 'Name') + self.assertEqual(last_msg.tracking_value_ids.old_value_char, 'Test') + self.assertEqual(last_msg.tracking_value_ids.new_value_char, 'my_name')