Files
odoo_source/addons/mail/models/ir_model_fields.py
T
Lucas Lefèvre 8d2d41068d [IMP] mail: Delete tracking values when uninstalling module
Purpose
=======
mail.tracking.values should be deleted when the corresponding
field is deleted (when the module which defined it is uninstalled).

Firstly because we don't want useless data in db.

Secondly because the groups associated with the field can no longer
be checked if it was deleted. As we don't know to whom the value
was restricted, the value should not be displayed anyway.
Note: this case was fixed in saas-12.2 by b9e96b7 but the proper
way to fix it is to delete the tracking values.

Specification
=============

Delete `mail.tracking.value` when the associated
`ir.model.fields` is deleted.

Two alternatives were considered:

1. Change the `field` field of `mail.tracking.value` from Char
to a m2o to `ir.model.fields` which allows to use delete
oncascade. This implies to modify existing code, but more
importantly it adds database queries.

2. Override the unlink method of `ir.model.fields` to
first unlink associated tracking values.

The first method is probably cleaner but the second method
was nonetheless chosen as we don't want to impact
performance in the main tracking flow only to better
support module uninstalls which happens rarely.

Note: when a module is uninstalled, `ir.model.fields`
are unlinked one by one (in a for loop). Tracking values are
therefore also unlinked field by field. Batchifying field
deletion would greatly reduce the amount of queries.
2019-04-17 17:05:17 +02:00

51 lines
1.8 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import fields, models
class IrModelField(models.Model):
_inherit = 'ir.model.fields'
tracking = fields.Integer(
string="Enable Ordered Tracking",
help="If set every modification done to this field is tracked in the chatter. Value is used to order tracking values.",
)
def _reflect_field_params(self, field):
""" Tracking value can be either a boolean enabling tracking mechanism
on field, either an integer giving the sequence. Default sequence is
set to 100. """
vals = super(IrModelField, self)._reflect_field_params(field)
tracking = getattr(field, 'tracking', None)
if tracking is True:
tracking = 100
elif tracking is False:
tracking = None
vals['tracking'] = tracking
return vals
def _instanciate_attrs(self, field_data):
attrs = super(IrModelField, self)._instanciate_attrs(field_data)
if field_data.get('tracking'):
attrs['tracking'] = field_data['tracking']
return attrs
def unlink(self):
"""
Delete 'mail.tracking.value's when a module is uninstalled
"""
if self:
query = """
DELETE FROM mail_tracking_value
WHERE id IN (
SELECT t.id
FROM mail_tracking_value t
INNER JOIN mail_message m ON (m.id = t.mail_message_id)
INNER JOIN ir_model_fields f ON (t.field = f.name AND m.model = f.model)
WHERE f.id IN %s
);
"""
self.env.cr.execute(query, (tuple(self.ids),))
return super(IrModelField, self).unlink()