|
|
"""
Computes duration_tracking, a Json field stored as { <many2one_id (str)>: <duration_spent_in_seconds (int)> }
e.g. {"1": 1230, "2": 2220, "5": 14}
`_track_duration_field` must be present in the model that uses the mixin to specify on what
field to compute time spent. Besides, tracking must be activated for that field.
e.g.
class MyModel(models.Model):
_track_duration_field = "tracked_field"
tracked_field = fields.Many2one('tracked.model', tracking=True)
"""
field = self.env['ir.model.fields'].sudo().search_fetch([
('model', '=', self._name),
('name', '=', self._track_duration_field),
], ['id'], limit=1)
if (
self._track_duration_field not in self._track_get_fields()
or self._fields[self._track_duration_field].type != 'many2one'
):
self.duration_tracking = False
raise ValueError(_(
'Field ā%(field)sā on model ā%(model)sā must be of type Many2one and have tracking=True for the computation of duration.',
field=self._track_duration_field, model=self._name
))
self.env['mail.tracking.value'].flush_model()
self.env['mail.message'].flush_model()
query = """
SELECT m.res_id,
v.create_date,
v.old_value_integer
FROM mail_tracking_value v
LEFT JOIN mail_message m
ON m.id = v.mail_message_id
AND v.field_id = %(field_id)s
WHERE m.model = %(model_name)s
AND m.res_id IN %(record_ids)s
ORDER BY v.id
"""
self.env.cr.execute(query, {"field_id": field.id, "model_name": self._name, "record_ids": tuple(self.ids)})
trackings = self.env.cr.dictfetchall()
for record in self:
record_trackings = [tracking for tracking in trackings if tracking['res_id'] == record._origin.id]
record.duration_tracking = record._get_duration_from_tracking(record_trackings)
|
|