Community: Framework mailing list archives

expert-framework@mail.odoo.com

How to carry forward the context through multiple actions?

by
Temur
- 11/14/2014 04:04:38
Hi all,

I want to carry forward the context set by first wizard to the second and then to the third wizard,
however values in the context are lost between operations, here is an example code:

XML:
================

<?xml version="1.0"?>
<openerp>
    <data>

        <record id="view_test_but_open" model="ir.ui.view">
            <field name="name">test.partners.wizard.form</field>
            <field name="model">test.partners.wizard</field>
            <field name="arch" type="xml">
                <form string="Partners Game" version="7.0">
                    <separator colspan="4" string="Do you really want to start new game?"/>
                    <footer>
                        <button string="New Game" name="test_partners_open_window" type="object" class="oe_highlight"/>
                        or
                        <button string="Cancel" class="oe_link" special="cancel"/>
                    </footer>
                </form>
            </field>
        </record>

        <record id="action_test_but_open" model="ir.actions.act_window">
            <field name="name">Partners Game</field>
            <field name="res_model">test.partners.wizard</field>
            <field name="view_type">form</field>
            <field name="view_mode">tree,form</field>
            <field name="view_id" ref="view_test_but_open"/>
            <field name="target">new</field>
        </record>

        <menuitem id="test_but_open_main"
            name="DEVELOPMENT"
            />
        <menuitem id="test_but_open_sub_section"
            name="Games"
            parent="test_but_open_main"
            />
        <menuitem id="test_but_open_sub"
            parent="test_but_open_sub_section"
            name="Partners Game"
            action="action_test_but_open"
            />

        <record id="action_partners_game_tree_1" model="ir.actions.act_window">
            <field name="name">Partners Game</field>
            <field name="res_model">res.partner</field>
            <field name="view_type">tree</field>
            <field name="view_id" ref="base.view_partner_tree"/>
        </record>

        <record id="view_test_next_level" model="ir.ui.view">
            <field name="name">view.test.next.level.form</field>
            <field name="model">test.partners.level.wizard</field>
            <field name="arch" type="xml">
                <form string="Partners Game - Next Level" version="7.0">
                    <separator colspan="4" string="Do you really want to go to next level of game?"/>
                    <footer>
                        <button string="Next Level" name="next_level" type="object" class="oe_highlight"/>
                        or
                        <button string="Cancel" class="oe_link" special="cancel"/>
                    </footer>
                </form>
            </field>
        </record>

        <record id="action_next_level" model="ir.actions.act_window">
            <field name="name">Next Level</field>
            <field name="res_model">test.partners.level.wizard</field>
            <field name="view_type">form</field>
            <field name="view_mode">tree,form</field>
            <field name="view_id" ref="view_test_next_level"/>
            <field name="target">new</field>
        </record>

        <record model="ir.values" id="ir_open_next_level">
            <field name="key">action</field>
            <field name="key2">tree_but_open</field>
            <field name="model">res.partner</field>
            <field name="name">Next Level</field>
            <field name="value" eval="'ir.actions.act_window,%d'%action_next_level"/>
            <field name="object" eval="True"/>
        </record>

    </data>
</openerp>

PYTHON:
================

# -*- coding: utf-8 -*-

from openerp.osv import fields, osv

import random

random.seed()

class test_partners_wizard(osv.osv_memory):
    _name = "test.partners.wizard"
    _description = "Testing tree_but_open for context durability"
    _columns = {
    }
    _defaults = {
    }
    def test_partners_open_window(self, cr, uid, ids, context=None):
        mod_obj = self.pool.get('ir.model.data')
        act_obj = self.pool.get('ir.actions.act_window')
        if context is None:
            context = {}
        result = mod_obj.get_object_reference(cr, uid, 'test_but_open', 'action_partners_game_tree_1')
        id = result and result[1] or False
        result = act_obj.read(cr, uid, [id], context=context)[0]
        important_value = random.randint(1,100)
        print '[step1] IMPORTANT_VALUE: %s' %important_value # print the important_vaule...

        result['context'] = str({'important_value':important_value})
        return result

test_partners_wizard()

class test_partners_level_wizard(osv.osv_memory):
        _name = "test.partners.level.wizard"
        _description = "Move game to next level"
        _columns = {
        }
        _defaults = {
        }
        def next_level(self, cr, uid, ids, context=None):
            if context == None:
                context = {}
            print '[step2] IMPORTANT_VALUE: %s' %context.get('important_value',False) # it prints: "[step2] IMPORTANT_VALUE: False", i.e. the important_value is lost for the moment. Any suggestions?
            # ....

test_partners_level_wizard()

#
# ....then Wizard3 and stuff like that
#

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

How to carry forward values in a context from the first wizard  to the second wizard in the above code?


Here is my question at help page:
https://www.odoo.com/forum/help-1/question/how-to-preserve-context-across-multiple-actions-wizards-64377


Any tips are appreciated.

thanks,
Temur