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
4.2 KiB

  1. # -*- coding: utf-8 -*-
  2. from openerp.osv import osv
  3. class MailThread(osv.Model):
  4. _inherit = "mail.thread"
  5. def message_track(self, cr, uid, ids, tracked_fields, initial_values, context={}):
  6. def convert_for_display(value, col_info):
  7. if not value and col_info['type'] == 'boolean':
  8. return 'False'
  9. if not value:
  10. return ''
  11. if col_info['type'] == 'many2one':
  12. return value.name_get()[0][1]
  13. if col_info['type'] == 'selection':
  14. return dict(col_info['selection'])[value]
  15. return value
  16. def format_message(message_description, tracked_values):
  17. message = ''
  18. if message_description:
  19. message = '<span>%s</span>' % message_description
  20. for name, change in tracked_values.items():
  21. message += '<div> &nbsp; &nbsp; &bull; <b>%s</b>: ' % change.get('col_info')
  22. if change.get('old_value'):
  23. message += '%s &rarr; ' % change.get('old_value')
  24. message += '%s</div>' % change.get('new_value')
  25. return message
  26. if not tracked_fields:
  27. return True
  28. update_fields = [f for f in tracked_fields]
  29. for browse_record in self.browse(cr, uid, ids, context=context):
  30. p = getattr(browse_record, 'partner_id', None)
  31. if p:
  32. browse_record._context.update({'lang': p.lang})
  33. initial = initial_values[browse_record.id]
  34. changes = set()
  35. tracked_values = {}
  36. # update translation
  37. tracked_fields = self._get_tracked_fields(cr, uid, update_fields, browse_record._context)
  38. # generate tracked_values data structure: {'col_name': {col_info, new_value, old_value}}
  39. for col_name, col_info in tracked_fields.items():
  40. initial_value = initial[col_name]
  41. record_value = getattr(browse_record, col_name)
  42. if record_value == initial_value and getattr(self._all_columns[col_name].column, 'track_visibility', None) == 'always':
  43. tracked_values[col_name] = dict(col_info=col_info['string'],
  44. new_value=convert_for_display(record_value, col_info))
  45. elif record_value != initial_value and (record_value or initial_value): # because browse null != False
  46. if getattr(self._all_columns[col_name].column, 'track_visibility', None) in ['always', 'onchange']:
  47. tracked_values[col_name] = dict(col_info=col_info['string'],
  48. old_value=convert_for_display(initial_value, col_info),
  49. new_value=convert_for_display(record_value, col_info))
  50. if col_name in tracked_fields:
  51. changes.add(col_name)
  52. if not changes:
  53. continue
  54. # find subtypes and post messages or log if no subtype found
  55. subtypes = []
  56. for field, track_info in self._track.items():
  57. if field not in changes:
  58. continue
  59. for subtype, method in track_info.items():
  60. if method(self, cr, uid, browse_record, context):
  61. subtypes.append(subtype)
  62. posted = False
  63. for subtype in subtypes:
  64. subtype_rec = self.pool.get('ir.model.data').xmlid_to_object(cr, uid, subtype, context=context)
  65. if not (subtype_rec and subtype_rec.exists()):
  66. _logger.debug('subtype %s not found' % subtype)
  67. continue
  68. message = format_message(subtype_rec.description if subtype_rec.description else subtype_rec.name, tracked_values)
  69. self.message_post(cr, uid, browse_record.id, body=message, subtype=subtype, context=context)
  70. posted = True
  71. if not posted:
  72. message = format_message('', tracked_values)
  73. self.message_post(cr, uid, browse_record.id, body=message, context=context)
  74. return True