如何在OpenERP上创建动态视图

时间:2011-07-04 09:51:41

标签: python openerp

我需要帮助才能创建动态视图。让我解释一下:我有Form1和Form2的观点。 Form1包含expression字段和extract按钮。 Form2包含Form1中表达式的提取元素。例如,当您向Form1的表达式字段输入(a+b)*c-d*0,5时,Form2应该提取并显示:

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

现在,这是我的班级:

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()

这是我的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>

到目前为止,它只是在Form1和Form2之间切换。我如何提取表达式,如上所述?

3 个答案:

答案 0 :(得分:6)

要在openerp v6中添加动态视图,请覆盖函数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将是一个字典,主要包含两个内容,Xml架构和字段。 以result['arch']形式提供xml体系结构作为字符串,提供result['fields']中的字段作为字典字典。然后返回结果。然后,您将根据您在字段和架构中给出的内容获得视图。

答案 1 :(得分:1)

更简单的方法是......首先从第一个表单(FORM 1)获取表达式,然后根据您的选择对其进行评估,并保留第二个表单中的“TEXT”字段,该字段按照您的格式包含此数据在那个领域..

答案 2 :(得分:-1)

我不确定你要问的是什么,但如果你只想解析用户的表达,那么this question on Python expression parsers应该会有所帮助。