Skip to Content
Menu
This question has been flagged
18 Replies
21331 Views

Hi !

I basically try to invoke a function from within a Controller that does exactly what I want. But, I can't reach it, neither by importing this controller, nor by using it's route.

To use the function directly, I need an instance of this controller.

To use the route method, I tried using Requests (Python) and a simple get on the route. However, I need the session_id in the headers. And I can't retrieve that info within the Model function, where I am.

I tried with request.session_id, but I got this error:

Traceback (most recent call last):
File "/home/odoo/server/openerp/service/server.py", line 929, in preload_registries
registry = RegistryManager.new(dbname, update_module=update_module)
File "/home/odoo/server/openerp/modules/registry.py", line 370, in new
openerp.modules.load_modules(registry._db, force_demo, status, update_module)
File "/home/odoo/server/openerp/modules/loading.py", line 351, in load_modules
force, status, report, loaded_modules, update_module)
File "/home/odoo/server/openerp/modules/loading.py", line 255, in load_marked_modules
loaded, processed = load_module_graph(cr, graph, progressdict, report=report, skip_modules=loaded_modules, perform_checks=perform_checks)
File "/home/odoo/server/openerp/modules/loading.py", line 176, in load_module_graph
_load_data(cr, module_name, idref, mode, kind='data')
File "/home/odoo/server/openerp/modules/loading.py", line 118, in _load_data
tools.convert_file(cr, module_name, filename, idref, mode, noupdate, kind, report)
File "/home/odoo/server/openerp/tools/convert.py", line 901, in convert_file
convert_xml_import(cr, module, fp, idref, mode, noupdate, report)
File "/home/odoo/server/openerp/tools/convert.py", line 987, in convert_xml_import
obj.parse(doc.getroot(), mode=mode)
File "/home/odoo/server/openerp/tools/convert.py", line 853, in parse
self._tags[rec.tag](self.cr, rec, n, mode=mode)
File "/home/odoo/server/openerp/tools/convert.py", line 379, in _tag_function
_eval_xml(self,rec, self.pool, cr, uid, self.idref, context=context)
File "/home/odoo/server/openerp/tools/convert.py", line 230, in _eval_xml
res = getattr(model, method)(cr, uid, *args)
File "/home/odoo/server/openerp/api.py", line 241, in wrapper
return old_api(self, *args, **kwargs)
File "/home/odoo/server/openerp/api.py", line 336, in old_api
result = method(recs, *args, **kwargs)
File "/home/odoo/my_addons/vimexcom/models/company.py", line 62, in set_params
headers = {'cookie': 'session_id=%s' % request.session_id}
File "/usr/lib/python2.7/dist-packages/werkzeug/local.py", line 336, in __getattr__
return getattr(self._get_current_object(), name)
File "/usr/lib/python2.7/dist-packages/werkzeug/local.py", line 295, in _get_current_object
return self.__local()
File "/usr/lib/python2.7/dist-packages/werkzeug/local.py", line 131, in _lookup
raise RuntimeError('object unbound')
ParseError: "object unbound" while parsing /home/odoo/my_addons/vimexcom/data/data.xml:10, near
<function model="res.company" name="set_params"/>

Any tips would be appreciated.

Thanks !

Avatar
Discard
Best Answer

if it may help you, then you can get session id as follows:

from openerp import http


session_id = http.request.session.sid


UPDATE:

Controllers are registering itself in the openerp.http.controllers_per_module dict (see code)... so you can get controller instance using module name and controller name under it:

from openerp.http import controllers_per_module

controller_instance = None
for name,instance in controllers_per_module.get('my_module'): if name.endswith('my_module.controller.name'): controller_instance = instance break if controller_instance != None: controller_instance.a_function(*args)




Avatar
Discard
Author

I tried that, but I got an error (can't remember the error).

Author

Updated my question.

http.request.session.sid gives you a session id string.

Author

Same error with request.session.sid

ok. it seems session id does not helps

Author

thanks anyway, I'll find a workaround one day, I'm sure ^^

you mention direct call, what is problem with it?

Author

Oh, yeah, if I import the controller (from addons/website/controllers/main import Website) and use it (let's say ... Website.some_function(*args)), I get a "unbound method blablabla must be called with Website instance as first argument ..." But, hey, where do I find that instance ? Can I create one ?

actually you can create, if you'll have all arguments to "constructor" if required, if no such requirement then it may be as simple as:

Website().some_function(*args)), 
Author

Oh yeah, I'm so stupid. And I pretend to be a developper ? Thanks, I'll try that on Monday ! Good we !

for ordinary python object it works, I don't know if it'll work for a controller without side effects :) Good we!

As I doubted it has side effect to create controller instance, it registering itself in a global dict.. and you can get existing instance from there... see updated answer.

Author

Ok, so far, that's an amazing answer ! But, what if the controller is not registered ? Can I safely instanciate one ?

Normally it should be registered. as we can see in the code, all direct children of a "Controller" (i.e. "http.Controller") class are registered. So, if a controller class you're interested in is a child of the Controller class, then it's surely registered there. you'll need just figure out it's full name under it's own module in order to find it in the "controllers_per_module". Try to print or log the "controllers_per_module" variable and check if it's a case. Most probably it'll be registered. if not, then you can try to instantiate one and check if it's a safe operation...

Author

Ok, no, nothing works, and I think it's because my method is called on module update, so, maybe not all the controllers are instanciated at this moment (the Website controller specifically). But, hey, your answer is the right one I think, and I'll accept that. And, for the record, I just copy/pasted/shortened the content of the method I wanted to call so... problem solved.

Best Answer

Maybe you need to call your function in another moment, like after the models registry(pool) is loaded. You could do this by implementing the method _register_hook in your model. Odoo will always call that method in your model to allow you to initialize whatever you want, but you just have the self and cr arguments.

Maybe helps

Avatar
Discard
Related Posts Replies Views Activity
3
Oct 15
10727
2
Jul 24
837
4
Oct 20
4314
2
Mar 16
3259
4
Oct 21
35813