Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to invoke a Controller function from inside a Model function ?

By
PY
on 7/3/15, 10:36 AM 1,683 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 !

1

Temur

--Temur--
2896
| 6 5 7
Tbilisi, Georgia
--Temur--
Programmer
Temur
On 7/3/15, 11:38 AM

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)




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

PY
on 7/3/15, 11:40 AM

Updated my question.

PY
on 7/3/15, 11:43 AM

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

Temur
on 7/3/15, 11:45 AM

Same error with request.session.sid

PY
on 7/3/15, 11:46 AM

ok. it seems session id does not helps

Temur
on 7/3/15, 11:48 AM

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

PY
on 7/3/15, 11:49 AM

you mention direct call, what is problem with it?

Temur
on 7/3/15, 11:49 AM

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 ?

PY
on 7/3/15, 11:52 AM

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)), 
Temur
on 7/3/15, 11:55 AM

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

PY
on 7/3/15, 11:56 AM

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

Temur
on 7/3/15, 12:01 PM

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.

Temur
on 7/3/15, 12:30 PM

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

PY
on 7/6/15, 3:20 AM

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

Temur
on 7/6/15, 4:03 AM

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.

PY
on 7/6/15, 4:22 AM
1

Axel Mendoza

--Axel Mendoza--
10238
| 7 7 8
Camaguey, Cuba
--Axel Mendoza--

DevOps - Full stack - Software Architect - Developer - Technology Integrator

I could help you to develop anything and solve complex problems based on technologies, integrations and tricky stuffs mostly in Python with OpenERP/Odoo, Zato, Django and many others frameworks programming languages and technologies.

I offers consulting services to anyone with an unanswered questions or needs for customizations. Think about it, maybe it's better to have an expert to solve your issues and projects than having a full time employee trying to understand what to do an how

Reach me at aekroft@gmail.com

Axel Mendoza
On 7/10/15, 4:12 AM

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

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

2 follower(s)

Stats

Asked: 7/3/15, 10:36 AM
Seen: 1683 times
Last updated: 7/10/15, 4:12 AM