Skip to Content
Menu
This question has been flagged
1 Reply
11829 Views

I'm trying to get currently logged in user id similar to the usage of 

<field name = "domain_force"> [('manager.id', 'in', user.id)] </field>

I want to use it like this
attrs = "{'invisible': [('state', 'in', ['draft', 'approved', 'rejected']), ('manager.id', 'in', user.id)]} "
The above code is invalid as I cannot use it to get the current user so as to make it invisible.
It works in the case of domain but not in the case of attrs. 


Avatar
Discard
Best Answer

UID In attrs are not supported. Instead of using the uid field in attrs you can use the computed field.

Solutions.
1. You can use a compute field to update the current user id and compare it in attrs

manager_id = fields.Integer(compute='get_current_uid', string='Manager ID Computed')

def get_current_uid(self):
"""

:param self:
:return:
"""
if self.env.context.get('uid', False):
self.manager_id = self.env.context.get('uid', False)
else:
self.manager_id = False
​attrs = "{'invisible': [('state', 'in', ['draft', 'approved', 'rejected']), ('manager.id', '=', computed_field)]}"


2. In ODOO this function(overrided and updated) allows getting uid in attrs.


# Don't deal with groups, it is done by check_group().
# Need the context to evaluate the invisible attribute on tree views.
# For non-tree views, the context shouldn't be given.
def transfer_node_to_modifiers(node, modifiers, context=None, in_tree_view=False):
if node.get('attrs'):
if 'uid' in node.get('attrs'):
if context.get('uid', False):
user_id = str(context.get('uid', False))
attrs = node.get('attrs')
node.set('attrs', attrs.replace('uid', user_id))
else:
user_id = '1'
attrs = node.get('attrs')
node.set('attrs', attrs.replace('uid', user_id))

modifiers.update(eval(node.get('attrs')))
if node.get('states'):
if 'invisible' in modifiers and isinstance(modifiers['invisible'], list):
# TODO combine with AND or OR, use implicit AND for now.
modifiers['invisible'].append(('state', 'not in', node.get('states').split(',')))
else:
modifiers['invisible'] = [('state', 'not in', node.get('states').split(','))]

for a in ('invisible', 'readonly', 'required'):
if node.get(a):
v = bool(eval(node.get(a), {'context': context or {}}))
if in_tree_view and a == 'invisible':
# Invisible in a tree view has a specific meaning, make it a
# new key in the modifiers attribute.
modifiers['tree_invisible'] = v
elif v or (a not in modifiers or not isinstance(modifiers[a], list)):
# Don't set the attribute to False if a dynamic value was
# provided (i.e. a domain from attrs or states).
modifiers[a] = v



Avatar
Discard
Author

This was really helpful. Thank you Hilar.

Related Posts Replies Views Activity
1
Oct 20
2350
1
Jan 24
13851
3
Aug 22
2404
2
Dec 21
4531
6
Aug 20
6104