Add Variable to Context

Hi,

I want to add a variable into context e.g. ‘employee_id’ : ‘em0110220’.
So on other class I can access it as below:

context = Transaction().context
employee_id = context.get('employee_id')

But I couldn’t find example.

One more question:

I have a class named Employee and what I want is: when ever a user open the detail page that load Employee data from DB and displayed to the front end.. I want the class to set the context variable as above.

My question: how to trigger context variable creation when Employee loaded? any builtin classmethod which I need to implement to make this class triggers context variable creation on data load?

Regards
Bromo

Normally the employee key is set on the context if the user has set a values in its own preferences.

You may have a look on the company module source which is the one which adds the needed fields on the user model and sets the values on the context. This way the values are stored on the client customer and send back to the server on each request.

Actually this is what I want todo @pokoli :slight_smile:

        if cls.contains_key(domain, 'running_members.member.id') == True:
            seek_user_id = domain[1][2]
            logger.info(f">>>>>>>>> CONTAINS KEY running_members.member.id: {seek_user_id}")
            Transaction().set_context(employee_id = seek_user_id)

And from other class:

            employee_id = Transaction().context.get('employee_id')
            if employee_id == None:
                logger.info(">>>>>>>>>>>>>>>>>>>>>>>> EMPLOYEE ID NOT FOUND")

I can’t seem to add new variable in the context.

Bromo

I don’t know in which version you are working, but the later versions have several keys removed from the context, including employee to make caching work better. Also context should be treated as kind of unsecure because a user can change the context.

The question is what you want to achieve. Most of the time the employee is used to filter out records or check if the employee is in a group etc. If you want that kind of checks, look at for example at modules/timesheet/ir.py · branch/default · Tryton / Tryton · GitLab to see how you can add the employee back into the context, but now on the server and not in the client.

P.S. the bots of the bullshitgenerators are scraping the internet on our costs by exhausting our bandwidth. To fight that several checks are in place which makes the names of the links disappear. The internet is not a fun place to be anymore :cry:

No this employee thing is part of our custom module.. totally not utilizing built-in Tryton or add-in Tryton feature.

I just want to inject a variable called employee_id into context and retrieves and use it on another class. I just need to be able to set it in the context.

Bromo

I’m not entirely clear at what point you are trying to set the context value, and at what point you are using it, however…

Normally set_context is used as a context manager for example: use of set_context in the sale module
Note, the context is only set for the duration of the with block, and Model’s objects from outside the with block may have have a different context to the one you are expecting as I think they get their context when they are instanciated (although you can rebrowse them to reinstanciate them with the current context).

Something else you may want to take a quick look at, if you want the context to be set for related records, is the field’s context property, e.g.: Use of context for a field in the sale module

Also it is possible to use a context model to set the context for a window (but I don’t think this is what you are looking for): e.g.: use of context model in the sale module

2 Likes