Skip to content

Instantly share code, notes, and snippets.

@ray-odoo
Created August 8, 2020 22:40
Show Gist options
  • Save ray-odoo/e2c94b70800a66b02025adf9943f6832 to your computer and use it in GitHub Desktop.
Save ray-odoo/e2c94b70800a66b02025adf9943f6832 to your computer and use it in GitHub Desktop.
# Available variables:
# - env: Odoo Environment on which the action is triggered
# - model: Odoo Model of the record on which the action is triggered; is a void recordset
# - record: record on which the action is triggered; may be void
# - records: recordset of all records on which the action is triggered in multi-mode; may be void
# - time, datetime, dateutil, timezone: useful Python libraries
# - log: log(message, level='info'): logging function to record debug information in ir.logging table
# - Warning: Warning Exception to use with raise
# To return an action, assign: action = {...}
# create a User called 'Listener' with the Warning Avatar and update ID on line 50
out_prefix = '<ul class="o_mail_thread_message_tracking">'
arrow = ' <span class="fa fa-long-arrow-right" role="img" aria-label="Changed" title="Changed"></span> '
out_suffix = '</ul>'
in_prefix = '<li><div class="o_thread_message_content">'
in_suffix = '</div></li>'
for record in records:
message = out_prefix
found = False
for field_name in env.context['old_values'][record.id]:
if not record.env['ir.model.fields'].search([('model_id','=','product.template'),('name','=',field_name),('tracking','>',0)]):
field = record.env['ir.model.fields'].search([('model_id','=','product.template'),('name','=',field_name)])
field_label = field.field_description + ': '
field_type = field.ttype
if field_type in ['boolean','char','date','datetime','float','html','integer','monetary','selection','text','many2one']:
found = True
old_value = env.context['old_values'][record.id][field_name] or ''
new_value = record[field_name] or ''
if field_type in ['boolean','char','date','datetime','html','integer','monetary','text']:
old_value = str(old_value)
new_value = str(new_value)
elif field_type == 'selection':
old_label = env['ir.model.fields.selection'].search([('field_id','=',field.id),('value','=',old_value)]).name or ''
new_label = env['ir.model.fields.selection'].search([('field_id','=',field.id),('value','=',new_value)]).name or ''
old_value = old_label
new_value = new_label
elif field_type == 'many2one':
old_value = env.context['old_values'][record.id][field_name][1]
new_value = record[field_name].display_name
else:
old_value = '%.2f' % old_value
new_value = '%.2f' % new_value
message += in_prefix + field_label + old_value + arrow + new_value + in_suffix
message += out_suffix
if found:
record.message_post(body = message,author_id = 14)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment