Test Context with tryton console?

how can i set tryton context in console ?

    pool = Pool()
    User = pool.get('res.user')
    Employee = pool.get('company.employee')
    with Transaction().set_user(0):`
        user = User(Transaction().user)
    with Transaction().set_context(_check_access=False, _datetime=None):
        context['employee'] = EvalEnvironment(Employee(user.employee.party.id), Employee)

but seems like context is not defined . if i am correct the context get automatically get define with 'with statement’


I tested your code and I think that it is defining the context correctly.
Inside the first with Transaction().context is returning {} and in the second one: {'_check_access': False, '_datetime': None}

Is your console not returning the same? Are you expecting to get something different?

In tryton console you did not have the user context set. You have to retrieve it from the user preferences.

Here is a sample code to get the context of admin (id=1) user:

User = pool.get('res.user')
with transaction.set_user(1):
    context = User.get_preferences()

with transaction.set_context(context):

Hope this helps

1 Like

i just tried the code in trytond console seems like now i can’t login in as admin i am getting error context is not defined

Could you please share the full traceback of the error?

    Traceback (most recent call last):
      File "/usr/local/lib/python37/dist-packages/trytond/wsgipy", line 110, in dispatch_request
        selfcheck_request_size(request, max_request_size)
      File "/usr/local/lib/python37/dist-packages/trytond/wsgipy", line 90, in check_request_size
        if requestuser_id:
      File "/werkzeug/utilspy", line 73, in __get__
        value = selffunc(obj)
      File "/usr/local/lib/python37/dist-packages/trytond/protocols/wrapperspy", line 73, in user_id
      File "/usr/local/lib/python37/dist-packages/trytond/securitypy", line 85, in check
        find = Sessioncheck(user, session)
      File "/usr/local/lib/python37/dist-packages/trytond/ir/sessionpy", line 77, in check
        domain or [],
      File "/usr/local/lib/python37/dist-packages/trytond/model/modelsqlpy", line 1300, in search
        domain = Ruledomain_get(cls__name__, mode='read')
      File "/usr/local/lib/python37/dist-packages/trytond/ir/rulepy", line 244, in domain_get
        clause, clause_global = clsget(model_name, mode=mode)
      File "/usr/local/lib/python37/dist-packages/trytond/ir/rulepy", line 209, in get
        decoder = PYSONDecoder(cls_get_context())
      File "/usr/local/lib/python37/dist-packages/trytond/modules/company/irpy", line 82, in _get_context
        return context
    NameError: name 'context' is not defined

Here it is
same occurs on login

i was able to sort that out it was related to my code in ir.py in company module. but how can i get the relation in between user and employee for example using context

thanks a lot this solved my issue :innocent:

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