From e390fc18611598b765507597a5a96d8336214112 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Tue, 3 May 2022 06:40:34 +0200
Subject: [PATCH] api: consider anonymised forms when retrieving groupby labels
 (#64701)

---
 tests/api/test_statistics.py | 14 +++++++++++---
 wcs/backoffice/management.py |  4 +++-
 wcs/statistics/views.py      |  7 +++++--
 3 files changed, 19 insertions(+), 6 deletions(-)

diff --git a/tests/api/test_statistics.py b/tests/api/test_statistics.py
index 8b8da33a0..cda56ce4d 100644
--- a/tests/api/test_statistics.py
+++ b/tests/api/test_statistics.py
@@ -70,7 +70,12 @@ def formdef(pub):
     )
     item_field.display_locations = ['statistics']
     items_field = fields.ItemsField(
-        id='3', varname='test-items', label='Test items', type='items', items=['foo', 'bar', 'baz']
+        id='3',
+        varname='test-items',
+        label='Test items',
+        type='items',
+        items=['foo', 'bar', 'baz'],
+        anonymise=False,
     )
     items_field.display_locations = ['statistics']
     block_field = fields.BlockField(id='4', label='Block Data', varname='blockdata', type='block:foobar')
@@ -475,7 +480,8 @@ def test_statistics_forms_count_subfilters_query(pub, formdef):
     assert resp.json['data']['series'][0]['data'] == []
 
 
-def test_statistics_forms_count_group_by(pub, formdef):
+@pytest.mark.parametrize('anonymise', [False, True])
+def test_statistics_forms_count_group_by(pub, formdef, anonymise):
     for i in range(20):
         formdata = formdef.data_class()()
         formdata.just_created()
@@ -494,6 +500,8 @@ def test_statistics_forms_count_group_by(pub, formdef):
         else:
             formdata.receipt_time = datetime.datetime(2021, 3, 1, 2, 0).timetuple()
         formdata.store()
+        if anonymise:
+            formdata.anonymise()
 
     # group by item field
     url = '/api/statistics/forms/count/?form=%s' % formdef.url_name
@@ -542,8 +550,8 @@ def test_statistics_forms_count_group_by(pub, formdef):
     assert resp.json['data']['x_labels'] == ['2021-01', '2021-02', '2021-03']
     assert resp.json['data']['series'] == [
         {'data': [3, None, None], 'label': 'No'},
-        {'data': [13, None, None], 'label': 'Yes'},
         {'data': [None, None, 4], 'label': 'None'},
+        {'data': [13, None, None], 'label': 'Yes'},
     ]
 
     # group by status
diff --git a/wcs/backoffice/management.py b/wcs/backoffice/management.py
index fde442b47..9092eaafc 100644
--- a/wcs/backoffice/management.py
+++ b/wcs/backoffice/management.py
@@ -940,12 +940,14 @@ class FormPage(Directory):
         selected_filter,
         selected_filter_operator='eq',
         criterias=None,
+        anonymised=False,
     ):
         criterias = (criterias or [])[:]
         # remove potential filter on self
         criterias = [x for x in criterias if x.attribute != 'f%s' % filter_field.id]
         # apply other filters
-        criterias.append(Null('anonymised'))
+        if not anonymised:
+            criterias.append(Null('anonymised'))
         criterias.append(StrictNotEqual('status', 'draft'))
         if selected_filter == 'all':
             if selected_filter_operator == 'ne':
diff --git a/wcs/statistics/views.py b/wcs/statistics/views.py
index 4b9000e61..215a36053 100644
--- a/wcs/statistics/views.py
+++ b/wcs/statistics/views.py
@@ -221,7 +221,7 @@ class FormsCountView(RestrictedView):
             elif field.type in ('item', 'items'):
                 if not get_publisher().is_using_postgresql():
                     continue
-                options = form_page.get_item_filter_options(field, selected_filter='all')
+                options = form_page.get_item_filter_options(field, selected_filter='all', anonymised=True)
                 if not options:
                     continue
             elif field.type == 'bool':
@@ -295,7 +295,9 @@ class FormsCountView(RestrictedView):
         elif group_by_field.type == 'bool':
             group_labels = {True: _('Yes'), False: _('No')}
         elif group_by_field.type in ('item', 'items'):
-            options = form_page.get_item_filter_options(group_by_field, selected_filter='all')
+            options = form_page.get_item_filter_options(
+                group_by_field, selected_filter='all', anonymised=True
+            )
             group_labels = {option[0]: option[1] for option in options}
         group_labels[None] = _('None')
 
@@ -303,4 +305,5 @@ class FormsCountView(RestrictedView):
         series = [
             {'label': group_labels.get(group, group), 'data': data} for group, data in totals_by_group.items()
         ]
+        series.sort(key=lambda x: x['label'].lower())
         return x_labels, series
-- 
2.36.0

