Search code examples
pythonlistodooodoo-12

Show/Hide fields if there is not in list


My concept is that I have 5 different access level on Odoo.

I want to check if login user is in list (list is created by an automated action), if yes show the fields else hide them.

My code:

list_of_users= []
if record.user_id.partner_id.id: #level 1 user add on list
  list_of_users.append(record.user_id.partner_id.id)

if record.user_id.sale_team_id.user_id.partner_id.id: #level 2 user add on list
  list_of_users.append(record.user_id.sale_team_id.user_id.partner_id.id)

if record.user_id.sale_team_id.regional_manager_id.partner_id.id: #level 3 user add on list
  list_of_users.append(record.user_id.sale_team_id.regional_manager_id.partner_id.id)

user_ids = record.env['res.users'].search([])
flag = record.env['res.users'].search([('groups_id','ilike','L4')])

for user_ids in flag: #level 4 and 5 users add on list
  user_record = record.env['res.users'].browse(user_ids)
  list_of_users.append(user_ids.partner_id.id)

record.update({'x_partner_follower_custom': [(6, 0, list_of_users)]})

On view i use attrs="{'invisible': [('x_partner_follower_custom', '=', False)]}" inside on fields i want to show/hide

x_partner_follower_custom: many2many res.partner field

Output Sample: enter image description here

As you can see im return users back to view, but it seems the attrs is not properly set. Any idea how to fix it?


Solution

  • First of all the field should not be stored and it must be computed every time because it depends on the value of the current logged user, remove store:

       @api.depends('x_partner_follower_custom')
       def user_exist(self):
            for rec in self:
                 if rec.x_partner_follower_custom and self.env.user.partner_id.id in rec.x_partner_follower_custom.ids:
                     rec.your_boolean_field_name = True
                 else:
                     rec.your_boolean_field_name = False 
    

    So the field depends on x_partner_follower_custom and the current logged user it should not be stored.

    EDITS

    Everything is explained in the Form view and how to make the code works like it was created in the code:

    How to create a compute field using UI

    I manage it to find solution, a couple days ago: @Fotic

    for record in self:
      if record['x_partner_follower_custom'] and self.env.user.partner_id.id in record['x_partner_follower_custom'].ids:
        record['x_partner_is_follower_custom'] = True
      else:
        record['x_partner_is_follower_custom'] = False