Odoo Help

0

0
4 Answers
0
Jesús Marco
20‏/3‏/2018 8:35 م

Hi Sehrish, Kabeer thanks for the tips, although I think I´m getting closer, I still can´t make it work, I went coding with method 2 (Sehrish) using it inside a computed field but several exceptions have been occurring:

field:

'make_invisible':fields.function(get_user, string="Is Invisible", readonly=0)

XML:

<field name="make_invisible"/>

First try:

def get_user(self, cr, uid, ids, name, arg, context=None):   
    _logger.debug("This is GET_USER method ") 
    desired_group_name = self.env['res.groups'].search([('name','=','GM')]) 
    is_desired_group = self.env.user.id in desired_group_name.users.ids 
    self.make_visible=is_desired_group

got:

desired_group_name = self.env['res.groups'].search([('name','=','GM')]) 
AttributeError: 'hr.employee' object has no attribute 'env'

Second try: (try to use api.multi to avoid previous error)

@api.multi 
def get_user(self, cr, uid, ids, name, arg, context=None):
    _logger.debug("This is GET_USER method ")
    desired_group_name = self.env['res.groups'].search([('name','=','GM')])
    is_desired_group = self.env.user.id in desired_group_name.users.ids 
    self.make_visible=is_desired_group

got:

File "/opt/odoo/openerp/api.py", line 363, in old_api 
    result = method(recs, *args, **kwargs)
TypeError: get_user() takes at least 6 arguments (4 given)

Third try: (use different function definition with api.multi)

@api.multi 
def get_user(self):
    _logger.debug("This is GET_USER method ")
    desired_group_name = self.env['res.groups'].search([('name','=','GM')])
    is_desired_group = self.env.user.id in desired_group_name.users.ids 
    self.make_visible=is_desired_group

got:

File "/opt/odoo/openerp/api.py", line 709, in __new__  
    self.cr, self.uid, self.context = self.args = (cr, uid, frozendict(context)) 
ValueError: dictionary update sequence element #0 has length 1; 2 is required

Fouth try: (tried to use original function definition with pool.get instead of env)

def get_user(self, cr, uid, ids, name, arg, context=None):  
    _logger.debug("This is GET_USER method ")
    #desired_group_name = self.env['res.groups'].search([('name','=','GM')])
    desired_group_name = self.pool.get('res.groups').search([('name','=','GM')])
    is_desired_group = self.env.user.id in desired_group_name.users.ids 
    self.make_visible=is_desired_group

got:

File "/opt/odoo/openerp/api.py", line 241, in wrapper  
    return old_api(self, *args, **kwargs) 
TypeError: search() takes at least 4 arguments (2 given)

Please help, dont know what else to do and what am I doing wrong!!!

0
Sehrish
20‏/3‏/2018 2:55 ص

How to check login user group. The need of this post is, to sometime we need to visible invisible some filed on the basis of login user group or we want to perform some action on the basis of login user. To do so we need to get login user group. There are two ways

Method 1:

user = self.env['res.users'].sudo().search([('login','=',self.env.user.login)]) 
desired_group_user = self.env['res.groups'].sudo().search([('name','=','desired_group_name')]) 
query = "select gid from res_groups_users_rel where gid ={} and uid={}".format(desired_group_user.id,user.id)
self.env.cr.execute(query) is_desired_group = self.env.cr.fetchone() 
desired_user_gr = self.env['res.groups'].sudo().search([('id','=',is_desired_group)])

Method 2:

desired_group_name = self.env['res.groups'].search([('name','=','desired_group_name')])
is_desired_group = self.env.user.id in desired_group_name.users.ids

For further info about code and description visit: http://learnopenerp.blogspot.com/2017/10/how-to-check-login-user-group-in-odoo.html

This is the answer of your first part of question. If you want to visible invisible fields on some condition read: http://learnopenerp.blogspot.com/2016/10/how-to-visible-and-invisible-fields-in.html

1 Comment
Sehrish
20‏/3‏/2018 2:57 ص
Ask a Question
Writer
Keep Informed
1 follower(s)
About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

Register
Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now