0

我是 openERP 的新手,我正在创建一个 web 模块。在这个模块中,我想读取服务器上的文件,在屏幕上显示其内容并使用按钮打印屏幕内容。

我已经创建了打开文件并返回其内容的 python 函数。我制作了一个小部件,显示一个触发打印机调用的按钮。但是,我不知道如何从 JQuery 代码中调用 python 函数。

这是我的模块的外观:

<web_example>
  +-- __openerp__.py
  +-- __init__.py
  +-- web_example.py
  +-- web_example_view.xml
  +-- static/
       +-- src/
            +-- js/
                +--first_module.js
            +-- xml/
                +--web_example_view.xml

这是我的python代码:

from osv import fields, osv
import re


class web_example(osv.osv):

    _name='web.example'
    #_table ='test_printer'
    _description = 'Test pour l\'impression de fichiers'

    def button_function(self, cr, uid, ids, context=None):
        filename = '/home/stage2013/Documents/testlecture'
        fichier = open('/home/stage2013/Documents/testlecture', 'r')
        toutesleslignes = fichier.readlines()
        fichier.close()
        print toutesleslignes
        return toutesleslignes  

web_example()

这是我的 JS 代码:我想要在文件中获取函数 def button_function(...) 的结果。

// static/src/js/first_module.js
openerp.web_example = function (instance) {
    instance.web.client_actions.add('example.action', 'instance.web_example.Action');
    instance.web_example.Action = instance.web.Widget.extend({
        template: 'web_example.action',
        events: {
            'click .oe_web_example_print button' : 'print_start',
            'click .oe_web_example_load button' : 'load_start'
        },
        init: function () {
            this._super.apply(this, arguments);
        },

        print_start: function () {
            window.print();
        },
        load_start: function () {
            //CALL THE PYTHON FUNCTION FROM WEB_EXAMPLE.PY
        }

    });
};

和 xml (web_example.xml):

<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>
        <record model="ir.actions.client" id="action_client_example">
            <field name="name">Example Client Action</field>
            <field name="tag">example.action</field>
        </record>        

        <menuitem action="action_client_example" id="test_printer_2" parent="product.menu_products" name="Test impression 2"/>
    </data>
</openerp>

模板(static/src/xml/web_example.xml):第一个按钮是在JS中加载文件,第二个是打印页面

<templates>
<div t-name="web_example.action" class="oe_web_example oe_web_example_stopped">
    <p class="oe_web_example_load">
        <button type="button">Load file</button>
    </p>
    <p class="oe_web_example_print">
        <button name="button_function" type="object"  >Imprimer</button>
    </p>
    <textarea id="response_fichier"></textarea>
</div>
</templates>

谁能帮我从 JS 代码中调用 python 函数?或者告诉我我是否以错误的方式这样做......

4

2 回答 2

1
new instance.web.Model("web.example").call("button_function",[ids]).then(function(res) {
   //res is the result returned by the python function
 });

如果你想传递“ids”之外的其他参数,它会像: call("button_function",[ids,param1,param2]) 在python中:

def button_function(self, cr, uid, ids, param1, param2, context=None)

您还应该知道这些调用是异步的,因此 python 函数的调用可能比您预期的要晚。这里解释了一些东西http://doc.openerp.com/trunk/developers/web/rpc/

迟到的问题我也有,所以如果你把它整理出来,请在这里发布。

于 2013-05-10T11:34:14.273 回答
0

Here search_read is function that is called from pos.js

return $.when(new db.web.Model("sale.order").get_func("search_read")([]).pipe(function(result) 
   {
     var _name = result;
     alert(_name.toSource());

    }),
new db.web.Model("res.users").get_func("read")(this.session.uid, ['name']).pipe(function(result) 
     { 
     }));
于 2013-05-08T07:44:28.087 回答