PYSON statements in Tryton 5.x

We use the following PYSON statements in states attribute:

Eval('_user') != Eval('create_uid')
Not(In(Id('res','group_admin'), Eval('groups', [])))

They work well in Tryton 4.x, but do not work in Tryton 5.x.
What is wrong with them?

Please provide more information. What is wrong? What is the current behavior and what is the expected?

I guess you were using the implicit user context as field instead of using evaluating explicitly the ‘context’.

This condition determines that the user did not create the record.

This condition determines that the user is not a member of Administration group.

These conditions work in Tryton 4.x, but do not work in Tryton 5.x.
The name ‘_user’ you advised here:!searchin/tryton-dev/Eval(_user)|sort:date/tryton-dev/-LaH1CUAMBs/qCJjBk60Kw8J

Indeed there is no more implicit fallback to context. context must be explicitly evaluated like Eval('context', {}).get('user').

But we do not need context in this case.
These are system names create_uid, user, groups, group_admin, aren’t they?
What about the current user? Do we need to use user name or _user name?

There are no “system names”, only record and context, since Issue 7072: Rationalize context usage in client - Tryton issue tracker

Thus, we have to define a context model for our model only in order to get the values of current user, user groups, etc? Is there another way?

I don’t know if it is the best way to solve this problem but as you already have the current employee in the context so you can create a functional field that returns the current user or the groups the user belongs to depending on the employee and then use this functional field in the PYSON statements

AFAIK what @ced tries to explain is you need to use explicit context on pyson.
As an example, the statement:
should be replace by
Eval('context', {}).get('groups')

1 Like

Functional field will be evaluated for each record.
But the current user is the same for all records.

This statement works, so the “system name” groups still exists. :slightly_smiling_face:

We have not added the name user to the context, so this statement does not work.

It seems the best solution is to add the current user to the user context fields:

class User(metaclass=PoolMeta):
    __name__ = 'res.user'
    user = fields.Function(fields.Integer('User'), 'get_user')

    def __setup__(cls):
        super(User, cls).__setup__()

    def get_user(self, name):
        return Transaction().user

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