Palza
Palza

Reputation: 788

Odoo 9 yes/no pop-up box python

Odoo v9.

I would like to prompt the user with a yes/no option before certain python code is executed.

If this were in a button I could use the confirm="text?" attribute, however I do not want to trigger any database updates irrespective of the users answer to this particular prompt.

e.g. onchange event triggers yes/no box, which can then manipulate page, however the user still has to press "save" to keep any changes.

Is there any way of doing this using base odoo?

Upvotes: 0

Views: 1818

Answers (1)

danidee
danidee

Reputation: 9624

onchange events can't return a dialog because they were simply not built to do that https://www.odoo.com/forum/how-to/developers-13/what-should-onchange-methods-do-and-return-57760

You can use a wizard (it's not really a wizard it's just a form) and show it as a popup. our wizard model will be a Transient model, which would be discarded by odoo after a specified amount of time.

from openerp import fields, models, api

class test_model(models.Model):
    _name = 'test.model'
    name = fields.Char(string='Value')

    @api.multi
    def call_up_wizard(self):
      return {
            'name': 'Are you sure?',
            'type': 'ir.actions.act_window',
            'res_model': 'wizard',
            'view_mode': 'form',
            'view_type': 'form',
            'target': 'new',
             }

class wizard(models.TransientModel):
    _name = 'wizard'

    yes_no = fields.Char(default='Do you want to proceed?')

    @api.multi
    def yes(self):
        pass
       # sure continue!

    @api.multi
    def no(self):
        pass # don't do anything stupid

This is how your view would look like

<record id="cashadvance_list" model="ir.ui.view">
  <field name="name">Test Dialog</field>
  <field name="model">test.model</field>
  <field name="type">form</field>
  <field name="arch" type="xml">
    <form string="Test Dialog" version="8.0">
      <group>
        <button class="oe_highlight" name="call_up_wizard" string="confirm" type="object" />
      </group>
    </form>
  </field>
</record>

<record model="ir.ui.view" id="wizard_form">
  <field name="name">wizard.form</field>
  <field name="model">wizard</field>
  <field name="type">form</field>
  <field name="arch" type="xml">
    <form string="Confirm dialog">

      <field name="yes_no" readonly="1" />

        <footer>
          <button class="oe_highlight" name="yes" string="Yes" />
          <button class="oe_highlight" name="no" string="No" />
        </footer>

    </form>
  </field>
</record>


      <record model="ir.actions.act_window" id="action_test">
            <field name="name">Cash advance list</field>
            <field name="type">ir.actions.act_window</field>
            <field name="res_model">test.model</field>
            <field name="view_type">form</field>
            <field name="view_mode">form,tree</field>
            <field name="help" type="html">
                <p class="oe_view_nocontent_create">
                  It's empty, create something
                </p>
                </field>
        </record>

In our test_model view we have a button that's shows the wizard as a popup, and then in the wizard buttons are handled by the yes and no methods respectively, this gives you better control over the buttons as you know have separate methods controlling the two buttons you have on the form.

I just put this answer, for anyone who happens to stumble on this thread and wants to know how to create a popup from a button.

Upvotes: 2

Related Questions