This question has been flagged
1 Reply
8359 Views

I'm mew in OpenERP and I'm trying to create a web module "web_example".

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

In web_example.py I have a function that returns the content of a file on the server. In static/src/xml/web_exemple.py I have a button which triggers javascript from first_module.js. And in first_module.js I would like to call the function from web_example.py to get the content of the file. How should I do?

Here is my code :

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

        });
    };




#web_example.py

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

web_example()
Avatar
Discard
Best Answer

To call a py method in openerp web_addon, first get the dataset od that object and then call the method. For ex, in this case it can be like :

load_start: function () {

//ids = get the id of the current record
   // context : get the context 
var ds = new instance.web.DataSet(this, 'web.example', context);
ds.call('button_function', [ids]).done(function(r) {
    //process the value returned from 'button_function' as per your requirement.
})

}

Avatar
Discard