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.

79 lines
3.9 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=None):
  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. for browse_record in self.browse(cr, uid, ids, context=context):
  30. initial = initial_values[browse_record.id]
  31. changes = set()
  32. tracked_values = {}
  33. # generate tracked_values data structure: {'col_name': {col_info, new_value, old_value}}
  34. for col_name, col_info in tracked_fields.items():
  35. initial_value = initial[col_name]
  36. record_value = getattr(browse_record, col_name)
  37. if record_value == initial_value and getattr(self._all_columns[col_name].column, 'track_visibility', None) == 'always':
  38. tracked_values[col_name] = dict(col_info=col_info['string'],
  39. new_value=convert_for_display(record_value, col_info))
  40. elif record_value != initial_value and (record_value or initial_value): # because browse null != False
  41. if getattr(self._all_columns[col_name].column, 'track_visibility', None) in ['always', 'onchange']:
  42. tracked_values[col_name] = dict(col_info=col_info['string'],
  43. old_value=convert_for_display(initial_value, col_info),
  44. new_value=convert_for_display(record_value, col_info))
  45. if col_name in tracked_fields:
  46. changes.add(col_name)
  47. if not changes:
  48. continue
  49. # find subtypes and post messages or log if no subtype found
  50. subtypes = []
  51. for field, track_info in self._track.items():
  52. if field not in changes:
  53. continue
  54. for subtype, method in track_info.items():
  55. if method(self, cr, uid, browse_record, context):
  56. subtypes.append(subtype)
  57. posted = False
  58. for subtype in subtypes:
  59. subtype_rec = self.pool.get('ir.model.data').xmlid_to_object(cr, uid, subtype, context=context)
  60. if not (subtype_rec and subtype_rec.exists()):
  61. _logger.debug('subtype %s not found' % subtype)
  62. continue
  63. message = format_message(subtype_rec.description if subtype_rec.description else subtype_rec.name, tracked_values)
  64. self.message_post(cr, uid, browse_record.id, body=message, subtype=subtype, context=context)
  65. posted = True
  66. if not posted:
  67. message = format_message('', tracked_values)
  68. self.message_post(cr, uid, browse_record.id, body=message, context=context)
  69. return True