From 9818599bbaa74f08c8a3b67d876c81b081a2d7d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sat, 7 Dec 2019 15:15:31 +0100 Subject: [PATCH] Add employee to rule context issue8825 review252621002 --- CHANGELOG | 1 + ir.py | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index b3a9e82d67..5df112b649 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +* Add employee to rule context * Add company and employee administrator group * Add top level menu entry diff --git a/ir.py b/ir.py index 002b949244..febad5df10 100644 --- a/ir.py +++ b/ir.py @@ -5,7 +5,8 @@ try: except ImportError: pytz = None -from trytond.model import ModelSQL, ModelView, fields, dualmethod +from trytond.model import ( + ModelSQL, ModelView, fields, dualmethod, EvalEnvironment) from trytond.pool import PoolMeta, Pool from trytond.pyson import Eval, If from trytond.transaction import Transaction @@ -51,6 +52,11 @@ class Date(metaclass=PoolMeta): class Rule(metaclass=PoolMeta): __name__ = 'ir.rule' + @classmethod + def __setup__(cls): + super().__setup__() + cls.domain.help += '\n- "employee" from the current user' + @classmethod def _get_cache_key(cls): key = super(Rule, cls)._get_cache_key() @@ -59,6 +65,22 @@ class Rule(metaclass=PoolMeta): return key + (Transaction().context.get('company'), Transaction().context.get('employee')) + @classmethod + def _get_context(cls): + pool = Pool() + User = pool.get('res.user') + Employee = pool.get('company.employee') + context = super()._get_context() + # Use root to avoid infinite loop when accessing user attributes + with Transaction().set_user(0): + user = User(Transaction().user) + if user.employee: + with Transaction().set_context( + _check_access=False, _datetime=None): + context['employee'] = EvalEnvironment( + Employee(user.employee.id), Employee) + return context + class Cron(metaclass=PoolMeta): __name__ = "ir.cron"