Skip to Content
Menu
This question has been flagged
2 Replies
8132 Views

Hi,

right now I'm trying to import a class from another module and use it. The code is as following

import importlib 

cooperative_management = importlib.import_module('\odoo.addons.cooperative-management')
class ApiExample(http.Controller):    
    @http.route('/oauth-api/oauth-api/', auth='public')    
    def index(self, **kw):        
        try:            
            test = cooperative_management.controllers.quotasManagement() except Exception as e:            

body = {                 'error': {                    'code':500,                    "message":str(e),                    "type": type(e).__name__,                    "filename":__file__,                    "line": e.__traceback__.tb_lineno                }            }           

 return body


If I invoke the previous method, even with try catch, it always returns 500: internal error but doesn't return the custom error from the except.

Avatar
Discard
Best Answer

Hi,

You can do it as follows:

from odoo.addons.mail.controllers.main import MailController

@http.route('/lead/case_mark_won', type='http', auth='user', methods=['GET'])
def crm_lead_case_mark_won(self, res_id, token):
comparison, record, redirect = MailController._check_token_and_record_or_redirect('crm.lead', int(res_id), token)
if comparison and record:
try:
record.action_set_won_rainbowman()
except Exception:
_logger.exception("Could not mark crm.lead as won")
return MailController._redirect_to_messaging()
return redirect

Above sample from crm module.

Thanks

Avatar
Discard
Author

Hi,

Thank you so much for helping

when I try to use the from .. import class, there's an error related to the hyphen ("-") in module name. Do you know how to workaround on this? Do I have to change the module name?

you might missed to import _ ?
from odoo .import _

Author

even importing "_" didn't solve. Well, changed the module name to use "_" instead of hyphen "-", it's now working as you suggested in your first answer.
Next time I should not use non standard characters in odoo/python modules :)

Best Answer
You can import like this: 
from odoo.addons.custom_addon_name.controllers.your_py_file_name import class_of_function_name


Avatar
Discard
Related Posts Replies Views Activity
1
Oct 20
9156
0
Aug 22
2848
2
Jan 25
2253
1
Dec 24
5985
1
Nov 24
2396