Check if specific group(s) are in context - Pyson

Using pyson, I can set the states attribute for a field based on the context.

As an example, if the number of groups in context is greater than 2, the following will set a field to be read-only:

description = fields.Char('Description', 
           states={'readonly': Len(Eval('context', {}).get('groups') ) > 2  } )

Now, suppose I want to allow the above field to be read-only if the group with id = 20 is in groups.

I tried using: 'readonly': In(20, Eval('context', {}).get('groups') ) , but that gave the error:

File "C:\Users\user\.virtualenvs\tryton_test\lib\site-packages\trytond\pyson.py", line 485, in __init__
    assert obj.types().issubset({dict, list}), \
AssertionError: obj must be a dict or a list

I also tried: 'readonly': 20.in_(Eval('context', {}).get('groups') ), but that gave the error:

 File "C:\Users\user\.virtualenvs\tryton_test\lib\site-packages\trytond\modules\sale\sale.py", line 137
    states={
    ^
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?

Where am I going wrong?

Hello,

Is there a reason why you cannot just remove write access for this field on this group ?

The type of groups must be a list or a dict. PYSON use the default value (which is an empty string) to determine the type. So you must explicitly say that groups is a list with: Eval('context', {}).get('groups', []).

See:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.