Allow customizing context per model for rule

This commit is contained in:
Cédric Krier
2023-09-24 13:59:00 +02:00
parent d2908701c6
commit d87effab7b
3 changed files with 13 additions and 11 deletions
+4 -4
View File
@@ -54,8 +54,8 @@ class Rule(metaclass=PoolMeta):
'\n- "companies" as list of ids from the current user')
@classmethod
def _get_cache_key(cls):
key = super(Rule, cls)._get_cache_key()
def _get_cache_key(cls, model_name):
key = super()._get_cache_key(model_name)
# XXX Use company from context instead of browse to prevent infinite
# loop, but the cache is cleared when User is written.
context = Transaction().context
@@ -66,11 +66,11 @@ class Rule(metaclass=PoolMeta):
)
@classmethod
def _get_context(cls):
def _get_context(cls, model_name):
pool = Pool()
User = pool.get('res.user')
Employee = pool.get('company.employee')
context = super()._get_context()
context = super()._get_context(model_name)
# Use root to avoid infinite loop when accessing user attributes
user_id = Transaction().user
with Transaction().set_user(0):
+1
View File
@@ -1,3 +1,4 @@
* Allow customizing context per model for rule
* Use multiple jobs to dump and restore a test database
* Support database cache as template for tests
* Add route test case
+8 -7
View File
@@ -132,8 +132,8 @@ class Rule(ModelSQL, ModelView):
def check_domain(cls, rules, field_names=None):
if field_names and 'domain' not in field_names:
return
ctx = cls._get_context()
for rule in rules:
ctx = cls._get_context(rule.rule_group.model.model)
try:
value = PYSONDecoder(ctx).decode(rule.domain)
except Exception:
@@ -149,8 +149,8 @@ class Rule(ModelSQL, ModelView):
raise DomainError(gettext(
'ir.msg_rule_invalid_domain', name=rule.rec_name))
@staticmethod
def _get_context():
@classmethod
def _get_context(cls, model_name):
User = Pool().get('res.user')
transaction = Transaction()
user_id = transaction.user
@@ -163,8 +163,8 @@ class Rule(ModelSQL, ModelView):
'groups': User.get_groups()
}
@staticmethod
def _get_cache_key():
@classmethod
def _get_cache_key(cls, model_name):
# _datetime value will be added to the domain
return (Transaction().user, Transaction().context.get('_datetime'))
@@ -243,10 +243,11 @@ class Rule(ModelSQL, ModelView):
clause = defaultdict(lambda: ['OR'])
clause_global = defaultdict(lambda: ['OR'])
decoder = PYSONDecoder(cls._get_context())
# Use root user without context to prevent recursion
with transaction.set_user(0), transaction.set_context(user=0):
for rule in cls.browse(ids):
decoder = PYSONDecoder(
cls._get_context(rule.rule_group.model.model))
assert rule.domain, ('Rule domain empty,'
'check if migration was done')
dom = decoder.decode(rule.domain)
@@ -276,7 +277,7 @@ class Rule(ModelSQL, ModelView):
assert mode in cls.modes
key = (model_name, mode) + cls._get_cache_key()
key = (model_name, mode) + cls._get_cache_key(model_name)
domain = cls._domain_get_cache.get(key, False)
if domain is not False:
return domain