Help

4

How to pass context from wizard to view and to and other action after?

Avatar
Pascal Tremblay

Hello all,

I have a new wizard who run well. This wizard allow user to choose a category.

When the user confirms the wizard, a new board.board view is started with the value of the choosen category in the context. Many graphs will be on this board according to the choosen category.

When the board.board view receive the context, how can it pass the context to the first graph action?

After this, how could the graph action use this context in its own domain?

I have a new field on stock.quant model (product_categ_id) and I want the first graph of my board only has quants with the category choosen by the user in the wizard.

Could you help please?


The wizard python (context has the choosen category in it) :

class wizard_choose_category(models.TransientModel):
    _name = 'wizard.choose.category'
    _description = 'Wizard that allow to choose a category'
    choose_category = fields.Many2one('product.category', string='Product category', domain=[('parent_id','!=', 1)])        #date = fields.Datetime('Date', default=fields.Datetime.now, required=True)        @api.multi    def open_table(self):
        self.ensure_one()
        ctx = dict(
            self._context,
            category=self.choose_category.id
            )
            
            res = {
            'domain': "[]",           
                'name': _('My dashboard by category'),
                'view_type': 'form',
                'view_mode': 'form',
                'res_model': 'board.board',
                'type': 'ir.actions.act_window',
                'context': ctx,
                'view_id': self.env.ref('vtm2_cavavin_inventory_graphs.inventory_by_category_form').id,
                'target': 'current',
            }

The view of boards (how to pass the received context to the called action?) :

<record id="inventory_by_category_form" model="ir.ui.view">
    <field name="name">Inventory dashboard form</field>
    <field name="model">board.board</field>
    <field name="arch" type="xml">
      <form string="Inventory dashboard">
            <board style="1-0">
                <column>
                    <action context="get.context()" string="Inventory pivot table"
                        name="%(action_inventory_pivot_1)d" />
                            
                </column>
            </board>
        </form>
    </field>
</record>


The action of the first graph (how to use the received context in the domain?) :

<record id="action_inventory_pivot_1" model="ir.actions.act_window">
            <field name="name">My inventory pivot #1</field>
            <field name="res_model">stock.quant</field>
            <field name="view_type">form</field>
            <field name="view_mode">graph</field>
            <field name="domain">[]</field>
            <field name="view_id" ref="inventory_pivot_1" />
        </record>


EDIT #1

I was certain that this line would work... But it is not the case...

<action string="Inventory of your choosen category - pivot table" name="%(action_inventory_pivot_1)d" 
    context="{'category': context.get('category')}" />



EDIT #2

With this line, all works fine. My graph displays all the quant from de category = 9. So the action tag of in a board view can pass the context to the called action. But the context.get() method doesn't seem to work in it...

<action string="Inventory of your choosen category - pivot table" name="%(action_inventory_pivot_1)d"
     context="{'category': 9}" />
1 Comment
Avatar
Discard
Avatar
Sehrish
-
2 Answers
3
Avatar
Pascal Tremblay
Best Answer

Here is our solution.


The action of the first graph (with the new domain who uses the context value) :

<record id="action_inventory_pivot_1" model="ir.actions.act_window">
            <field name="name">My inventory pivot #1</field>
            <field name="res_model">stock.quant</field>
            <field name="view_type">form</field>
            <field name="view_mode">graph</field>
            <field name="domain">[('product_category_id','=',context.get('category'))]</field>
            <field name="view_id" ref="inventory_pivot_1" />
        </record>


And we use Python to override the context of the board.board action because it doesn't want to understand context.get() method.

from lxml import etree

class board_board(osv.osv):
    _inherit = 'board.board'
    def fields_view_get(self, cr, user, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):

        result = super(board_board,self).fields_view_get(cr, user, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=submenu)
       
        doc = etree.XML(result['arch'])
       
        for node in doc.xpath("//action"):
            set_context = "{'choosen_category': %s}" % context.get('category')
            node.set("context", set_context)
                result["arch"] = etree.tostring(doc)
        
        return result




Avatar
Discard