Tuesday 25 March 2014

Work with web-controller OpenERP



How to override web controller in openerp 8.0.

Here, i try to define one example that may be useful to you to understand how to work with web controller in openerp 8.0.

Existing controller (addons/web/controllers/main.py) :
- class name Home, and method index

class Home(http.Controller):

    @http.route('/', type='http', auth="none")
    def index(self, s_action=None, db=None, **kw):
        return http.local_redirect('/web', query=request.params, keep_hash=True)

Now, here i define how to override existing controller...
 - Here, my class name Website, and in parameter i pass path of controller which i want to inherit, so here "Home" class which is in addons/web/controllers/main.py so in path i pass : openerp.addons.web.controllers.main.Home.

 - @http.route : in openerp 8.0 the route system changed.
           '/' : '/' route will match the new index() method in Website.
        'type' : There are different type like http,json...
                 (http : normal http requests by passing, Json : Methods that received JSON can be defined by passing 'json')

class Website(openerp.addons.web.controllers.main.Home):
    @http.route('/', type='http', auth="public", website=True, multilang=True)
    def index(self, **kw):
        try:
            main_menu = request.registry['ir.model.data'].get_object(request.cr, request.uid, 'website', 'main_menu')
            first_menu = main_menu.child_id and main_menu.child_id[0]
            # Dont 302 loop on /
            if first_menu and not ((first_menu.url == '/') or first_menu.url.startswith('/#') or first_menu.url.startswith('/?')):
                return request.redirect(first_menu.url)
        except:
            pass
        return self.page("website.homepage")

9 comments:

  1. Hy,

    I'm struggling with web module overriding.
    I would like to patch this : https://bugs.launchpad.net/openerp-web/+bug/1175163
    So I tried to override the saveas_ajax method.
    I created a module with a folder called controllers.
    Inside I have a .py like this :
    import openerp
    from openerp.addons.web import http
    openerpweb = http

    class export_filenames(openerpweb.Controller):
    _cp_path = "/web/binary"

    @openerpweb.httprequest
    def saveas_ajax(self, req, data, token):
    print("patch OK")
    .../...

    Unfortunately, even if it doesn't failed at the startup, I never pass by this method.
    What could I miss?

    regards

    ReplyDelete
  2. Pinakin,
    Exactly where do you enter this code. In which file?

    ReplyDelete
  3. I have same problem.
    Somebody found a solution?

    ReplyDelete
  4. Nice post that helpful for me ..!!!

    ReplyDelete
  5. Very useful ... thanks Pinakin. !!!

    ReplyDelete
  6. Thank you for sharing this code with us. It is very helpful. Keep sharing. Odoo Integration

    ReplyDelete
  7. Thank you for providing us with this information about crm development. It is extremely beneficial. Continue to share.

    ReplyDelete