changeset 46940aee60d4 in modules/company:default
details: https://hg.tryton.org/modules/company?cmd=changeset;node=46940aee60d4
description:
        Add employee to rule context

        issue8825
        review252621002
diffstat:

 CHANGELOG |   1 +
 ir.py     |  24 +++++++++++++++++++++++-
 2 files changed, 24 insertions(+), 1 deletions(-)

diffs (56 lines):

diff -r 759189e8c1cb -r 46940aee60d4 CHANGELOG
--- a/CHANGELOG Fri Dec 06 17:33:46 2019 +0100
+++ b/CHANGELOG Sat Dec 07 15:15:31 2019 +0100
@@ -1,3 +1,4 @@
+* Add employee to rule context
 * Add company and employee administrator group
 * Add top level menu entry
 
diff -r 759189e8c1cb -r 46940aee60d4 ir.py
--- a/ir.py     Fri Dec 06 17:33:46 2019 +0100
+++ b/ir.py     Sat Dec 07 15:15:31 2019 +0100
@@ -5,7 +5,8 @@
 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
@@ -52,6 +53,11 @@
     __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()
         # XXX Use company from context instead of browse to prevent infinite
@@ -59,6 +65,22 @@
         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"

Reply via email to