[FIX] mail: Check field exists when computing groups

Steps to reproduce:

1. install `hr_contract_salary`

2. create a contract

3. set or update `hr_responsible_id` on the contract
(it creates a `mail.tracking.value`)

4. uninstall `hr_contract_salary`

5. go to the contract form view
=> traceback `hr_responsible_id` does not exist

Before displaying a `mail.tracking.value`, `groups` of the
fields are checked to fitler tracking values according to access
rights of the user.
However if the module that added the field has been uninstalled,
the field does not exists anymore.

Fix:
check if field exists before getting fields groups.
If the field does not exists: set groups to `base.group_system`

closes odoo/odoo#31398

Signed-off-by: "Yannick Tivisse (yti)" <yti@odoo.com>
This commit is contained in:
Lucas Lefèvre
2019-03-05 08:55:02 +00:00
committed by Yannick Tivisse
parent d31f3ebade
commit 120b8768bb
2 changed files with 9 additions and 1 deletions
+3 -1
View File
@@ -40,7 +40,9 @@ class MailTracking(models.Model):
def _compute_groups(self):
for tracking in self:
model = self.env[tracking.mail_message_id.model]
tracking.groups = model._fields[tracking.field].groups
field = model._fields.get(tracking.field)
# field may not exist anymore if module has been uninstalled
tracking.groups = field.groups if field else 'base.group_system'
@api.model
def create_tracking_values(self, initial_value, new_value, col_name, col_info, tracking_sequence):
@@ -149,3 +149,9 @@ class TestTracking(common.BaseFunctionalTest, common.MockEmails):
msg_admin = self.partner_admin.sudo(self.user_admin)._notify_prepare_template_context(self.record.message_ids, self.record)
self.assertFalse(msg_emp.get('tracking_values'), "should not have protected tracking values")
self.assertTrue(msg_admin.get('tracking_values'), "should have protected tracking values")
def test_unknown_field(self):
self.record.sudo().write({'email_from': 'X'}) # create a tracking value
tracking = self.record.sudo().mapped('message_ids.tracking_value_ids')[0]
tracking.field = 'I do not exist'
self.assertEqual(tracking.groups, 'base.group_system')