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.

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