Projet

Général

Profil

0001-dataviz-display-total-in-tables-37903.patch

Valentin Deniaud, 08 janvier 2020 12:51

Télécharger (4,92 ko)

Voir les différences:

Subject: [PATCH] dataviz: display total in tables (#37903)

 combo/apps/dataviz/models.py |  9 ++++++
 tests/test_dataviz.py        | 57 +++++++++++++++++++++++++++++++++---
 2 files changed, 62 insertions(+), 4 deletions(-)
combo/apps/dataviz/models.py
169 169
            else:
170 170
                ctx['table'] = chart.render_table(
171 171
                    transpose=bool(chart.axis_count == 2),
172
                    total=chart.compute_sum,
172 173
                )
173 174
                ctx['table'] = ctx['table'].replace('<table>', '<table class="main">')
174 175
        return ctx
......
248 249
            # use a single colour for dots
249 250
            chart.config.style.colors = ('#1f77b4',) * len(x_labels)
250 251

  
252
        chart.compute_sum = bool(response.get('measure') == 'integer')
253
        if chart.compute_sum and self.chart_type == 'table':
254
            if chart.axis_count < 2:  # workaround pygal
255
                chart.compute_sum = False
256
                if chart.axis_count == 1:
257
                    data.append(sum(data))
258
                    x_labels.append('Total')
259

  
251 260
        if self.chart_type != 'pie':
252 261
            for i, serie_label in enumerate(y_labels):
253 262
                if chart.axis_count < 2:
tests/test_dataviz.py
118 118
            'data': [222, 134, 53],
119 119
            'axis': {
120 120
                'x_labels': ['web', 'mail', 'email']
121
            }
121
            },
122
            'measure': 'integer',
122 123
        }
123 124
        return {'content': json.dumps(response), 'request': request, 'status_code': 200}
124 125
    if url.path == '/visualization/2/json/':
......
127 128
            'data': [222, 134, 53],
128 129
            'axis': {
129 130
                'y_labels': ['web', 'mail', 'email']
130
            }
131
            },
132
            'measure': 'integer',
131 133
        }
132 134
        return {'content': json.dumps(response), 'request': request, 'status_code': 200}
133 135
    if url.path == '/visualization/3/json/':
......
140 142
            'axis': {
141 143
                'x_labels': ['web', 'mail', 'email'],
142 144
                'y_labels': ['foo', 'bar'],
143
            }
145
            },
146
            'measure': 'integer',
144 147
        }
145 148
        return {'content': json.dumps(response), 'request': request, 'status_code': 200}
146 149
    if url.path == '/visualization/4/json/':
147 150
        response = {
148 151
            'format': '1',
149 152
            'data': 222,
150
            'axis': {}
153
            'axis': {},
154
            'measure': 'integer',
151 155
        }
152 156
        return {'content': json.dumps(response), 'request': request, 'status_code': 200}
153 157
    if url.path == '/visualization/5/json/':
......
455 459
                (u'plop:tenth', False, u'tenth visualization (percents)'),
456 460
                (u'plop:third', False, u'third visualization (X/Y)'),
457 461
            ]
462

  
463

  
464
def test_table_cell(app, admin_user):
465
    page = Page(title='One', slug='index')
466
    page.save()
467

  
468
    app = login(app)
469

  
470
    with override_settings(KNOWN_SERVICES={
471
            'bijoe': {'plop': {'title': 'test', 'url': 'https://bijoe.example.com',
472
                      'secret': 'combo', 'orig': 'combo'}}}):
473
        with HTTMock(bijoe_mock):
474
            cell = ChartNgCell(page=page, order=1, placeholder='content')
475
            cell.data_reference = 'plop:example'
476
            cell.chart_type = 'table'
477
            cell.save()
478
            resp = app.get('/api/dataviz/graph/1/')
479
            resp = app.get('/')
480
            assert resp.text.count('Total') == 1
481

  
482
            cell.data_reference = 'plop:second'
483
            cell.save()
484
            resp = app.get('/api/dataviz/graph/1/')
485
            resp = app.get('/')
486
            assert resp.text.count('Total') == 1
487

  
488
            cell.data_reference = 'plop:third'
489
            cell.save()
490
            resp = app.get('/api/dataviz/graph/1/')
491
            resp = app.get('/')
492
            assert '114' in resp.text
493
            assert resp.text.count('Total') == 2
494

  
495
            cell.data_reference = 'plop:fourth'
496
            cell.save()
497
            resp = app.get('/api/dataviz/graph/1/')
498
            resp = app.get('/')
499
            assert resp.text.count('Total') == 0
500

  
501
            # total of durations is not computed
502
            cell.data_reference = 'plop:eigth'
503
            cell.save()
504
            resp = app.get('/api/dataviz/graph/1/')
505
            resp = app.get('/')
506
            assert resp.text.count('Total') == 0
458
-