You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
3.5 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 Camptocamp SA
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. import json
  5. from odoo import api, fields, models
  6. class MailTrackingValue(models.Model):
  7. _inherit = "mail.tracking.value"
  8. new_value_formatted = fields.Char(compute='_compute_formatted_value',
  9. string='New value')
  10. old_value_formatted = fields.Char(compute='_compute_formatted_value',
  11. string='Old value')
  12. record_name = fields.Char(related='mail_message_id.record_name')
  13. model = fields.Char(related='mail_message_id.model', store='True',
  14. string='Model')
  15. @api.depends('new_value_char', 'new_value_integer', 'new_value_float',
  16. 'new_value_text', 'new_value_datetime', 'new_value_monetary',
  17. 'old_value_char', 'old_value_integer', 'old_value_float',
  18. 'old_value_text', 'old_value_datetime', 'old_value_monetary')
  19. def _compute_formatted_value(self):
  20. """ Sets the value formatted field used in the view """
  21. # Could be improved, by styling in the view depending on type of field
  22. for record in self:
  23. if record.field_type in ('many2many', 'one2many', 'char'):
  24. record.new_value_formatted = record.new_value_char
  25. record.old_value_formatted = record.old_value_char
  26. elif record.field_type == 'integer':
  27. record.new_value_formatted = str(record.new_value_integer)
  28. record.old_value_formatted = str(record.old_value_integer)
  29. elif record.field_type == 'float':
  30. record.new_value_formatted = str(record.new_value_float)
  31. record.old_value_formatted = str(record.old_value_float)
  32. elif record.field_type == 'monetary':
  33. record.new_value_formatted = str(record.new_value_monetary)
  34. record.old_value_formatted = str(record.old_value_monetary)
  35. elif record.field_type == 'datetime':
  36. record.new_value_formatted = str(record.new_value_datetime)
  37. record.old_value_formatted = str(record.old_value_datetime)
  38. elif record.field_type == 'text':
  39. record.new_value_formatted = record.new_value_text
  40. record.old_value_formatted = record.old_value_text
  41. @api.model
  42. def create_tracking_values(self, old_value, new_value, col_name, col_info):
  43. """ Add tacking capabilities for many2many and one2many fields """
  44. if col_info['type'] in ('many2many', 'one2many'):
  45. def get_values(source, prefix):
  46. if source:
  47. names = ', '.join(source.mapped('display_name'))
  48. json_ids = json.dumps(source.ids)
  49. else:
  50. names = ''
  51. json_ids = json.dumps([])
  52. return {
  53. '{}_value_char'.format(prefix): names,
  54. '{}_value_text'.format(prefix): json_ids,
  55. }
  56. values = {
  57. 'field': col_name,
  58. 'field_desc': col_info['string'],
  59. 'field_type': col_info['type'],
  60. }
  61. values.update(get_values(old_value, 'old'))
  62. values.update(get_values(new_value, 'new'))
  63. return values
  64. else:
  65. return super(MailTrackingValue, self).create_tracking_values(
  66. old_value, new_value,
  67. col_name, col_info
  68. )