Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to popup success message in odoo?

I am sending invitation by clicking button after clicking button and successfully sending invitation there is pop up message of successfully invitation send. But the problem is that the main heading of pop up message is Odoo Server Error. That is because I am using

raise osv.except_osv("Success", "Invitation is successfully sent")

Is there any alternative to make it better.

like image 404
Adam Strauss Avatar asked Oct 09 '19 10:10

Adam Strauss


1 Answers

When I need something like this I have a dummy wizard with message field, and have a simple form view that show the value of that field.

When ever I want to show a message after clicking on a button I do this:

     @api.multi
     def action_of_button(self):
        # do what ever login like in your case send an invitation
        ...
        ...
        # don't forget to add translation support to your message _()
        message_id = self.env['message.wizard'].create({'message': _("Invitation is successfully sent")})
        return {
            'name': _('Successfull'),
            'type': 'ir.actions.act_window',
            'view_mode': 'form',
            'res_model': 'message.wizard',
            # pass the id
            'res_id': message_id.id,
            'target': 'new'
        }

The form view of message wizard is as simple as this:

<record id="message_wizard_form" model="ir.ui.view">
    <field name="name">message.wizard.form</field>
    <field name="model">message.wizard</field>
    <field name="arch" type="xml">
        <form >
            <p class="text-center">
                <field name="message"/>
            </p>
        <footer>
            <button name="action_ok" string="Ok" type="object" default_focus="1" class="oe_highlight"/> 
        </footer>
        <form>
    </field>
</record>

Wizard is just simple is this:

class MessageWizard(model.TransientModel):
    _name = 'message.wizard'

    message = fields.Text('Message', required=True)

    @api.multi
    def action_ok(self):
        """ close wizard"""
        return {'type': 'ir.actions.act_window_close'}

Note: Never use exceptions to show Info message because everything run inside a big transaction when you click on button and if there is any exception raised a Odoo will do rollback in the database, and you will lose your data if you don't commit your job first manually before that, witch is not recommended too in Odoo

like image 152
Charif DZ Avatar answered Sep 20 '22 20:09

Charif DZ