Project

General

Profile

0001-api-consider-anonymised-forms-when-retrieving-groupb.patch

Frédéric Péters, 03 May 2022 07:25 AM

Download (4.68 KB)

View differences:

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(-)
tests/api/test_statistics.py
70 70
    )
71 71
    item_field.display_locations = ['statistics']
72 72
    items_field = fields.ItemsField(
73
        id='3', varname='test-items', label='Test items', type='items', items=['foo', 'bar', 'baz']
73
        id='3',
74
        varname='test-items',
75
        label='Test items',
76
        type='items',
77
        items=['foo', 'bar', 'baz'],
78
        anonymise=False,
74 79
    )
75 80
    items_field.display_locations = ['statistics']
76 81
    block_field = fields.BlockField(id='4', label='Block Data', varname='blockdata', type='block:foobar')
......
475 480
    assert resp.json['data']['series'][0]['data'] == []
476 481

  
477 482

  
478
def test_statistics_forms_count_group_by(pub, formdef):
483
@pytest.mark.parametrize('anonymise', [False, True])
484
def test_statistics_forms_count_group_by(pub, formdef, anonymise):
479 485
    for i in range(20):
480 486
        formdata = formdef.data_class()()
481 487
        formdata.just_created()
......
494 500
        else:
495 501
            formdata.receipt_time = datetime.datetime(2021, 3, 1, 2, 0).timetuple()
496 502
        formdata.store()
503
        if anonymise:
504
            formdata.anonymise()
497 505

  
498 506
    # group by item field
499 507
    url = '/api/statistics/forms/count/?form=%s' % formdef.url_name
......
542 550
    assert resp.json['data']['x_labels'] == ['2021-01', '2021-02', '2021-03']
543 551
    assert resp.json['data']['series'] == [
544 552
        {'data': [3, None, None], 'label': 'No'},
545
        {'data': [13, None, None], 'label': 'Yes'},
546 553
        {'data': [None, None, 4], 'label': 'None'},
554
        {'data': [13, None, None], 'label': 'Yes'},
547 555
    ]
548 556

  
549 557
    # group by status
wcs/backoffice/management.py
940 940
        selected_filter,
941 941
        selected_filter_operator='eq',
942 942
        criterias=None,
943
        anonymised=False,
943 944
    ):
944 945
        criterias = (criterias or [])[:]
945 946
        # remove potential filter on self
946 947
        criterias = [x for x in criterias if x.attribute != 'f%s' % filter_field.id]
947 948
        # apply other filters
948
        criterias.append(Null('anonymised'))
949
        if not anonymised:
950
            criterias.append(Null('anonymised'))
949 951
        criterias.append(StrictNotEqual('status', 'draft'))
950 952
        if selected_filter == 'all':
951 953
            if selected_filter_operator == 'ne':
wcs/statistics/views.py
221 221
            elif field.type in ('item', 'items'):
222 222
                if not get_publisher().is_using_postgresql():
223 223
                    continue
224
                options = form_page.get_item_filter_options(field, selected_filter='all')
224
                options = form_page.get_item_filter_options(field, selected_filter='all', anonymised=True)
225 225
                if not options:
226 226
                    continue
227 227
            elif field.type == 'bool':
......
295 295
        elif group_by_field.type == 'bool':
296 296
            group_labels = {True: _('Yes'), False: _('No')}
297 297
        elif group_by_field.type in ('item', 'items'):
298
            options = form_page.get_item_filter_options(group_by_field, selected_filter='all')
298
            options = form_page.get_item_filter_options(
299
                group_by_field, selected_filter='all', anonymised=True
300
            )
299 301
            group_labels = {option[0]: option[1] for option in options}
300 302
        group_labels[None] = _('None')
301 303

  
......
303 305
        series = [
304 306
            {'label': group_labels.get(group, group), 'data': data} for group, data in totals_by_group.items()
305 307
        ]
308
        series.sort(key=lambda x: x['label'].lower())
306 309
        return x_labels, series
307
-