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.

89 lines
4.2 KiB

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