This question has been flagged
5 Replies
9455 Views

Hi, I am trying to add a feature to require "Log Note", the internal note logged in the chatter, upon canceling 'stock.picking' record.  
My code to override the original "action_cancel"  
  

def action_cancel(self):
view = self.env.ref('mail.email_compose_message_wizard_form')
wiz = self.env['mail.compose.message'].create({'is_log': True})
_logger.info("Action Cancel is overrided.")
super(Picking, self).action_cancel()
return {
'name': _('Your Reason to Cancel?'),
'type': 'ir.actions.act_window',
'view_type': 'form',
'view_mode': 'form',
'res_model': 'mail.compose.message',
'views': [(view.id, 'form')],
'view_id': view.id,
'target': 'new',
'res_id': wiz.id,
'context': {},
}
  

It opens up the write wizard, but when I hit button 'Log', wizard closes but no note is logged in chatter.
Should I pass anything else through context?  

I already tried    


'context': {'active_model': self._name,
'active_id':self.id}

And also this in context.
    'context': {'default_model': self._name,
'default_res_id':self.id}
Avatar
Discard
Best Answer

Pass message_type field and it's value ("comment" or "notification") in the context when you return an action.

'default_message_type': 'comment',


Avatar
Discard
Author

thx for reply, I just tried but still not working.

I also looked into mail_compose_message.py,

line 125: message_type = fields.Selection(default="comment")

I think without default_, on creation, message_type has already been comment.

Try with notification as well in place of comment.

Author

It seems like if I pass default_message_type in context regardless the value, the wizard cannot be opened, and caused odoo-server error:

The requested operation cannot be completed due to security restrictions. Please contact your system administrator.

(Document type: Email composition wizard, Operation: create)

is_log = fields.Boolean('Log an Internal Note', help='Whether the message is an internal note (comment mode only)')

Field is_log says it only works in comment mode, I am so lost.

Author Best Answer

I finally got it figured out.  For future reference,  

To Log note in the chatter.  There is a built-in function available named "message_post".  

There's no need to directly call the built-in wizard, I can create my own wizard and just write an action to use "message_post".  

"message_post" is available by _inherit = ['mail.thread', 'mail.activity.mixin'], which is needed for the xml form view to display chatter section anyway.  

Example:  

# This method overrides action_cancel and calls my own wizard.
def action_cancel(self):
view = self.env.ref('mymodule.xmlid_of_wizard')
wiz = self.env['action.cancel.confirm'].create({})
_logger.info("Action Cancel is overrided.")
super(Picking, self).action_cancel()
return {
'name': _('Your Reason to Cancel?'),
'type': 'ir.actions.act_window',
'view_type': 'form',
'view_mode': 'form',
'res_model': 'action.cancel.confirm',
'views': [(view.id, 'form')],
'view_id': view.id,
'target': 'new',
'res_id': wiz.id,
'context': {'parent_model': self._name,
'parent_id': self.id},
}


# Method in the wizard to Log
def action_confirm(self):
parent = self.env[self._context['parent_model']].browse(self._context['parent_id'])
msg = "Log to write"
parent.message_post(body=msg)
Avatar
Discard