mail_tracking_value.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from datetime import datetime
  4. from odoo import api, fields, models
  5. class MailTracking(models.Model):
  6. _name = 'mail.tracking.value'
  7. _description = 'Mail Tracking Value'
  8. _rec_name = 'field'
  9. _order = 'tracking_sequence asc'
  10. field = fields.Many2one('ir.model.fields', required=True, readonly=1, index=True, ondelete='cascade')
  11. field_desc = fields.Char('Field Description', required=True, readonly=1)
  12. field_type = fields.Char('Field Type')
  13. field_groups = fields.Char(compute='_compute_field_groups')
  14. old_value_integer = fields.Integer('Old Value Integer', readonly=1)
  15. old_value_float = fields.Float('Old Value Float', readonly=1)
  16. old_value_monetary = fields.Float('Old Value Monetary', readonly=1)
  17. old_value_char = fields.Char('Old Value Char', readonly=1)
  18. old_value_text = fields.Text('Old Value Text', readonly=1)
  19. old_value_datetime = fields.Datetime('Old Value DateTime', readonly=1)
  20. new_value_integer = fields.Integer('New Value Integer', readonly=1)
  21. new_value_float = fields.Float('New Value Float', readonly=1)
  22. new_value_monetary = fields.Float('New Value Monetary', readonly=1)
  23. new_value_char = fields.Char('New Value Char', readonly=1)
  24. new_value_text = fields.Text('New Value Text', readonly=1)
  25. new_value_datetime = fields.Datetime('New Value Datetime', readonly=1)
  26. currency_id = fields.Many2one('res.currency', 'Currency', readonly=True, ondelete='set null',
  27. help="Used to display the currency when tracking monetary values")
  28. mail_message_id = fields.Many2one('mail.message', 'Message ID', required=True, index=True, ondelete='cascade')
  29. tracking_sequence = fields.Integer('Tracking field sequence', readonly=1, default=100)
  30. @api.depends('mail_message_id', 'field')
  31. def _compute_field_groups(self):
  32. for tracking in self:
  33. model = self.env[tracking.mail_message_id.model]
  34. field = model._fields.get(tracking.field.name)
  35. tracking.field_groups = field.groups if field else 'base.group_system'
  36. @api.model
  37. def create_tracking_values(self, initial_value, new_value, col_name, col_info, tracking_sequence, model_name):
  38. tracked = True
  39. field = self.env['ir.model.fields']._get(model_name, col_name)
  40. if not field:
  41. return
  42. values = {'field': field.id, 'field_desc': col_info['string'], 'field_type': col_info['type'], 'tracking_sequence': tracking_sequence}
  43. if col_info['type'] in ['integer', 'float', 'char', 'text', 'datetime', 'monetary']:
  44. values.update({
  45. 'old_value_%s' % col_info['type']: initial_value,
  46. 'new_value_%s' % col_info['type']: new_value
  47. })
  48. elif col_info['type'] == 'date':
  49. values.update({
  50. 'old_value_datetime': initial_value and fields.Datetime.to_string(datetime.combine(fields.Date.from_string(initial_value), datetime.min.time())) or False,
  51. 'new_value_datetime': new_value and fields.Datetime.to_string(datetime.combine(fields.Date.from_string(new_value), datetime.min.time())) or False,
  52. })
  53. elif col_info['type'] == 'boolean':
  54. values.update({
  55. 'old_value_integer': initial_value,
  56. 'new_value_integer': new_value
  57. })
  58. elif col_info['type'] == 'selection':
  59. values.update({
  60. 'old_value_char': initial_value and dict(col_info['selection']).get(initial_value, initial_value) or '',
  61. 'new_value_char': new_value and dict(col_info['selection'])[new_value] or ''
  62. })
  63. elif col_info['type'] == 'many2one':
  64. values.update({
  65. 'old_value_integer': initial_value and initial_value.id or 0,
  66. 'new_value_integer': new_value and new_value.id or 0,
  67. 'old_value_char': initial_value and initial_value.sudo().name_get()[0][1] or '',
  68. 'new_value_char': new_value and new_value.sudo().name_get()[0][1] or ''
  69. })
  70. else:
  71. tracked = False
  72. if tracked:
  73. return values
  74. return {}
  75. def _tracking_value_format(self):
  76. tracking_values = [{
  77. 'changedField': tracking.field_desc,
  78. 'id': tracking.id,
  79. 'newValue': {
  80. 'currencyId': tracking.currency_id.id,
  81. 'fieldType': tracking.field_type,
  82. 'value': tracking._get_new_display_value()[0],
  83. },
  84. 'oldValue': {
  85. 'currencyId': tracking.currency_id.id,
  86. 'fieldType': tracking.field_type,
  87. 'value': tracking._get_old_display_value()[0],
  88. },
  89. } for tracking in self]
  90. return tracking_values
  91. def _get_display_value(self, prefix):
  92. assert prefix in ('new', 'old')
  93. result = []
  94. for record in self:
  95. if record.field_type in ['integer', 'float', 'char', 'text', 'monetary']:
  96. result.append(record[f'{prefix}_value_{record.field_type}'])
  97. elif record.field_type == 'datetime':
  98. if record[f'{prefix}_value_datetime']:
  99. new_datetime = record[f'{prefix}_value_datetime']
  100. result.append(f'{new_datetime}Z')
  101. else:
  102. result.append(record[f'{prefix}_value_datetime'])
  103. elif record.field_type == 'date':
  104. if record[f'{prefix}_value_datetime']:
  105. new_date = record[f'{prefix}_value_datetime']
  106. result.append(fields.Date.to_string(new_date))
  107. else:
  108. result.append(record[f'{prefix}_value_datetime'])
  109. elif record.field_type == 'boolean':
  110. result.append(bool(record[f'{prefix}_value_integer']))
  111. else:
  112. result.append(record[f'{prefix}_value_char'])
  113. return result
  114. def _get_old_display_value(self):
  115. # grep : # old_value_integer | old_value_datetime | old_value_char
  116. return self._get_display_value('old')
  117. def _get_new_display_value(self):
  118. # grep : # new_value_integer | new_value_datetime | new_value_char
  119. return self._get_display_value('new')