Projet

Général

Profil

0001-statistics-add-filter-to-hide-None-labels-71660.patch

Valentin Deniaud, 13 décembre 2022 15:31

Télécharger (5,44 ko)

Voir les différences:

Subject: [PATCH] statistics: add filter to hide "None" labels (#71660)

 tests/api/test_statistics.py | 22 ++++++++++++++++++++++
 wcs/statistics/views.py      | 32 +++++++++++++++++++++++++-------
 2 files changed, 47 insertions(+), 7 deletions(-)
tests/api/test_statistics.py
446 446
            {'id': 'checkbox', 'label': 'Checkbox'},
447 447
            {'id': 'status', 'label': 'Status'},
448 448
        ],
449
        'has_subfilters': True,
449 450
    }
450 451

  
451 452
    # check item field subfilter
......
499 500
        'required': True,
500 501
    }
501 502

  
503
    # group by triggers new subfilter
504
    new_resp = get_app(pub).get(
505
        sign_uri('/api/statistics/forms/count/?form=%s&group-by=test-item' % formdef.url_name)
506
    )
507
    assert new_resp.json['data']['subfilters'][1] == {
508
        'id': 'hide_none_label',
509
        'label': 'Hide values corresponding to "None" label',
510
        'options': [{'id': 'true', 'label': 'Yes'}, {'id': 'false', 'label': 'No'}],
511
        'required': True,
512
        'default': 'false',
513
    }
514
    assert len(new_resp.json['data']['subfilters']) == len(resp.json['data']['subfilters']) + 1
515

  
502 516
    # add item field with no formdata, it should not appear
503 517
    item_field = fields.ItemField(
504 518
        id='20',
......
716 730
        {'label': 'None', 'data': [4, None, None, None, None, None, None]},
717 731
    ]
718 732

  
733
    # hide None label
734
    resp = get_app(pub).get(sign_uri(url + '&group-by=test-item&hide_none_label=true'))
735
    assert resp.json['data']['x_labels'] == ['2021-01']
736
    assert resp.json['data']['series'] == [
737
        {'data': [13], 'label': 'Foo'},
738
        {'data': [3], 'label': 'baz'},
739
    ]
740

  
719 741
    # group by items field
720 742
    url = '/api/statistics/forms/count/?form=%s' % formdef.url_name
721 743
    resp = get_app(pub).get(sign_uri(url + '&group-by=test-items'))
wcs/statistics/views.py
30 30
from wcs.formdata import FormData
31 31
from wcs.formdef import FormDef
32 32
from wcs.qommon import _, misc, pgettext_lazy
33
from wcs.qommon.storage import Contains, Equal, GreaterOrEqual, Less, Null, Or, StrictNotEqual
33
from wcs.qommon.storage import Contains, Equal, GreaterOrEqual, Less, NotNull, Null, Or, StrictNotEqual
34 34

  
35 35

  
36 36
class RestrictedView(View):
......
244 244
            totals_kwargs['criterias'].append(Equal('formdef_id', formdef.id))
245 245
            totals_kwargs['criterias'].extend(self.get_filters_criterias(formdef, form_page))
246 246
            self.set_group_by_parameters(group_by, formdef, form_page, totals_kwargs, group_labels)
247
            subfilters = self.get_subfilters(form_page)
247
            subfilters = self.get_subfilters(
248
                form_page, show_none_label_filter=bool('group_by' in totals_kwargs)
249
            )
248 250
        else:
249 251
            totals_kwargs['criterias'].append(StrictNotEqual('status', 'draft'))
250 252

  
......
324 326
        return criterias
325 327

  
326 328
    @staticmethod
327
    def get_subfilters(form_page):
329
    def get_subfilters(form_page, show_none_label_filter):
328 330
        subfilters = []
329 331
        field_choices = []
330 332
        for field in form_page.get_formdef_fields():
......
371 373
                field_choices.append((field.contextual_varname, field.label))
372 374

  
373 375
        if field_choices:
374
            subfilters.insert(
375
                0,
376
            additionnal_filters = [
376 377
                {
377 378
                    'id': 'group-by',
378 379
                    'label': _('Group by'),
......
381 382
                        {'id': 'simple-status', 'label': _('Simplified status')},
382 383
                    ]
383 384
                    + [{'id': x[0], 'label': x[1]} for x in field_choices],
384
                },
385
            )
385
                    'has_subfilters': True,
386
                }
387
            ]
388

  
389
            if show_none_label_filter:
390
                additionnal_filters.append(
391
                    {
392
                        'id': 'hide_none_label',
393
                        'label': _('Hide values corresponding to "None" label'),
394
                        'options': [{'id': 'true', 'label': _('Yes')}, {'id': 'false', 'label': _('No')}],
395
                        'required': True,
396
                        'default': 'false',
397
                    }
398
                )
399

  
400
            subfilters = additionnal_filters + subfilters
386 401

  
387 402
        return subfilters
388 403

  
......
441 456
        else:
442 457
            totals_kwargs['group_by'] = sql.get_field_id(group_by_field)
443 458

  
459
        if self.request.GET.get('hide_none_label') == 'true':
460
            totals_kwargs['criterias'].append(NotNull(totals_kwargs['group_by']))
461

  
444 462
        group_labels.update(self.get_group_labels(group_by_field, formdef, form_page, group_by))
445 463

  
446 464
    def get_grouped_time_data(self, totals, group_labels):
447
-