How can we get the login to ir.action.act.window in odoo v10?

We are getting the login user in self.env.user , but I want to access the username in ir.action.act.window .

    <record id="act_mail_messages_form_ept_closed" model="ir.actions.act_window">
        <field name="name">Closed</field>
        <field name="res_model">mail.message</field>
        <field name="domain">[('model','=','res.partner'),('res_id','!=',False),('user.company_id','=',company_id)]</field>         
        <field name="context">{'readonly_by_pass': True,'check_domain':True}</field>
        <field name="view_type">form</field>
        <field name="search_view_id" ref="view_message_search"/>
        <field name="view_mode">tree,form</field>
    </record>        

      

By my requirement, I want to filter the data without creating a write rule, due to the fact that we will create a mail.message write rule, then the system will be very slow, because the write rule will be checked for each record system.

I want to filter company messages using the domain in mail.message.

In mail.message I have a company_id field (custom field) and I want to filter the data when the action is called.

Is there an alternative solution for filtering the message without creating an entry rule, or is there any way from where we can access the username in ir.action.act.window ?

+3


source to share


2 answers


if you only need the user id for your domain: use uid

If you want to filter records with a complex domain that cannot be put on an action, you should use ir.actions.server

:

XML:



<record id="action_mail_closed" model="ir.actions.server">
    <field name="name">Closed</field>
                 <!-- here the name of the module containing mail_message model-->
    <field name="model_id" ref="module_name.mail_message"/>
    <field name="state">code</field>
    <field name="code">action = model.open_closed()</field>
    <field eval="True" name="condition"/>
</record>

      

Python:

@api.model
def open_closed(self):
    # here you can filter you records as you want
    records = self.env... search(...)
    search_view_id = self.env.ref('module_name.view_message_search')
    return {
        'name': _('Closed'),
        'type': 'ir.actions.act_window',
        'view_type': 'form',
        'view_mode': 'tree,form',
        'res_model': 'mail.message',
        'search_view_id': search_view_id.id,
        'target': 'current',
        'context': {'readonly_by_pass': True,'check_domain':True},
        # and here show only your records make sure it not empty
        'domain' : [('id', 'in', records.ids)]
        }

      

+1


source


I was in a similar situation where I needed to add a domain in order to only show results in the tree that are relevant to the logged in user. This is what worked for me.

XML

<field name="domain">[('cmp_id','=',uid)]</field>   

      



Python:

cmp_id = fields.Many2one('module.company', related='user_id.company_id')

      

Here, module is a custom module that you created for the company, cmp_id is a Many2one field that is associated with the company id via user_id.

0


source







All Articles