From 4eb3c5d0d061437473abce11c037b5ab282ce454 Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Sat, 12 Dec 2020 10:46:58 +0100
Subject: [PATCH 2/3] formdata: hold is_for_current_user function (#38254)

---
 wcs/formdata.py            | 17 +++++++++++++++++
 wcs/forms/common.py        |  2 +-
 wcs/wf/register_comment.py | 18 +-----------------
 wcs/workflows.py           | 18 +-----------------
 4 files changed, 20 insertions(+), 35 deletions(-)

diff --git a/wcs/formdata.py b/wcs/formdata.py
index 96643958..5d1b613a 100644
--- a/wcs/formdata.py
+++ b/wcs/formdata.py
@@ -909,16 +909,33 @@ class FormData(StorableObject):
 
     def is_submitter(self, user):
         if self.user_id and user and str(self.user_id) == str(user.id):
             return True
         if get_session() and get_session().is_anonymous_submitter(self):
             return True
         return False
 
+    def is_for_current_user(self, to):
+        from wcs.workflows import get_role_translation
+        if not to:
+            return True
+        if not get_request():
+            return False
+        user = get_request().user
+        for role in to or []:
+            if role == '_submitter':
+                if self.is_submitter(user):
+                    return True
+            elif user:
+                role = get_role_translation(self, role)
+                if role in user.get_roles():
+                    return True
+        return False
+
     def is_draft(self, status=None):
         if status is None:
             status = self.status
         return status == 'draft'
 
     def get_concerned_roles(self):
         if self.is_draft():
             # drafts are only visible to submitter
diff --git a/wcs/forms/common.py b/wcs/forms/common.py
index 054f2c77..659e11ae 100644
--- a/wcs/forms/common.py
+++ b/wcs/forms/common.py
@@ -347,17 +347,17 @@ class FormStatusPage(Directory, FormTemplateMixin):
             if form.has_errors():
                 return
             url = self.submit(form)
             if url is None:
                 url = get_request().get_frontoffice_url()
                 status = self.filled.get_status()
                 top_alert = False
                 for item in status.items or []:
-                    if item.key == 'displaymsg' and item.position == 'top' and item.is_for_current_user(self.filled):
+                    if item.key == 'displaymsg' and item.position == 'top' and self.filled.is_for_current_user(item.to):
                         top_alert = True
                         break
                 if top_alert:
                     # prevent an existing anchor client side to take effect
                     url += '#'
                 else:
                     url += '#action-zone'
             response = get_response()
diff --git a/wcs/wf/register_comment.py b/wcs/wf/register_comment.py
index 36bddaa5..27e080f1 100644
--- a/wcs/wf/register_comment.py
+++ b/wcs/wf/register_comment.py
@@ -48,34 +48,18 @@ class JournalEvolutionPart: #pylint: disable=C1001
             # treat it as html, escape strings from ezt variables
             self.content = template_on_formdata(formdata, message,
                     ezt_format=ezt.FORMAT_HTML)
             return
 
         # treat is as text/plain
         self.content = template_on_formdata(formdata, message)
 
-    def is_for_current_user(self, filled):
-        if not self.to:
-            return True
-        if not get_request():
-            return False
-        user = get_request().user
-        for role in self.to or []:
-            if role == '_submitter':
-                if filled.is_submitter(user):
-                    return True
-            elif user:
-                role = get_role_translation(filled, role)
-                if role in user.get_roles():
-                    return True
-        return False
-
     def view(self, filled):
-        if not (self.content and self.is_for_current_user(filled)):
+        if not (self.content and filled.is_for_current_user(self.to)):
             return ''
         if self.content.startswith('<'):
             return htmltext(self.content)
         else:
             # use empty lines to mark paragraphs
             return htmltext('<p>') + \
                    htmltext('\n').join(
                             [(x or htmltext('</p><p>')) for x in self.content.splitlines()]) + \
diff --git a/wcs/workflows.py b/wcs/workflows.py
index 97504343..da72074c 100644
--- a/wcs/workflows.py
+++ b/wcs/workflows.py
@@ -2898,34 +2898,18 @@ class DisplayMessageWorkflowStatusItem(WorkflowStatusItem):
         elif self.position == 'bottom':
             parts.append(_('bottom of page'))
         elif self.position == 'actions':
             parts.append(_('with actions'))
         if self.to:
             parts.append(_('for %s') % self.render_list_of_roles(self.to))
         return ', '.join(parts)
 
-    def is_for_current_user(self, filled):
-        if not self.to:
-            return True
-        if not get_request():
-            return False
-        user = get_request().user
-        for role in self.to or []:
-            if role == '_submitter':
-                if filled.is_submitter(user):
-                    return True
-            elif user:
-                role = get_role_translation(filled, role)
-                if role in user.get_roles():
-                    return True
-        return False
-
     def get_message(self, filled, position='top'):
-        if not (self.message and self.position == position and self.is_for_current_user(filled)):
+        if not (self.message and self.position == position and filled.is_for_current_user(self.to)):
             return ''
 
         dict = copy.copy(get_publisher().substitutions.get_context_variables('lazy'))
         dict['date'] = misc.localstrftime(filled.receipt_time)
         dict['number'] = filled.id
         handling_role = filled.get_handling_role()
         if handling_role and handling_role.details:
             dict['receiver'] = handling_role.details.replace('\n', '<br />')
-- 
2.29.2

