Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

python - How to create a dynamic view on OpenERP

I need a help to create a dynamic view. Let me explain: I have Form1 and Form2 views. Form1 contains expression field and extract button. Form2 contains extracted elements of the expression from Form1. For example when you enter (a+b)*c-d*0,5 to Form1's expression field, Form2 should extract and display this:

( - open brace
a - variable
+ - addition
b - variable
) - close brace
* - multiplication
c - variable
- - subtraction
d - variable
* - multiplication
0,5 - constant number

Now, here is my class:

class wz_formula(osv.osv_memory):
    """
        Formula Wizard
    """
    _name = "wz.formula"
    _inherit = "ir.wizard.screen"
    _description = "Formula Wizard"

    _columns = {
            'name': fields.char('Formula name', required=True, size=64)
            , 'expression': fields.char('expression', required=True, size=64)
            , 'state': fields.selection([('init','init'),('done','done')], 'state', readonly=True)
    }

    _defaults = {
            'state': 'init'
    }

    def next(self, cr, uid, ids, context=None):
        if context is None:
            context = {}
        formula_obj = self.browse(cr, uid, ids)[0]
        formula_name = formula_obj.name
        infix = formula_obj.expression
        if formula_name and expression:
            modobj = self.pool.get('ir.module.module')
            mids = modobj.search(cr, uid, [('state', '=', 'installed')])
            modobj.update_translations(cr, uid, mids, [formula_name, expression], context or {})
            self.write(cr, uid, ids, {'state': 'done'}, context=context)
        return {
            'name': _('Formula')
            , 'view_type': 'form'
            , 'view_mode': 'form'
            , 'view_id': False
            , 'res_model': 'wz.formula'
            , 'domain': []
            , 'context': dict(context, active_ids=ids)
            , 'type': 'ir.actions.act_window'
            , 'target': 'new'
            , 'res_id': ids and ids[0] or False
        }

# create an object
wz_formula()

Here is my xml:

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

        <record id="view_wz_formula" model="ir.ui.view">
            <field name="name">Formula</field>
            <field name="model">wz.formula</field>
            <field name="type">form</field>
            <field name="arch" type="xml">
                <form string="Formula">
                    <group colspan="8" col="8" states="init">
                        <separator string="Formula" colspan="8"/>
                        <field name="state" invisible="1"/>
                        <field name="name"/>
                        <field name="expression" width="220"/>
                        <button special="cancel" string="Cancel" icon="gtk-cancel" colspan="1"/>
                        <button name="next" string="Next" type="object" icon="gtk-ok" colspan="1"/>
                    </group>
                    <group colspan="8" col="8" states="done">
                        <separator string="Done" colspan="8"/>
                        <button special="cancel" string="Close" icon="gtk-cancel"/>
                    </group>
                </form>
           </field>
        </record>

        <record id="action_view_wz_formula" model="ir.actions.act_window">
            <field name="name">Formula</field>
            <field name="type">ir.actions.act_window</field>
            <field name="res_model">wz.formula</field>
            <field name="view_type">form</field>
            <field name="view_mode">form</field>
            <field name="target">new</field>
        </record>

        <menuitem
            name="Create a formula"
            action="action_view_wz_formula"
            id="menu_view_wz_formula"
            parent="menu_fs_root" sequence="2"/>

    </data>
</openerp>

So far, it just switches between Form1 and Form2. How can I extract the expression as I explained above?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To add a dynamic view in openerp v6, override the function fields_view_get()

def fields_view_get(self, cr, uid, view_id=None, view_type='form', context={}, toolbar=False):
    result = super(<your_class_name>, self).fields_view_get(cr, uid, view_id, view_type, context=context, toolbar=toolbar)
    # your modification in the view
    # result['fields'] will give you the fields. modify it if needed
    # result['arch'] will give you the xml architecture. modify it if needed
    return result

result will be a dictionary which contain mainly two things, Xml architecture and fields. Provide the xml architecture in result['arch'] as string, provide the fields in result['fields'] as dictionary of dictionaries. Then return the result. Then you will get a view according to what you have given in the fields and architecure.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...