From ea72d1bb0d0adc2a84b223a3a075b2c0837d9422 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Corentin=20S=C3=A9chet?= <csechet@entrouvert.com>
Date: Fri, 1 Apr 2022 21:03:37 +0200
Subject: [PATCH] datasources: add an option to hide disabled users (#58591)

---
 tests/test_datasource_users.py                | 23 +++++++++++++++++++
 wcs/admin/data_sources.py                     |  6 +++++
 wcs/data_sources.py                           |  7 +++++-
 wcs/templates/wcs/backoffice/data-source.html |  7 ++++++
 4 files changed, 42 insertions(+), 1 deletion(-)

diff --git a/tests/test_datasource_users.py b/tests/test_datasource_users.py
index 9e0a42a7..dab4f904 100644
--- a/tests/test_datasource_users.py
+++ b/tests/test_datasource_users.py
@@ -112,3 +112,26 @@ def test_datasource_users(pub):
     assert data_sources.get_structured_items({'type': datasource.slug}) == []
     assert data_sources.get_structured_items(datasource.extended_data_source) == []
     assert tmpl.render(context) == ''
+
+    datasource.users_excluded_roles = []
+    datasource.store()
+    users[1].is_active = False
+    users[1].store()
+    assert not datasource.include_disabled_users
+    assert data_sources.get_structured_items({'type': datasource.slug}) == [{'id': 1, 'text': 'John Doe 0'}]
+    assert data_sources.get_structured_items(datasource.extended_data_source) == [
+        {'id': 1, 'text': 'John Doe 0'}
+    ]
+    assert tmpl.render(context) == 'John Doe 0'
+
+    datasource.include_disabled_users = True
+    datasource.store()
+    assert data_sources.get_structured_items({'type': datasource.slug}) == [
+        {'id': 1, 'text': 'John Doe 0'},
+        {'id': 2, 'text': 'John Doe 1'},
+    ]
+    assert data_sources.get_structured_items(datasource.extended_data_source) == [
+        {'id': 1, 'text': 'John Doe 0'},
+        {'id': 2, 'text': 'John Doe 1'},
+    ]
+    assert tmpl.render(context) == 'John Doe 0, John Doe 1'
diff --git a/wcs/admin/data_sources.py b/wcs/admin/data_sources.py
index f7fdf3c8..9b736562 100644
--- a/wcs/admin/data_sources.py
+++ b/wcs/admin/data_sources.py
@@ -180,6 +180,12 @@ class NamedDataSourceUI:
                 add_element_label=_('Add Role'),
                 element_kwargs={'render_br': False, 'options': options},
             )
+            form.add(
+                CheckboxWidget,
+                'include_disabled_users',
+                title=_('Include disabled users'),
+                value=self.datasource.include_disabled_users,
+            )
         if self.datasource.slug and not self.datasource.is_used():
             form.add(
                 SlugWidget,
diff --git a/wcs/data_sources.py b/wcs/data_sources.py
index 0492fd86..18b75f71 100644
--- a/wcs/data_sources.py
+++ b/wcs/data_sources.py
@@ -319,7 +319,9 @@ def _get_structured_items(data_source, mode=None, raise_on_error=False):
             excluded_roles=data_source.get('excluded_roles'),
             order_by='name',
         )
-        return [{'id': u.id, 'text': u.name} for u in users]
+
+        include_disabled_users = data_source.get('include_disabled_users')
+        return [{'id': u.id, 'text': u.name} for u in users if u.is_active or include_disabled_users]
 
     if data_source.get('type') == 'formula':
         # the result of a python expression, it must be a list.
@@ -471,6 +473,7 @@ class NamedDataSource(XmlStorableObject):
     users_included_roles = None
     users_excluded_roles = None
     category_id = None
+    include_disabled_users = False
 
     SLUG_DASH = '_'
 
@@ -494,6 +497,7 @@ class NamedDataSource(XmlStorableObject):
         ('record_on_errors', 'bool'),
         ('users_included_roles', 'str_list'),
         ('users_excluded_roles', 'str_list'),
+        ('include_disabled_users', 'bool'),
     ]
 
     def __init__(self, name=None):
@@ -553,6 +557,7 @@ class NamedDataSource(XmlStorableObject):
                 {
                     'included_roles': self.users_included_roles,
                     'excluded_roles': self.users_excluded_roles,
+                    'include_disabled_users': self.include_disabled_users,
                 }
             )
             return data_source
diff --git a/wcs/templates/wcs/backoffice/data-source.html b/wcs/templates/wcs/backoffice/data-source.html
index 89339329..9641e00d 100644
--- a/wcs/templates/wcs/backoffice/data-source.html
+++ b/wcs/templates/wcs/backoffice/data-source.html
@@ -43,6 +43,13 @@
           {% endfor %}
       </ul>
   </li>
+  <li>{% trans "Include disabled users :" %}
+    {% if datasource.include_disabled_users %}
+      {% trans "Yes" %}
+    {% else %}
+      {% trans "No" %}
+    {% endif %}
+  </li>
   {% endspaceless %}
   {% endif %}
   {% if datasource.cache_duration %}
-- 
2.35.1

