From 8a86c89ed3b594e6ab848817584ee05245ba35c7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Mon, 5 Jul 2021 21:15:57 +0200
Subject: [PATCH] workflows: use "everybody" as empty label for message/comment
 actions (#55410)

---
 wcs/qommon/form.py                  | 26 ++++++++++++++++++++++++++
 wcs/qommon/static/js/widget_list.js |  6 ++++++
 wcs/wf/register_comment.py          | 11 ++++-------
 wcs/workflows.py                    | 10 ++++------
 4 files changed, 40 insertions(+), 13 deletions(-)

diff --git a/wcs/qommon/form.py b/wcs/qommon/form.py
index 6ff8d28b0..028b2fb1a 100644
--- a/wcs/qommon/form.py
+++ b/wcs/qommon/form.py
@@ -1759,6 +1759,32 @@ class WidgetList(quixote.form.widget.WidgetList):
         return r.getvalue()
 
 
+class WidgetListOfRoles(WidgetList):
+    def __init__(self, name, value=None, roles=None, **kwargs):
+        self.first_element_empty_label = kwargs.pop('first_element_empty_label', '---')
+        super().__init__(
+            name,
+            value=value,
+            element_type=SingleSelectWidget,
+            element_kwargs={
+                'render_br': False,
+                'options': [(None, '---', None)] + roles or [],
+            },
+            **kwargs,
+        )
+
+    def get_widgets(self):
+        seen = False
+        for widget in super().get_widgets():
+            # change first option (empty value) of first element to use a specific label
+            if not seen and isinstance(widget, SingleSelectWidget):
+                widget.full_options = widget.full_options[:]
+                widget.full_options[0] = list(widget.full_options[0])
+                widget.full_options[0][1] = self.first_element_empty_label
+                seen = True
+            yield widget
+
+
 class WidgetDict(quixote.form.widget.WidgetDict):
     # Fix the title and hint setting
     # FIXME: to be fixed in Quixote upstream : title and hint parameters should be removed
diff --git a/wcs/qommon/static/js/widget_list.js b/wcs/qommon/static/js/widget_list.js
index b22d2aed8..74ad168b6 100644
--- a/wcs/qommon/static/js/widget_list.js
+++ b/wcs/qommon/static/js/widget_list.js
@@ -50,6 +50,12 @@ function prepare_widget_list_elements() {
                         }
                     }
                 );
+                if (name_attr == 'to$add_element') {
+                  /* replace 1st list element by an empty label, as it's used
+                   * to remove an actual selection */
+                  $(new_row).find('option[value="None"]').first().text('----');
+                }
+
                 /* add new row after the last row */
                 $(row).after($(new_row));
 
diff --git a/wcs/wf/register_comment.py b/wcs/wf/register_comment.py
index 07de33595..7f2c5fc35 100644
--- a/wcs/wf/register_comment.py
+++ b/wcs/wf/register_comment.py
@@ -28,7 +28,7 @@ from wcs.workflows import (
 )
 
 from ..qommon import _, ezt, get_logger
-from ..qommon.form import SingleSelectWidget, TextWidget, WidgetList
+from ..qommon.form import TextWidget, WidgetListOfRoles
 from ..qommon.template import TemplateError
 
 
@@ -97,16 +97,13 @@ class RegisterCommenterWorkflowStatusItem(WorkflowStatusItem):
             )
         if 'to' in parameters:
             form.add(
-                WidgetList,
+                WidgetListOfRoles,
                 '%sto' % prefix,
                 title=_('To'),
-                element_type=SingleSelectWidget,
                 value=self.to or [],
                 add_element_label=self.get_add_role_label(),
-                element_kwargs={
-                    'render_br': False,
-                    'options': [(None, '---', None)] + self.get_list_of_roles(include_logged_in_users=False),
-                },
+                first_element_empty_label=_('Everybody'),
+                roles=self.get_list_of_roles(include_logged_in_users=False),
             )
 
     def get_parameters(self):
diff --git a/wcs/workflows.py b/wcs/workflows.py
index ea58e10b5..d2e23bf42 100644
--- a/wcs/workflows.py
+++ b/wcs/workflows.py
@@ -49,6 +49,7 @@ from .qommon.form import (
     ValidatedStringWidget,
     VarnameWidget,
     WidgetList,
+    WidgetListOfRoles,
     WysiwygTextWidget,
 )
 from .qommon.humantime import seconds2humanduration
@@ -3246,16 +3247,13 @@ class DisplayMessageWorkflowStatusItem(WorkflowStatusItem):
             )
         if 'to' in parameters:
             form.add(
-                WidgetList,
+                WidgetListOfRoles,
                 '%sto' % prefix,
                 title=_('To'),
-                element_type=SingleSelectWidget,
                 value=self.to or [],
                 add_element_label=self.get_add_role_label(),
-                element_kwargs={
-                    'render_br': False,
-                    'options': [(None, '---', None)] + self.get_list_of_roles(include_logged_in_users=False),
-                },
+                first_element_empty_label=_('Everybody'),
+                roles=self.get_list_of_roles(include_logged_in_users=False),
             )
 
     def get_parameters(self):
-- 
2.32.0

