Skip to Content
Menu
This question has been flagged
1 Reply
5675 Views

Hello,

I want to put two actions for one menuitem, I can I do this ?


There is my python code :

class miadi_poidsConditionnement(models.Model):

    _name = 'miadi.poidsconditionnement'

    # _order = 'conditionnement_id asc'

    conditionnement_id = fields.Many2one('miadi.packaging', 'Packaging', default='', required=True)

    produit_id = fields.Many2one('product.product', 'Product', default='', required=True)

    nb_articles = fields.Integer(string='Number of products', default=0)

    poids = fields.Float(string='Packaging Weight', default=0)

    _sql_constraints = [

    ('uniq_id', 'unique(produit_id, conditionnement_id)', 'A product already exists with this packaging !'),

    ]

    @api.multi

    @api.model

    def chargement_ratio(self):

        productRatio = self.env['miadi.poidsconditionnement']

        conditionnements = self._cr.execute("SELECT id FROM miadi_packaging")

        query_cond = self.env.cr.dictfetchall()

        produits = self._cr.execute("SELECT id FROM product_product")

        query_prod = self.env.cr.dictfetchall()

        for conditionnements in query_cond:

            nom_conditionnement = conditionnements['id']

            for produits in query_prod:

                product = produits['id']

                if_exist = self._cr.execute("SELECT * FROM miadi_poidsConditionnement WHERE conditionnement_id=%s AND produit_id=%s", (nom_conditionnement, product))

                if not if_exist:

                    Ratio = productRatio.create({'conditionnement_id': nom_conditionnement, 'produit_id': product})


And there is my xlm code :

<?xml version="1.0" encoding="utf-8"?>

<openerp>

 <data>

  <record id="miadi_poidsConditionnement_form" model="ir.ui.view">

   <field name="name">miadi.poidsconditionnement.form</field>

   <field name="model">miadi.poidsconditionnement</field>

   <field name="arch" type="xml">

    <form string="Weight/number ratio">

     <group col="4">

      <field name="produit_id" widget="selection" eval="ref('product.product.attribute_value_ids')" />

      <!-- <field name="produit_id" /> -->

      <field name="conditionnement_id" widget="selection" eval="ref('miadi.packaging.conditionnement_id')" />

      <!-- <field name="conditionnement_id" /> -->

      <field name="nb_articles" />

      <field name="poids" />

     </group>

    </form>

   </field>

  </record>

  <record id="miadi_poidsConditionnement_tree" model="ir.ui.view">

   <field name="name">miadi.poidsconditionnement.tree</field>

   <field name="model">miadi.poidsconditionnement</field>

   <field name="arch" type="xml">

    <tree string="Weight/number ratio">

     <field name="produit_id" ref="product.product.attribute_value_ids" />

     <field name="conditionnement_id" ref="miadi.packaging.conditionnement_id" />

     <field name="nb_articles" />

     <field name="poids" />

    </tree>

   </field>

  </record>

  <record model="ir.ui.view" id="miadi_poidsConditionnement_search">

   <field name="name">miadi.poidsconditionnement.search</field>

   <field name="model">miadi.poidsconditionnement</field>

   <field name="arch" type="xml">

    <search string="Weight/number ratio">

     <field name="produit_id" />

     <group expand="0" string="Group by">

      <filter string="Product" domain="[]" context="{'group_by':'produit_id'}"/>

      <filter string="Packaging" domain="[]" context="{'group_by':'conditionnement_id'}"/>

     </group>

    </search>

   </field>

  </record>

  <record model="ir.actions.act_window" id="action_miadi_poidsConditionnement">

   <field name="name">Weight/number ratio</field>

   <field name="type">ir.actions.act_window</field>

   <field name="res_model">miadi.poidsconditionnement</field>

   <field name="view_type">form</field>

   <field name="view_mode">tree,form</field>

   <field name="view_id" ref="miadi_poidsConditionnement_tree"/>

   <field name="help" type="html">

    <p class="oe_view_nocontent_create">Click here to create a weight/number ratio.</p>

   </field>

  </record>

  <record id="action_chargement_ratio" model="ir.actions.server">

   <field name="name">Chargement Ratio</field>

   <field name="res_model">action_miadi_poidsConditionnement</field>

   <field name="type">ir.actions.server</field>

   <field name="model_id" ref="model_miadi_poidsconditionnement"/>

   <field name="code">chargementRatio = env['miadi.poidsconditionnement'];chargementRatio.chargement_ratio()</field>

  </record>

  <menuitem name="Weight/number ratio" id="menu_miadi_poidsConditionnement" parent="menu_miadi_packaging" sequence="15" groups="sales_team.group_sale_manager" action="action_miadi_poidsConditionnement" />

 </data>

</openerp>


I want to give 2 actions (action_miadi_poidsConditionnement and action_chargement_ratio) for the same menuitem (Weight/Number Ratio)


I hope someone will can help me,

Thanks for answers

Avatar
Discard
Best Answer

Hi Miadi,

Why are you trying to call two actions from same menu item ? If you want to display the two different views from same menu based on condition, You can use the concept of the server action, then from the python function you can return respective view based on the condition

Avatar
Discard
Author

Hi Niyas, I want to call two iactions because the first action is the display of tree view (action_miadi_poidsConditionnement) and the second action is a server action (action_chargement_ratio) which is calling a function to do SQL queries and add to the table (miadi.poidsconditionnement) some records and display them in the tree view.

I don't know if I'm really understandable but I hope you will can help me

Author

That I want, it's to call the function chargement_ratio(self) when I click on the Weight/number Ratio menuitem

Hi Miadi, you can call server action and can do the sql queries, and then you can return the id of the tree view from the python function

Author

Hi niyas,

How can I do this ? Can you give example code ? (or with my code if you want)

python side,

def function_name(self):

# do wat you want to code, then return like this

return {

'type': 'ir.actions.act_window',

'view_type': 'form',

'view_mode': 'form',

'res_model': 'account.invoice',

'res_id': invoice.id,

'view_id': self.env.ref('account.invoice_form').id,

'target': 'current',

}

In the xml,

<record id="contract_set_as_pending" model="ir.actions.server">

<field name="name">Test</field>

<field name="model_id" ref="model_hr_contract"/>

<field name="condition">True</field>

<field name="type">ir.actions.server</field>

<field name="state">code</field>

<field name="code">function_name()</field>

</record>

Author

I don't know if it's good but I made this :

Python :

def chargement_ratio(self):

#do what I want to code

return {

'type': 'ir.actions.act_window',

'view_type': 'tree',

'view_mode': 'tree',

'res_model': 'miadi.poidsconditionnement',

'res_id': miadi.poidsconditionnement.id,

'view_id': self.env.ref('action_miadi_poidsConditionnement').id,

}

XML :

<record id="action_chargement_ratio" model="ir.actions.server">

<field name="name">Chargement Ratio</field>

<field name="res_model">action_miadi_poidsConditionnement</field>

<field name="type">ir.actions.server</field>

<field name="model_id" ref="model_miadi_poidsconditionnement"/>

<field name="code">chargement_ratio()</field>

</record>

<menuitem name="Weight/number ratio" id="menu_miadi_poidsConditionnement" parent="menu_miadi_packaging" sequence="15" groups="sales_team.group_sale_manager" action="action_chargement_ratio" />

bit I an issue : ValueError: <type 'exceptions.NameError'>: "name 'chargement_ratio' is not defined" while evaluating

u'chargement_ratio()'

make it like this,

<field name="code">object.chargement_ratio()</field>

Author

I tried to make this :

<field name="code">chargementRatio = env['miadi.poidsconditionnement'].browse(context.get('active_id'));chargementRatio.chargement_ratio()</field>

And it gives me this :

ValueError: <type 'exceptions.NameError'>: "global name 'miadi' is not defined" while evaluating

u"chargementRatio = env['miadi.poidsconditionnement'].browse(context.get('active_id'));chargementRatio.chargement_ratio()"

I think the error is here :

'res_id': miadi.poidsconditionnement.id

No ?

Author

And in your last comment, I've to write "object.chargement_ratio()" or I've to write another thing instead of "object" ?