Skip to Content
Menu
This question has been flagged

Please help me, I need to filter leave requests by department depending on the department of the user logged in.

I tried simple Print inside the method it is working but it doesn't open the action window

Please tell me what's wrong or if there's easier way to do it. Thank you so much in advance.

Here's my code: 

Py:

    @api.multi

    def leave_filter_act(self):

        hr_emp = self.env['hr.employee'].search([('user_id', '=', self.env.user.id)])

       print hr_emp.department_id.id  #this is working

        return {

                'name': _("Leave Approvals"),

                'type': 'ir.actions.act_window',

                'res_model': 'hr.holidays',

                'view_mode': 'tree,form',

                'view_type': 'form',

                'views': [[False, 'tree'],[False, 'form'],],

                'context': {'search_default_department_id': [hr_emp.department_id.id]},

                'target': 'current',

            }

XML:

<record id="action_server_leave_filter" model="ir.actions.server">

          <field name="name">Leave Filter</field>

          <field name="model_id" ref="model_hr_holidays"/>

          <field name="code">env.get('hr.holidays').leave_filter_act()</field>

 </record>


 <menuitem

            id="menu_open_department_leave_approve_offcr"

            name="My Department"

            parent="hr_holidays.menu_hr_holidays_approvals"

            action="action_server_leave_filter"/>


Avatar
Discard

Show records on TreeView that are related to Active User: https://goo.gl/hv4WAi

Best Answer

Hello  Deb Joy

replace your 'action_server_leave_filter' record with this one

<record id="action_server_leave_filter" model="ir.actions.server">

          <field name="name">Leave Filter</field>

          <field name="model_id" ref="model_hr_holidays"/>

          <field name="state">code</field>

          <field name="code">action =  env.get('hr.holidays').leave_filter_act()</field>

 </record>


I hope it's work fine in your case.

Avatar
Discard
Author

It worked! Thank you so much :)

Best Answer

hello into python you can do like

def leave_filter_act(self):
    action = self.env.ref('module_name.action_server_leave_filter').read()[0]   
 return action
Avatar
Discard
Best Answer

Hi, I have slightly similar case. I am trying to call a method that calls a google maps report produced in python because with qweb was too difficult. 

<record id="action_map_report" model="ir.actions.server">
<field name="name">Reporte Google Maps</field>
    <field name="condition">True</field>
    <field name="type">ir.actions.server</field>
 <field name="model_id" ref="model_operaciones_servicios"/>
    <field name="state">code</field>
    <field name="code">action=self.generar_reporte_gmaps_servicios(cr,uid,context.get('active_ids'), context=context)</field>
</record>
<record model="ir.values" id="ir_operaciones_servicios_map_report">
    <field name="key2">client_action_multi</field>
    <field name="name">Analisis Recorrido en Google Maps</field>
    <field name="value" eval="'ir.actions.act_window,' + str(ref('operaciones.action_map_report'))" />
    <field name="key">action</field>
    <field name="model">operaciones.servicios</field>
 </record>

Here is the method that I am calling that is in the operaciones.servicios class:

@api.model
def generar_reporte_gmaps_servicios(self):
    fecha1 = self.fecha_reporte[:8] + '01'
    return {
        "type": "ir.actions.act_url",
        "url": "/web/operaciones/gmaps/servicios?ids=%s" % (self.ids),
        "target": "self",
        }

When a load the operaciones.servicios form returns this error:

Traceback (most recent call last):
  File "/home/odoo/odoo8/openerp/http.py", line 518, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "/home/odoo/odoo8/openerp/http.py", line 539, in dispatch
    result = self._call_function(**self.params)
  File "/home/odoo/odoo8/openerp/http.py", line 295, in _call_function
    return checked_call(self.db, *args, **kwargs)
  File "/home/odoo/odoo8/openerp/service/model.py", line 113, in wrapper
    return f(dbname, *args, **kwargs)
  File "/home/odoo/odoo8/openerp/http.py", line 292, in checked_call
    return self.endpoint(*a, **kw)
  File "/home/odoo/odoo8/openerp/http.py", line 755, in __call__
    return self.method(*args, **kw)
  File "/home/odoo/odoo8/openerp/http.py", line 388, in response_wrap
    response = f(*args, **kw)
  File "/home/odoo/odoo8/openerp/addons/web/controllers/main.py", line 949, in call_kw
    return self._call_kw(model, method, args, kwargs)
  File "/home/odoo/odoo8/openerp/addons/web/controllers/main.py", line 941, in _call_kw
    return getattr(request.registry.get(model), method)(request.cr, request.uid, *args, **kwargs)
  File "/home/odoo/odoo8/openerp/api.py", line 238, in wrapper
    return old_api(self, *args, **kwargs)
  File "/home/odoo/odoo8/openerp/addons/mail/mail_thread.py", line 344, in fields_view_get
    res = super(mail_thread, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=submenu)
  File "/home/odoo/odoo8/openerp/api.py", line 238, in wrapper
    return old_api(self, *args, **kwargs)
  File "/home/odoo/odoo8/openerp/models.py", line 1570, in fields_view_get
    resaction = ir_values_obj.get(cr, uid, 'action', 'client_action_multi', [(self._name, False)], False, context)
  File "/home/odoo/odoo8/openerp/api.py", line 238, in wrapper
    return old_api(self, *args, **kwargs)
  File "/home/odoo/odoo8/openerp/addons/calendar/calendar.py", line 620, in get
    meta, context, res_id_req, without_user, key2_req)
  File "/home/odoo/odoo8/openerp/api.py", line 238, in wrapper
    return old_api(self, *args, **kwargs)
  File "/home/odoo/odoo8/openerp/addons/base/ir/ir_values.py", line 501, in get
 return self._map_legacy_model_list(models, do_get, merge_results=True)
  File "/home/odoo/odoo8/openerp/addons/base/ir/ir_values.py", line 458, in _map_legacy_model_list
    result = map_fn(model, res_id)
  File "/home/odoo/odoo8/openerp/addons/base/ir/ir_values.py", line 500, in do_get
    return self.get_actions(cr, uid, action_slot=key2, model=model, res_id=res_id, context=context)
  File "/home/odoo/odoo8/openerp/api.py", line 238, in wrapper
    return old_api(self, *args, **kwargs)
  File "/home/odoo/odoo8/openerp/addons/base/ir/ir_values.py", line 429, in get_actions
    action_def = action_model.read(cr, uid, int(action_id), fields, context)
  File "/home/odoo/odoo8/openerp/api.py", line 238, in wrapper
    return old_api(self, *args, **kwargs)
  File "/home/odoo/odoo8/openerp/addons/base/ir/ir_actions.py", line 349, in read
    return results[0]
IndexError: list index out of range

When I delete the actions the form loads OK. What is wrong please?

Avatar
Discard
Related Posts Replies Views Activity
1
Jul 22
1000
0
Jun 21
1990
0
Apr 24
406
2
Aug 23
1178
2
Apr 24
3873