From 4e434e5a74d0e8c907868dbeb357e2f3f3d2f5a1 Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Thu, 2 Jun 2022 18:26:44 +0200 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(-) diff --git a/combo/apps/dataviz/models.py b/combo/apps/dataviz/models.py index 4f6e9aef..8d9c7c0c 100644 --- a/combo/apps/dataviz/models.py +++ b/combo/apps/dataviz/models.py @@ -321,7 +321,7 @@ class ChartNgCell(CellBase): ) def get_chart(self, width=None, height=None, request=None, raise_if_not_cached=False): - transaction.on_commit(lambda: spooler.refresh_statistics_data(cell_pk=self.pk)) + transaction.on_commit(lambda: spooler.refresh_statistics_data(cell_pk=self.pk, request=request)) response = self.get_statistic_data(request=request, raise_if_not_cached=raise_if_not_cached) response.raise_for_status() response = response.json() @@ -713,9 +713,9 @@ class ChartNgCell(CellBase): def available_filters(self): return self.statistic.filters + self.subfilters - def update_subfilters(self): + def update_subfilters(self, request=None): try: - response = self.get_statistic_data(request=get_request()) + response = self.get_statistic_data(request=request or get_request()) except (TemplateSyntaxError, VariableDoesNotExist): return diff --git a/combo/utils/spooler.py b/combo/utils/spooler.py index ac63a0c5..87d3bfd0 100644 --- a/combo/utils/spooler.py +++ b/combo/utils/spooler.py @@ -94,7 +94,7 @@ def refresh_statistics_list(): @tenantspool -def refresh_statistics_data(cell_pk): +def refresh_statistics_data(cell_pk, request): from combo.apps.dataviz.models import ChartNgCell, MissingRequest, MissingVariable try: @@ -103,9 +103,9 @@ def refresh_statistics_data(cell_pk): return try: - cell.get_statistic_data(invalidate_cache=True) - except (MissingRequest, MissingVariable): + cell.get_statistic_data(request=request, invalidate_cache=True) + except MissingVariable: return if cell.statistic.service_slug != 'bijoe': - cell.update_subfilters() + cell.update_subfilters(request) diff --git a/tests/test_dataviz.py b/tests/test_dataviz.py index 13b3a8e5..bf1232e8 100644 --- a/tests/test_dataviz.py +++ b/tests/test_dataviz.py @@ -2576,23 +2576,29 @@ def test_spooler_refresh_statistics_data(new_api_statistics): cell.statistic = Statistic.objects.get(slug='one-serie') cell.save() - refresh_statistics_data(cell.pk) + refresh_statistics_data(cell.pk, request=mock.MagicMock()) assert len(new_api_mock.call['requests']) == 1 - refresh_statistics_data(cell.pk) + refresh_statistics_data(cell.pk, request=mock.MagicMock()) assert len(new_api_mock.call['requests']) == 2 - # variables cannot be evaluated in spooler + # variables can be evaluated in spooler page.extra_variables = {'test': 'test'} page.save() cell.filter_params = {'ou': 'variable:test'} cell.save() - refresh_statistics_data(cell.pk) - assert len(new_api_mock.call['requests']) == 2 + refresh_statistics_data(cell.pk, request=mock.MagicMock()) + assert len(new_api_mock.call['requests']) == 3 + + # missing variable + cell.filter_params = {'ou': 'variable:unknown'} + cell.save() + refresh_statistics_data(cell.pk, request=mock.MagicMock()) + assert len(new_api_mock.call['requests']) == 3 ChartNgCell.objects.all().delete() - refresh_statistics_data(cell.pk) - assert len(new_api_mock.call['requests']) == 2 + refresh_statistics_data(cell.pk, request=mock.MagicMock()) + assert len(new_api_mock.call['requests']) == 3 @with_httmock(bijoe_mock) @@ -2602,7 +2608,7 @@ def test_spooler_refresh_statistics_data_bijoe(statistics): cell.statistic = Statistic.objects.get(slug='example') cell.save() - refresh_statistics_data(cell.pk) + refresh_statistics_data(cell.pk, request=None) assert len(bijoe_mock.call['requests']) == 1 -- 2.30.2