Projet

Général

Profil

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

Valentin Deniaud, 13 décembre 2022 16:39

Télécharger (5,55 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      | 33 +++++++++++++++++++++++++--------
 2 files changed, 47 insertions(+), 8 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': 'Ignore forms where "Test item" is empty.',
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(form_page, group_by)
248 248
        else:
249 249
            totals_kwargs['criterias'].append(StrictNotEqual('status', 'draft'))
250 250

  
......
323 323

  
324 324
        return criterias
325 325

  
326
    @staticmethod
327
    def get_subfilters(form_page):
326
    def get_subfilters(self, form_page, group_by):
328 327
        subfilters = []
329 328
        field_choices = []
330 329
        for field in form_page.get_formdef_fields():
......
371 370
                field_choices.append((field.contextual_varname, field.label))
372 371

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

  
386
            if group_by not in (None, 'channel', 'simple-status', 'status'):
387
                group_by_field = self.get_group_by_field(form_page, group_by)
388
                if group_by_field:
389
                    additionnal_filters.append(
390
                        {
391
                            'id': 'hide_none_label',
392
                            'label': _('Ignore forms where "%s" is empty.') % group_by_field.label,
393
                            'options': [{'id': 'true', 'label': _('Yes')}, {'id': 'false', 'label': _('No')}],
394
                            'required': True,
395
                            'default': 'false',
396
                        }
397
                    )
398

  
399
            subfilters = additionnal_filters + subfilters
386 400

  
387 401
        return subfilters
388 402

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

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

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

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