0
votes

I'm new in openERP and I'm creating a web module. In this module I want to read a file on the server, display its content on the screen and print the content of the screen with a button.

I have made the python function that open the file and return its content. And I have made a widget that displays a button that triggers the printer call. However, I don't know how to call the python function from the JQuery code.

Here is how my module looks :

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

Here my python code :

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

Here is my JS code : I want the get the result of the function def button_function(...) in the file.

// 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
        }

    });
};

And 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>

The template (static/src/xml/web_example.xml): the 1st button is to load the file in JS and the 2nd is the print the page

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

Can anyone help me to call the python function from the JS code? Or tell me if I'm doing it in the wrong way...

2

2 Answers

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

if you want to pass other parameters beside "ids", it will be like: call("button_function",[ids,param1,param2]) and in python:

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

You also should know that these calls are async, so the call of the python function may be later than you expect. Here is explained something http://doc.openerp.com/trunk/developers/web/rpc/

The problem with the late call I have it too, so if you sort it out please post it here.

0
votes

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) 
     { 
     }));