dataviz: pass request to spooler refresh task (#65882)

This commit is contained in:
Valentin Deniaud 2022-06-02 18:26:44 +02:00
parent 06b4b1dce5
commit 7a4bc2c02f
3 changed files with 22 additions and 16 deletions

View File

@ -324,7 +324,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()
@ -716,9 +716,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

View File

@ -94,8 +94,8 @@ def refresh_statistics_list():
@tenantspool
def refresh_statistics_data(cell_pk):
from combo.apps.dataviz.models import ChartNgCell, MissingRequest, MissingVariable
def refresh_statistics_data(cell_pk, request):
from combo.apps.dataviz.models import ChartNgCell, MissingVariable
try:
cell = ChartNgCell.objects.get(pk=cell_pk)
@ -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)

View File

@ -2596,23 +2596,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)
@ -2622,7 +2628,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