Projet

Général

Profil

0002-dataviz-pass-request-to-spooler-refresh-task-65882.patch

Valentin Deniaud, 07 juin 2022 12:27

Télécharger (4,21 ko)

Voir les différences:

Subject: [PATCH 2/2] dataviz: pass request to spooler refresh task (#65882)

 combo/apps/dataviz/models.py |  6 +++---
 combo/utils/spooler.py       |  8 ++++----
 tests/test_dataviz.py        | 22 ++++++++++++++--------
 3 files changed, 21 insertions(+), 15 deletions(-)
combo/apps/dataviz/models.py
321 321
        )
322 322

  
323 323
    def get_chart(self, width=None, height=None, request=None, raise_if_not_cached=False):
324
        transaction.on_commit(lambda: spooler.refresh_statistics_data(cell_pk=self.pk))
324
        transaction.on_commit(lambda: spooler.refresh_statistics_data(cell_pk=self.pk, request=request))
325 325
        response = self.get_statistic_data(request=request, raise_if_not_cached=raise_if_not_cached)
326 326
        response.raise_for_status()
327 327
        response = response.json()
......
713 713
    def available_filters(self):
714 714
        return self.statistic.filters + self.subfilters
715 715

  
716
    def update_subfilters(self):
716
    def update_subfilters(self, request=None):
717 717
        try:
718
            response = self.get_statistic_data(request=get_request())
718
            response = self.get_statistic_data(request=request or get_request())
719 719
        except (TemplateSyntaxError, VariableDoesNotExist):
720 720
            return
721 721

  
combo/utils/spooler.py
94 94

  
95 95

  
96 96
@tenantspool
97
def refresh_statistics_data(cell_pk):
97
def refresh_statistics_data(cell_pk, request):
98 98
    from combo.apps.dataviz.models import ChartNgCell, MissingRequest, MissingVariable
99 99

  
100 100
    try:
......
103 103
        return
104 104

  
105 105
    try:
106
        cell.get_statistic_data(invalidate_cache=True)
107
    except (MissingRequest, MissingVariable):
106
        cell.get_statistic_data(request=request, invalidate_cache=True)
107
    except MissingVariable:
108 108
        return
109 109

  
110 110
    if cell.statistic.service_slug != 'bijoe':
111
        cell.update_subfilters()
111
        cell.update_subfilters(request)
tests/test_dataviz.py
2576 2576
    cell.statistic = Statistic.objects.get(slug='one-serie')
2577 2577
    cell.save()
2578 2578

  
2579
    refresh_statistics_data(cell.pk)
2579
    refresh_statistics_data(cell.pk, request=mock.MagicMock())
2580 2580
    assert len(new_api_mock.call['requests']) == 1
2581 2581

  
2582
    refresh_statistics_data(cell.pk)
2582
    refresh_statistics_data(cell.pk, request=mock.MagicMock())
2583 2583
    assert len(new_api_mock.call['requests']) == 2
2584 2584

  
2585
    # variables cannot be evaluated in spooler
2585
    # variables can be evaluated in spooler
2586 2586
    page.extra_variables = {'test': 'test'}
2587 2587
    page.save()
2588 2588
    cell.filter_params = {'ou': 'variable:test'}
2589 2589
    cell.save()
2590
    refresh_statistics_data(cell.pk)
2591
    assert len(new_api_mock.call['requests']) == 2
2590
    refresh_statistics_data(cell.pk, request=mock.MagicMock())
2591
    assert len(new_api_mock.call['requests']) == 3
2592

  
2593
    # missing variable
2594
    cell.filter_params = {'ou': 'variable:unknown'}
2595
    cell.save()
2596
    refresh_statistics_data(cell.pk, request=mock.MagicMock())
2597
    assert len(new_api_mock.call['requests']) == 3
2592 2598

  
2593 2599
    ChartNgCell.objects.all().delete()
2594
    refresh_statistics_data(cell.pk)
2595
    assert len(new_api_mock.call['requests']) == 2
2600
    refresh_statistics_data(cell.pk, request=mock.MagicMock())
2601
    assert len(new_api_mock.call['requests']) == 3
2596 2602

  
2597 2603

  
2598 2604
@with_httmock(bijoe_mock)
......
2602 2608
    cell.statistic = Statistic.objects.get(slug='example')
2603 2609
    cell.save()
2604 2610

  
2605
    refresh_statistics_data(cell.pk)
2611
    refresh_statistics_data(cell.pk, request=None)
2606 2612
    assert len(bijoe_mock.call['requests']) == 1
2607 2613

  
2608 2614

  
2609
-