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.

88 lines
3.6 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 Therp BV <https://therp.nl>
  3. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  4. from openerp import api, models, _
  5. class MailThread(models.AbstractModel):
  6. _inherit = 'mail.thread'
  7. @api.model
  8. def _get_tracked_fields(
  9. self,
  10. updated_fields,
  11. suppress_x2many=True):
  12. result = super(MailThread, self)._get_tracked_fields(
  13. updated_fields)
  14. if not result:
  15. return result
  16. return dict(filter(
  17. lambda (y, x): not suppress_x2many
  18. or x['type'] not in ['one2many', 'many2many'],
  19. result.iteritems()))
  20. def _format_value_dict(self, col_info, values_id, values):
  21. relation_model = self.env[col_info['relation']]
  22. relation_model_fields = relation_model.fields_get(values.keys())
  23. message = ''
  24. for field, value in values.iteritems():
  25. message += '<br/> '
  26. if relation_model_fields[field]['type'] in [
  27. 'many2one', 'many2many', 'one2many']:
  28. field_model = relation_model_fields[field]['relation']
  29. if isinstance(value, tuple):
  30. value = value[0]
  31. value = self.env[field_model].browse(value).name_get()
  32. message += '%s &rarr; %s' % (
  33. relation_model_fields[field]['string'], value)
  34. record_name = ''
  35. record = relation_model.browse(values_id)
  36. for (__, name) in record.name_get():
  37. record_name = name
  38. return record_name, message
  39. @api.multi
  40. def write(self, values):
  41. x2many_fields = dict(filter(
  42. lambda (y, x): x['type'] in ['one2many', 'many2many']
  43. and y in values,
  44. (self._get_tracked_fields(
  45. values.keys(),
  46. suppress_x2many=False) or {}).iteritems()))
  47. if x2many_fields:
  48. message = '<ul>'
  49. for field in x2many_fields:
  50. message += '<li>%s<ul>' % x2many_fields[field].get('string',
  51. field)
  52. for value in values[field]:
  53. if value[0] in [3, 4, 5, 6]:
  54. continue
  55. message += '<li>'
  56. if value[0] == 0:
  57. message += _('Added: %s') % self._format_value_dict(
  58. x2many_fields[field],
  59. value[1],
  60. value[2])[1]
  61. elif value[0] == 1:
  62. message += _('Changed %s: %s') % self\
  63. ._format_value_dict(
  64. x2many_fields[field],
  65. value[1],
  66. value[2])
  67. elif value[0] == 2:
  68. deleted_line_model = getattr(self, field)._name
  69. fields = self.env[deleted_line_model]._fields.keys()
  70. fields = self.env[deleted_line_model].browse(
  71. value[1]).read(fields)
  72. message += _('Deleted: %s') % self._format_value_dict({
  73. 'relation': deleted_line_model},
  74. value[1],
  75. fields[0],
  76. )[1]
  77. else:
  78. message += '%s' % str(value)
  79. message += '</li>'
  80. message += '</ul></li>'
  81. message += '</ul>'
  82. self.message_post(body=message)
  83. return super(MailThread, self).write(values)