Skip to Content
Menu
This question has been flagged
3 Replies
27694 Views

Is it posssible to apply a domain only when some condition is true otherwise the domain should not be applied in V7

For eg. in a many2one field to hr.employee, the manager should be able to see only the employee under him but someone from the hr department should be able to view all the employees in the view.

Avatar
Discard
Author

No one know how to do it? is it even possible?

Author Best Answer

I solved the problem I couldn't find a regular way to do what i wanted. I feel this is a bit hackish way to achieve what i wanted; what i did was override the fields-view_get method and implemented what i wanted in that. here's a sample code of what i did, maybe it'll help other looking to implement similar functionality

def fields_view_get(self, cr, user, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):

    """
    Overrides orm field_view_get.
    @return: Dictionary of Fields, arch and toolbar.
    """

    res = {}
    res = super(employee_exit, self).fields_view_get(cr, user, view_id, view_type,
                                                   context, toolbar=toolbar, submenu=submenu)

    hr_employee = self.pool.get('hr.employee')
    emp = hr_employee.search(cr, user, [('user_id', '=', user)])[0]
    logged_employee = hr_employee.browse(cr, user, emp)
    from lxml import etree
    doc=etree.XML(res['arch'])
    for node in doc.xpath("//field[@name='target_employee_id']"):
        if not logged_employee.department_id.name == "Human Resources":        
            node.set('domain', "['&','|',('parent_id.user_id', '=', uid),('department_id.manager_id.user_id','=',uid),('user_id','!=',uid)]")
        elif logged_employee.department_id.manager_id.user_id.id==user:
            node.set('domain', "[('user_id','!=',uid)]")
        else:
            node.set('domain', "['&','&',('user_id','!=',uid),('user_id','!=',"+str(logged_employee.department_id.manager_id.user_id.id)+\
                     "),('user_id','!=',"+str(logged_employee.parent_id.user_id.id)+")]")
    res['arch']=etree.tostring(doc)
    return res

EDIT: I can't mark this as the answer as i don't have sufficient points. if somebody finds it useful and doesn't know of any better method please mark this as the answer so it may be visible to others in need at the top

Avatar
Discard
Best Answer

Funny enough, the question and the example provided can have two different answers:

Is it possible to have a a "conditional" domain?

Yes, by building the domain in a smart way, using OR (|) operators. For example, on a particular Model having a company_id field, you could use:

['|',('company_id','child_of',[user.company_id.id]),('company_id','=',False)]

Here the "child of" condition will only be enforced if the record's "company_id" has a value.

The manager should see only his employees, but hr department should see all

To easiest way to achieve this is to have two user Groups. The Managers group has a record rule to view only his employees and the HR manager group has a record rule to see all employees. You can find a similar example in the CRM app: there is a "Sales / See Own Leads" group and a "Sales / See All Leads" group.

Record rules apply to current record based on User attributes. You would need to know the Department to use for the current User, but AFAIK that info is not available in the User model. You would somehow need to have a department_id in the User and then could create a record rule (or many2one domain) similar to:

['|',('department_id','child_of',[user.department_id.id]),(False,'=',user.department_id)]
Avatar
Discard
Author

Thanks, that was educational but i have a many2one field to hr.employee which is being displayed in the view obviously as a dropdown, now what i want is if the logged in user has department the HR then when he uses the drop down he should be able to see all the employees of the other departments and all the employees below him in HR Department, but if the logged in user is a not of the HR Department and uses the dropdown he should be able to see only the employees he the parent of. Also please explain in the above example which company_id do you mean the user.company_id or 'company_id'

Expanded the answer. The feature you need would ge a great addition to lp:openerp-hrms.

Author

So in short its not posssible to get exactly what i need with exisiting functionality?

res.user doesnt have any field like department_id.... how is it possible to use this?....i have got error.....

Best Answer

Unfortunately, you cannot add the same fieldname twice with an "invisible" condition and different domains (only the last fields domain has effect).

A domain like 

"(False,'=',user.department_id)"

 will lead to 

"ValueError: Invalid leaf (False, '=', False)"

To use different domains, you can use multiple fields, where the latter ones are related to the first.

parent_target_employee_id = fields.Many2one(
related="target_employee_id",
domain=[...],
)
# or add the domain in the view only

While I like this solution the most, it has a side effects: the write method will be called multiple times. This is at least how Odoo 14 works: the related keyword causes a "write" call on the target, which is "self" in those cases. This can be ignored for simple models, but if the write method performs a lot of checks and is not side effect free, the approach to override _fields_view_get is a more performant solution.


Avatar
Discard
Related Posts Replies Views Activity
2
Dec 20
4660
1
Aug 15
5402
5
May 24
24293
1
May 24
5310
0
Jan 24
935