Search code examples
pythonpython-requestsodooodoo-12

How to pass a context from a wizard to controller and get the value of that context in controller in Odoo 12?


I have a wizard and I select a department, then I call an action (defined in xml) which calls a controller. I have also set (defined) my custom context and passed the selected department id in the context. I want to pass this context upon calling the action which loads a controller to the controller. In the controller, I need to access the passed context's values. This is the code where I set context in the controller and return an action with context:

@api.multi
def action_select_department(self):
    ctx = dict(self._context)
    if self.department_id:
        self.state = 'display'
        ctx.update({'dep_id': self.department_id.id})
        print('Dept id:::::::::::::::::::', self.department_id.id, ctx)
        return {
            'name': _('Website Next Patient Screen'),
            'type': 'ir.actions.act_url',
            'context': ctx,
            'url': '/next_patient',
            'target': 'self'
        }

And this is the controller code where I want to get the context:

 -*- coding: utf-8 -*-

from odoo import http
from odoo.http import request
from odoo.tools.translate import _


class hms_next_patient_screen(http.Controller):
    @http.route(['/next_patient'], type='http', auth="public", website=True)
    def next_patient(self, **kw):
        ctx = request.env.context.copy()
        dept = http.request.evn['nl_hms_next_patient.next.patiend.screen.wizard']
        print('DEPT:::::::::::::::::::::::::::::::', dept)
        print (">>>>>>>>>>>>>>>", ctx, self, kw)
        app_obj = request.env['hms.appointment']
        one = app_obj.sudo().search([('state', '=', 'in_consultation')], limit=1)
        next = app_obj.sudo().search([('state', '=', 'waiting')])
        two = three = four = app_obj
        if len(next)>=1:
            two = next[0]
        if len(next)>=2:
            three = next[1]
        if len(next)>=3:
            four = next[2]
        return request.render("acs_hms_next_patient_screen.next_patient_view",{'one':one,'two':two,'three':three,'four':four})

The method in wizard works fine and the action is called with loading the controller. But, I can't access the context value in the controller. How can I pass the custom context and access it in the controller?


Solution

  • You don't have to pass the details as context to a controller.You can pass it with the the url. Please try this.

    In the wizard,

     return {
          'name': _('Website Next Patient Screen'),
          'type': 'ir.actions.act_url',
          'url': '/next_patient?dep_id=%s' % self.department_id.id
          'target': 'self'
       }
    

    You can get the details that passed in the url, from the kw argument in controller.

    In Controller,

      department_id = kw.get('dep_id')