From d9c31d85d02eeb26c0481480f017ffdd9423799f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Mon, 3 Aug 2020 17:18:36 +0200
Subject: [PATCH] misc: don't record errors when evaluating roles for pending
 bar (#39096)

---
 wcs/backoffice/management.py | 10 ++++++----
 wcs/conditions.py            |  4 +++-
 wcs/formdata.py              |  4 ++--
 wcs/workflows.py             |  5 +++--
 4 files changed, 14 insertions(+), 9 deletions(-)

diff --git a/wcs/backoffice/management.py b/wcs/backoffice/management.py
index 51042c6e8..3f6b44f53 100644
--- a/wcs/backoffice/management.py
+++ b/wcs/backoffice/management.py
@@ -2666,8 +2666,10 @@ class FormBackOfficeStatusPage(FormStatusPage):
             related_user_forms = getattr(self.filled, 'related_user_forms', None) or []
             user_roles = set(get_request().user.get_roles())
             session = get_session()
+            get_publisher().substitutions.unfeed(lambda x: x is self.filled)
             for user_formdata in related_user_forms:
-                if user_roles.intersection(user_formdata.actions_roles):
+                if user_roles.intersection(user_formdata.get_actions_roles(
+                        condition_kwargs={'record_errors': False})):
                     session.mark_visited_object(user_formdata)
 
         return response
@@ -2975,9 +2977,9 @@ class FormBackOfficeStatusPage(FormStatusPage):
             if test_mode in ('django-condition', 'python-condition'):
                 condition = Condition({
                     'value': form.get_widget(test_mode).parse(),
-                    'type': test_mode.split('-')[0]})
-                condition.log_errors = False
-                condition.record_errors = False
+                    'type': test_mode.split('-')[0]},
+                    log_errors=False,
+                    record_errors=False)
                 try:
                     result = condition.unsafe_evaluate()
                 except Exception as exception:
diff --git a/wcs/conditions.py b/wcs/conditions.py
index 594980f51..49016ab9e 100644
--- a/wcs/conditions.py
+++ b/wcs/conditions.py
@@ -29,12 +29,14 @@ class Condition(object):
     record_errors = True
     log_errors = False
 
-    def __init__(self, condition, context={}):
+    def __init__(self, condition, context={}, record_errors=True, log_errors=False):
         if not condition:
             condition = {}
         self.type = condition.get('type')
         self.value = condition.get('value')
         self.context = context
+        self.log_errors = log_errors
+        self.record_errors = record_errors
 
     def __repr__(self):
         return '<%s (%s) %r>' % (self.__class__.__name__, self.type, self.value)
diff --git a/wcs/formdata.py b/wcs/formdata.py
index ea13e4ffd..f9a617c2a 100644
--- a/wcs/formdata.py
+++ b/wcs/formdata.py
@@ -935,7 +935,7 @@ class FormData(StorableObject):
 
     concerned_roles = property(get_concerned_roles)
 
-    def get_actions_roles(self):
+    def get_actions_roles(self, condition_kwargs=None):
         if self.is_draft():
             return []
 
@@ -949,7 +949,7 @@ class FormData(StorableObject):
             if not hasattr(item, 'by') or not item.by:
                 continue
             with get_publisher().substitutions.temporary_feed(self):
-                if not item.check_condition(self):
+                if not item.check_condition(self, **(condition_kwargs or {})):
                     continue
             for role in item.by:
                 if role == '_submitter':
diff --git a/wcs/workflows.py b/wcs/workflows.py
index 4d4fc6e13..b962b02d6 100644
--- a/wcs/workflows.py
+++ b/wcs/workflows.py
@@ -1844,10 +1844,11 @@ class WorkflowStatusItem(XmlSerialisable):
 
         return False
 
-    def check_condition(self, formdata):
+    def check_condition(self, formdata, record_errors=True, log_errors=False):
         context = {'formdata': formdata, 'status_item': self}
         try:
-            return Condition(self.condition, context).evaluate()
+            return Condition(self.condition, context,
+                    record_errors=record_errors, log_errors=log_errors).evaluate()
         except RuntimeError:
             return False
 
-- 
2.28.0

